/Skimpt3/Skimpt3/controls/GlassButton.cs

http://skimpt.googlecode.com/ · C# · 908 lines · 717 code · 76 blank · 115 comment · 99 complexity · e266ed0d4f8178a749dc35b0eea84ccd MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Text;
  10. using PushButtonState = System.Windows.Forms.VisualStyles.PushButtonState;
  11. namespace SkimptControls
  12. {
  13. /// <summary>
  14. /// Represents a glass button control.
  15. /// </summary>
  16. [ToolboxBitmap(typeof(GlassButton)), ToolboxItem(true), ToolboxItemFilter("System.Windows.Forms"), Description("Raises an event when the user clicks it.")]
  17. public partial class GlassButton : Button
  18. {
  19. #region " Constructors "
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="Glass.GlassButton" /> class.
  22. /// </summary>
  23. public GlassButton()
  24. {
  25. InitializeComponent();
  26. timer.Interval = animationLength / framesCount;
  27. base.BackColor = Color.Transparent;
  28. BackColor = Color.Black;
  29. ForeColor = Color.White;
  30. OuterBorderColor = Color.White;
  31. InnerBorderColor = Color.Black;
  32. ShineColor = Color.White;
  33. GlowColor = Color.FromArgb(-7488001);//unchecked((int)(0xFF8DBDFF)));
  34. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
  35. SetStyle(ControlStyles.Opaque, false);
  36. }
  37. #endregion
  38. #region " Fields and Properties "
  39. private Color _backColor;
  40. /// <summary>
  41. /// Gets or sets the background color of the control.
  42. /// </summary>
  43. /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the background color.</returns>
  44. [DefaultValue(typeof(Color), "Black")]
  45. public virtual new Color BackColor
  46. {
  47. get { return _backColor; }
  48. set
  49. {
  50. if (!_backColor.Equals(value))
  51. {
  52. _backColor = value;
  53. UseVisualStyleBackColor = false;
  54. CreateFrames();
  55. OnBackColorChanged(EventArgs.Empty);
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Gets or sets the foreground color of the control.
  61. /// </summary>
  62. /// <returns>The foreground <see cref="T:System.Drawing.Color" /> of the control.</returns>
  63. [DefaultValue(typeof(Color), "White")]
  64. public virtual new Color ForeColor
  65. {
  66. get { return base.ForeColor; }
  67. set
  68. {
  69. base.ForeColor = value;
  70. }
  71. }
  72. private Color _innerBorderColor;
  73. /// <summary>
  74. /// Gets or sets the inner border color of the control.
  75. /// </summary>
  76. /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the color of the inner border.</returns>
  77. [DefaultValue(typeof(Color), "Black"), Category("Appearance"), Description("The inner border color of the control.")]
  78. public virtual Color InnerBorderColor
  79. {
  80. get { return _innerBorderColor; }
  81. set
  82. {
  83. if (_innerBorderColor != value)
  84. {
  85. _innerBorderColor = value;
  86. CreateFrames();
  87. if (IsHandleCreated)
  88. {
  89. Invalidate();
  90. }
  91. OnInnerBorderColorChanged(EventArgs.Empty);
  92. }
  93. }
  94. }
  95. private Color _outerBorderColor;
  96. /// <summary>
  97. /// Gets or sets the outer border color of the control.
  98. /// </summary>
  99. /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the color of the outer border.</returns>
  100. [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("The outer border color of the control.")]
  101. public virtual Color OuterBorderColor
  102. {
  103. get { return _outerBorderColor; }
  104. set
  105. {
  106. if (_outerBorderColor != value)
  107. {
  108. _outerBorderColor = value;
  109. CreateFrames();
  110. if (IsHandleCreated)
  111. {
  112. Invalidate();
  113. }
  114. OnOuterBorderColorChanged(EventArgs.Empty);
  115. }
  116. }
  117. }
  118. private Color _shineColor;
  119. /// <summary>
  120. /// Gets or sets the shine color of the control.
  121. /// </summary>
  122. /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the shine color.</returns>
  123. [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("The shine color of the control.")]
  124. public virtual Color ShineColor
  125. {
  126. get { return _shineColor; }
  127. set
  128. {
  129. if (_shineColor != value)
  130. {
  131. _shineColor = value;
  132. CreateFrames();
  133. if (IsHandleCreated)
  134. {
  135. Invalidate();
  136. }
  137. OnShineColorChanged(EventArgs.Empty);
  138. }
  139. }
  140. }
  141. private Color _glowColor;
  142. /// <summary>
  143. /// Gets or sets the glow color of the control.
  144. /// </summary>
  145. /// <returns>A <see cref="T:System.Drawing.Color" /> value representing the glow color.</returns>
  146. [DefaultValue(typeof(Color), "255,141,189,255"), Category("Appearance"), Description("The glow color of the control.")]
  147. public virtual Color GlowColor
  148. {
  149. get { return _glowColor; }
  150. set
  151. {
  152. if (_glowColor != value)
  153. {
  154. _glowColor = value;
  155. CreateFrames();
  156. if (IsHandleCreated)
  157. {
  158. Invalidate();
  159. }
  160. OnGlowColorChanged(EventArgs.Empty);
  161. }
  162. }
  163. }
  164. private bool _fadeOnFocus;
  165. /// <summary>
  166. /// Gets or sets a value indicating whether the button should fade in and fade out when it's getting and loosing the focus.
  167. /// </summary>
  168. /// <value><c>true</c> if fading with changing the focus; otherwise, <c>false</c>.</value>
  169. [DefaultValue(false), Category("Appearance"), Description("Indicates whether the button should fade in and fade out when it is getting and loosing the focus.")]
  170. public virtual bool FadeOnFocus
  171. {
  172. get { return _fadeOnFocus; }
  173. set
  174. {
  175. if (_fadeOnFocus != value)
  176. {
  177. _fadeOnFocus = value;
  178. }
  179. }
  180. }
  181. private bool _isHovered;
  182. private bool _isFocused;
  183. private bool _isFocusedByKey;
  184. private bool _isKeyDown;
  185. private bool _isMouseDown;
  186. private bool IsPressed { get { return _isKeyDown || (_isMouseDown && _isHovered); } }
  187. /// <summary>
  188. /// Gets the state of the button control.
  189. /// </summary>
  190. /// <value>The state of the button control.</value>
  191. [Browsable(false)]
  192. public PushButtonState State
  193. {
  194. get
  195. {
  196. if (!Enabled)
  197. {
  198. return PushButtonState.Disabled;
  199. }
  200. if (IsPressed)
  201. {
  202. return PushButtonState.Pressed;
  203. }
  204. if (_isHovered)
  205. {
  206. return PushButtonState.Hot;
  207. }
  208. if (_isFocused || IsDefault)
  209. {
  210. return PushButtonState.Default;
  211. }
  212. return PushButtonState.Normal;
  213. }
  214. }
  215. #endregion
  216. #region " Events "
  217. /// <summary>Occurs when the value of the <see cref="P:Glass.GlassButton.InnerBorderColor" /> property changes.</summary>
  218. [Description("Event raised when the value of the InnerBorderColor property is changed."), Category("Property Changed")]
  219. public event EventHandler InnerBorderColorChanged;
  220. /// <summary>
  221. /// Raises the <see cref="E:Glass.GlassButton.InnerBorderColorChanged" /> event.
  222. /// </summary>
  223. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  224. protected virtual void OnInnerBorderColorChanged(EventArgs e)
  225. {
  226. if (InnerBorderColorChanged != null)
  227. {
  228. InnerBorderColorChanged(this, e);
  229. }
  230. }
  231. /// <summary>Occurs when the value of the <see cref="P:Glass.GlassButton.OuterBorderColor" /> property changes.</summary>
  232. [Description("Event raised when the value of the OuterBorderColor property is changed."), Category("Property Changed")]
  233. public event EventHandler OuterBorderColorChanged;
  234. /// <summary>
  235. /// Raises the <see cref="E:Glass.GlassButton.OuterBorderColorChanged" /> event.
  236. /// </summary>
  237. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  238. protected virtual void OnOuterBorderColorChanged(EventArgs e)
  239. {
  240. if (OuterBorderColorChanged != null)
  241. {
  242. OuterBorderColorChanged(this, e);
  243. }
  244. }
  245. /// <summary>Occurs when the value of the <see cref="P:Glass.GlassButton.ShineColor" /> property changes.</summary>
  246. [Description("Event raised when the value of the ShineColor property is changed."), Category("Property Changed")]
  247. public event EventHandler ShineColorChanged;
  248. /// <summary>
  249. /// Raises the <see cref="E:Glass.GlassButton.ShineColorChanged" /> event.
  250. /// </summary>
  251. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  252. protected virtual void OnShineColorChanged(EventArgs e)
  253. {
  254. if (ShineColorChanged != null)
  255. {
  256. ShineColorChanged(this, e);
  257. }
  258. }
  259. /// <summary>Occurs when the value of the <see cref="P:Glass.GlassButton.GlowColor" /> property changes.</summary>
  260. [Description("Event raised when the value of the GlowColor property is changed."), Category("Property Changed")]
  261. public event EventHandler GlowColorChanged;
  262. /// <summary>
  263. /// Raises the <see cref="E:Glass.GlassButton.GlowColorChanged" /> event.
  264. /// </summary>
  265. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  266. protected virtual void OnGlowColorChanged(EventArgs e)
  267. {
  268. if (GlowColorChanged != null)
  269. {
  270. GlowColorChanged(this, e);
  271. }
  272. }
  273. #endregion
  274. #region " Overrided Methods "
  275. /// <summary>
  276. /// Raises the <see cref="E:System.Windows.Forms.Control.SizeChanged" /> event.
  277. /// </summary>
  278. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  279. protected override void OnSizeChanged(EventArgs e)
  280. {
  281. CreateFrames();
  282. base.OnSizeChanged(e);
  283. }
  284. /// <summary>
  285. /// Raises the <see cref="E:System.Windows.Forms.Control.Click" /> event.
  286. /// </summary>
  287. /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
  288. protected override void OnClick(EventArgs e)
  289. {
  290. _isKeyDown = _isMouseDown = false;
  291. base.OnClick(e);
  292. }
  293. /// <summary>
  294. /// Raises the <see cref="E:System.Windows.Forms.Control.Enter" /> event.
  295. /// </summary>
  296. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  297. protected override void OnEnter(EventArgs e)
  298. {
  299. _isFocused = _isFocusedByKey = true;
  300. base.OnEnter(e);
  301. if (_fadeOnFocus)
  302. {
  303. FadeIn();
  304. }
  305. }
  306. /// <summary>
  307. /// Raises the <see cref="E:System.Windows.Forms.Control.Leave" /> event.
  308. /// </summary>
  309. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  310. protected override void OnLeave(EventArgs e)
  311. {
  312. base.OnLeave(e);
  313. _isFocused = _isFocusedByKey = _isKeyDown = _isMouseDown = false;
  314. Invalidate();
  315. if (_fadeOnFocus)
  316. {
  317. FadeOut();
  318. }
  319. }
  320. /// <summary>
  321. /// Raises the <see cref="M:System.Windows.Forms.ButtonBase.OnKeyUp(System.Windows.Forms.KeyEventArgs)" /> event.
  322. /// </summary>
  323. /// <param name="e">A <see cref="T:System.Windows.Forms.KeyEventArgs" /> that contains the event data.</param>
  324. protected override void OnKeyDown(KeyEventArgs e)
  325. {
  326. if (e.KeyCode == Keys.Space)
  327. {
  328. _isKeyDown = true;
  329. Invalidate();
  330. }
  331. base.OnKeyDown(e);
  332. }
  333. /// <summary>
  334. /// Raises the <see cref="M:System.Windows.Forms.ButtonBase.OnKeyUp(System.Windows.Forms.KeyEventArgs)" /> event.
  335. /// </summary>
  336. /// <param name="e">A <see cref="T:System.Windows.Forms.KeyEventArgs" /> that contains the event data.</param>
  337. protected override void OnKeyUp(KeyEventArgs e)
  338. {
  339. if (_isKeyDown && e.KeyCode == Keys.Space)
  340. {
  341. _isKeyDown = false;
  342. Invalidate();
  343. }
  344. base.OnKeyUp(e);
  345. }
  346. /// <summary>
  347. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseDown" /> event.
  348. /// </summary>
  349. /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.</param>
  350. protected override void OnMouseDown(MouseEventArgs e)
  351. {
  352. if (!_isMouseDown && e.Button == MouseButtons.Left)
  353. {
  354. _isMouseDown = true;
  355. _isFocusedByKey = false;
  356. Invalidate();
  357. }
  358. base.OnMouseDown(e);
  359. }
  360. /// <summary>
  361. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseUp" /> event.
  362. /// </summary>
  363. /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.</param>
  364. protected override void OnMouseUp(MouseEventArgs e)
  365. {
  366. if (_isMouseDown)
  367. {
  368. _isMouseDown = false;
  369. Invalidate();
  370. }
  371. base.OnMouseUp(e);
  372. }
  373. /// <summary>
  374. /// Raises the <see cref="M:System.Windows.Forms.Control.OnMouseMove(System.Windows.Forms.MouseEventArgs)" /> event.
  375. /// </summary>
  376. /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.</param>
  377. protected override void OnMouseMove(MouseEventArgs e)
  378. {
  379. base.OnMouseMove(e);
  380. if (e.Button != MouseButtons.None)
  381. {
  382. if (!ClientRectangle.Contains(e.X, e.Y))
  383. {
  384. if (_isHovered)
  385. {
  386. _isHovered = false;
  387. Invalidate();
  388. }
  389. }
  390. else if (!_isHovered)
  391. {
  392. _isHovered = true;
  393. Invalidate();
  394. }
  395. }
  396. }
  397. /// <summary>
  398. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseEnter" /> event.
  399. /// </summary>
  400. /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
  401. protected override void OnMouseEnter(EventArgs e)
  402. {
  403. _isHovered = true;
  404. FadeIn();
  405. Invalidate();
  406. base.OnMouseEnter(e);
  407. }
  408. /// <summary>
  409. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseLeave" /> event.
  410. /// </summary>
  411. /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
  412. protected override void OnMouseLeave(EventArgs e)
  413. {
  414. _isHovered = false;
  415. if (!(FadeOnFocus && _isFocusedByKey)) FadeOut();
  416. Invalidate();
  417. base.OnMouseLeave(e);
  418. }
  419. #endregion
  420. #region " Painting "
  421. /// <summary>
  422. /// Raises the <see cref="M:System.Windows.Forms.ButtonBase.OnPaint(System.Windows.Forms.PaintEventArgs)" /> event.
  423. /// </summary>
  424. /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data.</param>
  425. protected override void OnPaint(PaintEventArgs e)
  426. {
  427. DrawButtonBackgroundFromBuffer(e.Graphics);
  428. DrawForegroundFromButton(e);
  429. DrawButtonForeground(e.Graphics);
  430. if (Paint != null)
  431. {
  432. Paint(this, e);
  433. }
  434. }
  435. /// <summary>
  436. /// Occurs when the control is redrawn.
  437. /// </summary>
  438. public new event PaintEventHandler Paint;
  439. private void DrawButtonBackgroundFromBuffer(Graphics graphics)
  440. {
  441. int frame;
  442. if (!Enabled)
  443. {
  444. frame = FRAME_DISABLED;
  445. }
  446. else if (IsPressed)
  447. {
  448. frame = FRAME_PRESSED;
  449. }
  450. else if (!IsAnimating && _currentFrame == 0)
  451. {
  452. frame = FRAME_NORMAL;
  453. }
  454. else
  455. {
  456. if (!HasAnimationFrames)
  457. {
  458. CreateFrames(true);
  459. }
  460. frame = FRAME_ANIMATED + _currentFrame;
  461. }
  462. if (_frames == null || _frames.Count == 0)
  463. {
  464. CreateFrames();
  465. }
  466. graphics.DrawImage(_frames[frame], Point.Empty);
  467. }
  468. private Image CreateBackgroundFrame(bool pressed, bool hovered,
  469. bool animating, bool enabled, float glowOpacity)
  470. {
  471. Rectangle rect = ClientRectangle;
  472. if (rect.Width <= 0)
  473. {
  474. rect.Width = 1;
  475. }
  476. if (rect.Height <= 0)
  477. {
  478. rect.Height = 1;
  479. }
  480. Image img = new Bitmap(rect.Width, rect.Height);
  481. using (Graphics g = Graphics.FromImage(img))
  482. {
  483. g.Clear(Color.Transparent);
  484. DrawButtonBackground(g, rect, pressed, hovered, animating, enabled,
  485. _outerBorderColor, _backColor, _glowColor, _shineColor, _innerBorderColor,
  486. glowOpacity);
  487. }
  488. return img;
  489. }
  490. private static void DrawButtonBackground(Graphics g, Rectangle rectangle,
  491. bool pressed, bool hovered, bool animating, bool enabled,
  492. Color outerBorderColor, Color backColor, Color glowColor, Color shineColor,
  493. Color innerBorderColor, float glowOpacity)
  494. {
  495. SmoothingMode sm = g.SmoothingMode;
  496. g.SmoothingMode = SmoothingMode.AntiAlias;
  497. #region " white border "
  498. Rectangle rect = rectangle;
  499. rect.Width--;
  500. rect.Height--;
  501. using (GraphicsPath bw = CreateRoundRectangle(rect, 4))
  502. {
  503. using (Pen p = new Pen(outerBorderColor))
  504. {
  505. g.DrawPath(p, bw);
  506. }
  507. }
  508. #endregion
  509. rect.X++;
  510. rect.Y++;
  511. rect.Width -= 2;
  512. rect.Height -= 2;
  513. Rectangle rect2 = rect;
  514. rect2.Height >>= 1;
  515. #region " content "
  516. using (GraphicsPath bb = CreateRoundRectangle(rect, 2))
  517. {
  518. int opacity = pressed ? 0xcc : 0x7f;
  519. using (Brush br = new SolidBrush(Color.FromArgb(opacity, backColor)))
  520. {
  521. g.FillPath(br, bb);
  522. }
  523. }
  524. #endregion
  525. #region " glow "
  526. if ((hovered || animating) && !pressed)
  527. {
  528. using (GraphicsPath clip = CreateRoundRectangle(rect, 2))
  529. {
  530. g.SetClip(clip, CombineMode.Intersect);
  531. using (GraphicsPath brad = CreateBottomRadialPath(rect))
  532. {
  533. using (PathGradientBrush pgr = new PathGradientBrush(brad))
  534. {
  535. int opacity = (int)(0xB2 * glowOpacity + .5f);
  536. RectangleF bounds = brad.GetBounds();
  537. pgr.CenterPoint = new PointF((bounds.Left + bounds.Right) / 2f, (bounds.Top + bounds.Bottom) / 2f);
  538. pgr.CenterColor = Color.FromArgb(opacity, glowColor);
  539. pgr.SurroundColors = new Color[] { Color.FromArgb(0, glowColor) };
  540. g.FillPath(pgr, brad);
  541. }
  542. }
  543. g.ResetClip();
  544. }
  545. }
  546. #endregion
  547. #region " shine "
  548. if (rect2.Width > 0 && rect2.Height > 0)
  549. {
  550. rect2.Height++;
  551. using (GraphicsPath bh = CreateTopRoundRectangle(rect2, 2))
  552. {
  553. rect2.Height++;
  554. int opacity = 0x99;
  555. if (pressed | !enabled)
  556. {
  557. opacity = (int)(.4f * opacity + .5f);
  558. }
  559. using (LinearGradientBrush br = new LinearGradientBrush(rect2, Color.FromArgb(opacity, shineColor), Color.FromArgb(opacity / 3, shineColor), LinearGradientMode.Vertical))
  560. {
  561. g.FillPath(br, bh);
  562. }
  563. }
  564. rect2.Height -= 2;
  565. }
  566. #endregion
  567. #region " black border "
  568. using (GraphicsPath bb = CreateRoundRectangle(rect, 3))
  569. {
  570. using (Pen p = new Pen(innerBorderColor))
  571. {
  572. g.DrawPath(p, bb);
  573. }
  574. }
  575. #endregion
  576. g.SmoothingMode = sm;
  577. }
  578. private void DrawButtonForeground(Graphics g)
  579. {
  580. if (Focused && ShowFocusCues/* && isFocusedByKey*/)
  581. {
  582. Rectangle rect = ClientRectangle;
  583. rect.Inflate(-4, -4);
  584. ControlPaint.DrawFocusRectangle(g, rect);
  585. }
  586. }
  587. private Button _imageButton;
  588. private void DrawForegroundFromButton(PaintEventArgs pevent)
  589. {
  590. if (_imageButton == null)
  591. {
  592. _imageButton = new Button();
  593. _imageButton.Parent = new TransparentControl();
  594. _imageButton.SuspendLayout();
  595. _imageButton.BackColor = Color.Transparent;
  596. _imageButton.FlatAppearance.BorderSize = 0;
  597. _imageButton.FlatStyle = FlatStyle.Flat;
  598. }
  599. else
  600. {
  601. _imageButton.SuspendLayout();
  602. }
  603. _imageButton.AutoEllipsis = AutoEllipsis;
  604. if (Enabled)
  605. {
  606. _imageButton.ForeColor = ForeColor;
  607. }
  608. else
  609. {
  610. _imageButton.ForeColor = Color.FromArgb((3 * ForeColor.R + _backColor.R) >> 2,
  611. (3 * ForeColor.G + _backColor.G) >> 2,
  612. (3 * ForeColor.B + _backColor.B) >> 2);
  613. }
  614. _imageButton.Font = Font;
  615. _imageButton.RightToLeft = RightToLeft;
  616. _imageButton.Image = Image;
  617. if (Image != null && !Enabled)
  618. {
  619. Size size = Image.Size;
  620. float[][] newColorMatrix = new float[5][];
  621. newColorMatrix[0] = new float[] { 0.2125f, 0.2125f, 0.2125f, 0f, 0f };
  622. newColorMatrix[1] = new float[] { 0.2577f, 0.2577f, 0.2577f, 0f, 0f };
  623. newColorMatrix[2] = new float[] { 0.0361f, 0.0361f, 0.0361f, 0f, 0f };
  624. float[] arr = new float[5];
  625. arr[3] = 1f;
  626. newColorMatrix[3] = arr;
  627. newColorMatrix[4] = new float[] { 0.38f, 0.38f, 0.38f, 0f, 1f };
  628. System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(newColorMatrix);
  629. System.Drawing.Imaging.ImageAttributes disabledImageAttr = new System.Drawing.Imaging.ImageAttributes();
  630. disabledImageAttr.ClearColorKey();
  631. disabledImageAttr.SetColorMatrix(matrix);
  632. _imageButton.Image = new Bitmap(Image.Width, Image.Height);
  633. using (Graphics gr = Graphics.FromImage(_imageButton.Image))
  634. {
  635. gr.DrawImage(Image, new Rectangle(0, 0, size.Width, size.Height), 0, 0, size.Width, size.Height, GraphicsUnit.Pixel, disabledImageAttr);
  636. }
  637. }
  638. _imageButton.ImageAlign = ImageAlign;
  639. _imageButton.ImageIndex = ImageIndex;
  640. _imageButton.ImageKey = ImageKey;
  641. _imageButton.ImageList = ImageList;
  642. _imageButton.Padding = Padding;
  643. _imageButton.Size = Size;
  644. _imageButton.Text = Text;
  645. _imageButton.TextAlign = TextAlign;
  646. _imageButton.TextImageRelation = TextImageRelation;
  647. _imageButton.UseCompatibleTextRendering = UseCompatibleTextRendering;
  648. _imageButton.UseMnemonic = UseMnemonic;
  649. _imageButton.ResumeLayout();
  650. InvokePaint(_imageButton, pevent);
  651. if (_imageButton.Image != null && _imageButton.Image != Image)
  652. {
  653. _imageButton.Image.Dispose();
  654. _imageButton.Image = null;
  655. }
  656. }
  657. private class TransparentControl : Control
  658. {
  659. protected override void OnPaintBackground(PaintEventArgs pevent) { }
  660. protected override void OnPaint(PaintEventArgs e) { }
  661. }
  662. private static GraphicsPath CreateRoundRectangle(Rectangle rectangle, int radius)
  663. {
  664. GraphicsPath path = new GraphicsPath();
  665. int l = rectangle.Left;
  666. int t = rectangle.Top;
  667. int w = rectangle.Width;
  668. int h = rectangle.Height;
  669. int d = radius << 1;
  670. path.AddArc(l, t, d, d, 180, 90); // topleft
  671. path.AddLine(l + radius, t, l + w - radius, t); // top
  672. path.AddArc(l + w - d, t, d, d, 270, 90); // topright
  673. path.AddLine(l + w, t + radius, l + w, t + h - radius); // right
  674. path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
  675. path.AddLine(l + w - radius, t + h, l + radius, t + h); // bottom
  676. path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
  677. path.AddLine(l, t + h - radius, l, t + radius); // left
  678. path.CloseFigure();
  679. return path;
  680. }
  681. private static GraphicsPath CreateTopRoundRectangle(Rectangle rectangle, int radius)
  682. {
  683. GraphicsPath path = new GraphicsPath();
  684. int l = rectangle.Left;
  685. int t = rectangle.Top;
  686. int w = rectangle.Width;
  687. int h = rectangle.Height;
  688. int d = radius << 1;
  689. path.AddArc(l, t, d, d, 180, 90); // topleft
  690. path.AddLine(l + radius, t, l + w - radius, t); // top
  691. path.AddArc(l + w - d, t, d, d, 270, 90); // topright
  692. path.AddLine(l + w, t + radius, l + w, t + h); // right
  693. path.AddLine(l + w, t + h, l, t + h); // bottom
  694. path.AddLine(l, t + h, l, t + radius); // left
  695. path.CloseFigure();
  696. return path;
  697. }
  698. private static GraphicsPath CreateBottomRadialPath(Rectangle rectangle)
  699. {
  700. GraphicsPath path = new GraphicsPath();
  701. RectangleF rect = rectangle;
  702. rect.X -= rect.Width * .35f;
  703. rect.Y -= rect.Height * .15f;
  704. rect.Width *= 1.7f;
  705. rect.Height *= 2.3f;
  706. path.AddEllipse(rect);
  707. path.CloseFigure();
  708. return path;
  709. }
  710. #endregion
  711. #region " Unused Properties & Events "
  712. /// <summary>This property is not relevant for this class.</summary>
  713. /// <returns>This property is not relevant for this class.</returns>
  714. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
  715. public new FlatButtonAppearance FlatAppearance
  716. {
  717. get { return base.FlatAppearance; }
  718. }
  719. /// <summary>This property is not relevant for this class.</summary>
  720. /// <returns>This property is not relevant for this class.</returns>
  721. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
  722. public new FlatStyle FlatStyle
  723. {
  724. get { return base.FlatStyle; }
  725. set { base.FlatStyle = value; }
  726. }
  727. /// <summary>This property is not relevant for this class.</summary>
  728. /// <returns>This property is not relevant for this class.</returns>
  729. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
  730. public new bool UseVisualStyleBackColor
  731. {
  732. get { return base.UseVisualStyleBackColor; }
  733. set { base.UseVisualStyleBackColor = value; }
  734. }
  735. #endregion
  736. #region " Animation Support "
  737. private List<Image> _frames;
  738. private const int FRAME_DISABLED = 0;
  739. private const int FRAME_PRESSED = 1;
  740. private const int FRAME_NORMAL = 2;
  741. private const int FRAME_ANIMATED = 3;
  742. private bool HasAnimationFrames
  743. {
  744. get
  745. {
  746. return _frames != null && _frames.Count > FRAME_ANIMATED;
  747. }
  748. }
  749. private void CreateFrames()
  750. {
  751. CreateFrames(false);
  752. }
  753. private void CreateFrames(bool withAnimationFrames)
  754. {
  755. DestroyFrames();
  756. if (!IsHandleCreated)
  757. {
  758. return;
  759. }
  760. if (_frames == null)
  761. {
  762. _frames = new List<Image>();
  763. }
  764. _frames.Add(CreateBackgroundFrame(false, false, false, false, 0));
  765. _frames.Add(CreateBackgroundFrame(true, true, false, true, 0));
  766. _frames.Add(CreateBackgroundFrame(false, false, false, true, 0));
  767. if (!withAnimationFrames)
  768. {
  769. return;
  770. }
  771. for (int i = 0; i < framesCount; i++)
  772. {
  773. _frames.Add(CreateBackgroundFrame(false, true, true, true, (float)i / (framesCount - 1F)));
  774. }
  775. }
  776. private void DestroyFrames()
  777. {
  778. if (_frames != null)
  779. {
  780. while (_frames.Count > 0)
  781. {
  782. _frames[_frames.Count - 1].Dispose();
  783. _frames.RemoveAt(_frames.Count - 1);
  784. }
  785. }
  786. }
  787. private const int animationLength = 300;
  788. private const int framesCount = 10;
  789. private int _currentFrame;
  790. private int _direction;
  791. private bool IsAnimating
  792. {
  793. get
  794. {
  795. return _direction != 0;
  796. }
  797. }
  798. private void FadeIn()
  799. {
  800. _direction = 1;
  801. timer.Enabled = true;
  802. }
  803. private void FadeOut()
  804. {
  805. _direction = -1;
  806. timer.Enabled = true;
  807. }
  808. private void timer_Tick(object sender, EventArgs e)
  809. {
  810. if (!timer.Enabled)
  811. {
  812. return;
  813. }
  814. Refresh();
  815. _currentFrame += _direction;
  816. if (_currentFrame == -1)
  817. {
  818. _currentFrame = 0;
  819. timer.Enabled = false;
  820. _direction = 0;
  821. return;
  822. }
  823. if (_currentFrame == framesCount)
  824. {
  825. _currentFrame = framesCount - 1;
  826. timer.Enabled = false;
  827. _direction = 0;
  828. }
  829. }
  830. #endregion
  831. }
  832. }