Chocolade19721309

Untitled

May 7th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.84 KB | None | 0 0
  1. #region Author/About
  2. /************************************************************************************
  3. * GlowButton v1.1 *
  4. * *
  5. * Created: Febuary 24, 2010 *
  6. * Built on: Win7 *
  7. * Purpose: Glowing Button control *
  8. * Revision: 1.1 *
  9. * Tested On: Win7 32bit, Vista 64bit, XP Professional *
  10. * IDE: C# 2008 SP1 FW 3.5 *
  11. * Referenced: Control Library VTD *
  12. * Author: John Underhill (Steppenwolfe) *
  13. * *
  14. *************************************************************************************
  15.  
  16. You can not:
  17. -Sell or redistribute this code or the binary for profit.
  18. -Use this in spyware, malware, or any generally acknowledged form of malicious software.
  19. -Remove or alter the above author accreditation, or this disclaimer.
  20.  
  21. You can:
  22. -Use this code in your applications in any way you like.
  23. -Use this in a published program, (a credit to vtdev.com would be nice)
  24.  
  25. I will not:
  26. -Except any responsibility for this code whatsoever.
  27. -Modify on demand.. you have the source code, read it, learn from it, write it.
  28. -There is no guarantee of fitness, nor should you have any expectation of support.
  29. -I further renounce any and all responsibilities for this code, in every way conceivable,
  30. now, and for the rest of time.
  31.  
  32. -> Added missing disabled effects
  33. -> Changed glow method to a SetPixel based routine
  34.  
  35. Cheers,
  36. John
  37. */
  38. #endregion
  39.  
  40. #region Directives
  41. using System;
  42. using System.ComponentModel;
  43. using System.Drawing;
  44. using System.Drawing.Imaging;
  45. using System.Drawing.Drawing2D;
  46. using System.Windows.Forms;
  47. #endregion
  48.  
  49. namespace GlowButton
  50. {
  51. [DefaultEvent("MouseClick")]
  52. public partial class GlowButton : UserControl
  53. {
  54. #region Constants
  55. // adjust disabled image alpha level
  56. private const float FADE_LEVEL = .7F;
  57. // adjust mirror image alpha level
  58. private const float MIRROR_LEVEL = .15F;
  59. // glow color alpha
  60. private const int GLOW_LEVEL = 40;
  61. // alpha maximum before pixel is changed
  62. private const int GLOW_THRESHHOLD = 96;
  63. // glow padding
  64. private const int SIZE_OFFSET = 2;
  65. #endregion
  66.  
  67. #region Enums
  68. private enum ButtonState : int
  69. {
  70. Normal = 0,
  71. Pressed,
  72. Focused,
  73. Hover,
  74. Disabled,
  75. Checked
  76. }
  77.  
  78. public enum CheckedStyle : int
  79. {
  80. None = 0,
  81. Border,
  82. ColorChange
  83. }
  84. #endregion
  85.  
  86. #region Fields
  87. private bool _bCheckState = false;
  88. private bool _bFocusOnHover = false;
  89. private bool _bClicked = false;
  90. private bool _bFocusedMask = false;
  91. private bool _bImageMirror = false;
  92. private int _iGlowFactor = 2;
  93.  
  94. private CheckedStyle _eUseCheckStyle = CheckedStyle.None;
  95. private ButtonState _eButtonState = ButtonState.Normal;
  96.  
  97. private Color _clrCheckedBorderColor = Color.WhiteSmoke;
  98. private Color _clrImageCheckedColor = Color.SteelBlue;
  99. private Color _clrImageDisabledColor = Color.Transparent;
  100. private Color _clrImageFocusedColor = Color.SkyBlue;
  101. private Color _clrImageGlowColor = Color.WhiteSmoke;
  102. private Color _clrImageHoverColor = Color.LightSkyBlue;
  103. private Color _clrImageMirrorColor = Color.WhiteSmoke;
  104. private Color _clrImagePressedColor = Color.SteelBlue;
  105.  
  106. private Image _imgImage;
  107. private Image _imgBackgroundImage;
  108. private Bitmap _bmpBuffer;
  109. private Bitmap _bmpGlow;
  110. private Bitmap _bmpMirror;
  111. #endregion
  112.  
  113. #region Constructor
  114. public GlowButton()
  115. {
  116. InitializeComponent();
  117. Create();
  118. }
  119. private void Create()
  120. {
  121. if (_bmpBuffer != null)
  122. _bmpBuffer.Dispose();
  123. _bmpBuffer = new Bitmap(this.Width, this.Height);
  124. }
  125. private void Destroy()
  126. {
  127. if (_bmpBuffer != null)
  128. _bmpBuffer.Dispose();
  129. if (_bmpGlow != null)
  130. _bmpGlow.Dispose();
  131. if (_bmpMirror != null)
  132. _bmpMirror.Dispose();
  133. }
  134. #endregion
  135.  
  136. #region Properties
  137. #region Hidden Properties
  138. [Browsable(false)]
  139. public new bool AllowDrop
  140. {
  141. get { return base.AllowDrop; }
  142. set { base.AllowDrop = value; }
  143. }
  144. [Browsable(false)]
  145. public new AnchorStyles Anchor
  146. {
  147. get { return base.Anchor; }
  148. set { base.Anchor = value; }
  149. }
  150. [Browsable(false)]
  151. public new bool AutoScroll
  152. {
  153. get { return base.AutoScroll; }
  154. set { base.AutoScroll = value; }
  155. }
  156. [Browsable(false)]
  157. public new Size AutoScrollMargin
  158. {
  159. get { return base.AutoScrollMargin; }
  160. set { base.AutoScrollMargin = value; }
  161. }
  162. [Browsable(false)]
  163. public new Size AutoScrollMinSize
  164. {
  165. get { return base.AutoScrollMinSize; }
  166. set { base.AutoScrollMinSize = value; }
  167. }
  168. [Browsable(false)]
  169. public new AutoSizeMode AutoSizeMode
  170. {
  171. get { return base.AutoSizeMode; }
  172. set { base.AutoSizeMode = value; }
  173. }
  174. [Browsable(false)]
  175. public new AutoValidate AutoValidate
  176. {
  177. get { return base.AutoValidate; }
  178. set { base.AutoValidate = value; }
  179. }
  180. [Browsable(false)]
  181. public new ImageLayout BackgroundImageLayout
  182. {
  183. get { return base.BackgroundImageLayout; }
  184. set { base.BackgroundImageLayout = value; }
  185. }
  186. [Browsable(false)]
  187. public new ContextMenu ContextMenuStrip
  188. {
  189. get { return base.ContextMenu; }
  190. set { base.ContextMenu = value; }
  191. }
  192. [Browsable(false)]
  193. public new DockStyle Dock
  194. {
  195. get { return base.Dock; }
  196. set { base.Dock = value; }
  197. }
  198. [Browsable(false)]
  199. public new Font Font
  200. {
  201. get { return base.Font; }
  202. set { base.Font = value; }
  203. }
  204. [Browsable(false)]
  205. public new Color ForeColor
  206. {
  207. get { return base.ForeColor; }
  208. set { base.ForeColor = value; }
  209. }
  210. [Browsable(false)]
  211. public new RightToLeft RightToLeft
  212. {
  213. get { return base.RightToLeft; }
  214. set { base.RightToLeft = value; }
  215. }
  216. [Browsable(false)]
  217. public new Padding Padding
  218. {
  219. get { return base.Padding; }
  220. set { base.Padding = value; }
  221. }
  222. #endregion
  223.  
  224. #region Public Properties
  225. /// <summary>Get/Set button Background image</summary>
  226. [Browsable(true), Category("Appearance"), RefreshProperties(RefreshProperties.All),
  227. Description("Get/Set button Background image")]
  228. /*public Image BackgroundImage
  229. {
  230. get { return _imgBackgroundImage; }
  231. set
  232. {
  233. _imgBackgroundImage = value;
  234. if (_imgBackgroundImage != null)
  235. DrawButton();
  236. }
  237. }*/
  238.  
  239. public Image BackgroundImage
  240. {
  241. get { return _imgBackgroundImage; }
  242. set
  243. {
  244. _imgBackgroundImage = value;
  245. if (_imgBackgroundImage != null)
  246. Invalidate(); //Use this instead
  247. }
  248. }
  249.  
  250. /// <summary>Get/Set Checkbox state</summary>
  251. [Browsable(true), Category("Behavior"),
  252. Description("Get/Set Checkbox state")]
  253. public bool Checked
  254. {
  255. get { return _bCheckState; }
  256. set
  257. {
  258. _bCheckState = value;
  259. if (!_bCheckState)
  260. _eButtonState = ButtonState.Normal;
  261. DrawButton();
  262. }
  263. }
  264.  
  265. /// <summary>Get/Set the checked border Color</summary>
  266. [Browsable(true), Category("Appearance"),
  267. Description("Get/Set the checked border Color")]
  268. public Color CheckedBorderColor
  269. {
  270. get { return _clrCheckedBorderColor; }
  271. set { _clrCheckedBorderColor = value; }
  272. }
  273.  
  274. /// <summary>Get/Set Checkbox style</summary>
  275. [Browsable(true), Category("Appearance"),
  276. Description("Get/Set Checkbox style")]
  277. public CheckedStyle CheckStyle
  278. {
  279. get { return _eUseCheckStyle; }
  280. set { _eUseCheckStyle = value; }
  281. }
  282.  
  283. /// <summary>Get/Set Focus on button hover</summary>
  284. [Browsable(true), Category("Behavior"),
  285. Description("Get/Set Focus on button hover")]
  286. public bool FocusOnHover
  287. {
  288. get { return _bFocusOnHover; }
  289. set { _bFocusOnHover = value; }
  290. }
  291.  
  292. /// <summary>Get/Set Draw a Focused mask</summary>
  293. [Browsable(true), Category("Appearance"),
  294. Description("Get/Set Draw a Focused mask")]
  295. public bool FocusedMask
  296. {
  297. get { return _bFocusedMask; }
  298. set { _bFocusedMask = value; }
  299. }
  300.  
  301. /// <summary>Get/Set button image</summary>
  302. [Browsable(true), Category("Appearance"), RefreshProperties(RefreshProperties.All),
  303. Description("Get/Set button Image")]
  304. public Image Image
  305. {
  306. get { return _imgImage; }
  307. set
  308. {
  309. _imgImage = value;
  310. if (_imgImage != null)
  311. {
  312. if (this.AutoSize && this.DesignMode)
  313. ResetSize();
  314. if (this.ImageMirror)
  315. CreateMirror();
  316. CreateGlow();
  317. DrawButton();
  318. }
  319. }
  320. }
  321.  
  322. /// <summary>Get/Set the image checked Color</summary>
  323. [Browsable(true), Category("Appearance"),
  324. Description("Get/Set the image checked Color")]
  325. public Color ImageCheckedColor
  326. {
  327. get { return _clrImageCheckedColor; }
  328. set { _clrImageCheckedColor = value; }
  329. }
  330.  
  331. /// <summary>Get/Set the image disabled Color</summary>
  332. [Browsable(true), Category("Appearance"),
  333. Description("Get/Set the image disabled Color")]
  334. public Color ImageDisabledColor
  335. {
  336. get { return _clrImageDisabledColor; }
  337. set { _clrImageDisabledColor = value; }
  338. }
  339.  
  340. /// <summary>Get/Set the image focused Color</summary>
  341. [Browsable(true), Category("Appearance"),
  342. Description("Get/Set the image focused Color")]
  343. public Color ImageFocusedColor
  344. {
  345. get { return _clrImageFocusedColor; }
  346. set { _clrImageFocusedColor = value; }
  347. }
  348.  
  349. /// <summary>Get/Set the glow Color</summary>
  350. [Browsable(true), Category("Appearance"), RefreshProperties(RefreshProperties.All),
  351. Description("Get/Set the glow Color")]
  352. public Color ImageGlowColor
  353. {
  354. get { return _clrImageGlowColor; }
  355. set
  356. {
  357. _clrImageGlowColor = value;
  358. CreateGlow();
  359. }
  360. }
  361.  
  362. /// <summary>Get/Set the hover Color</summary>
  363. [Browsable(true), Category("Appearance"),
  364. Description("Get/Set the hover Color")]
  365. public Color ImageHoverColor
  366. {
  367. get { return _clrImageHoverColor; }
  368. set { _clrImageHoverColor = value; }
  369. }
  370.  
  371. /// <summary>Get/Set the pressed Color</summary>
  372. [Browsable(true), Category("Appearance"),
  373. Description("Get/Set the pressed Color")]
  374. public Color ImagePressedColor
  375. {
  376. get { return _clrImagePressedColor; }
  377. set { _clrImagePressedColor = value; }
  378. }
  379.  
  380. /// <summary>Get/Set Glow factor Depth</summary>
  381. [Browsable(true), Category("Appearance"), RefreshProperties(RefreshProperties.All),
  382. Description("Get/Set Glow factor Depth")]
  383. public int ImageGlowFactor
  384. {
  385. get { return _iGlowFactor; }
  386. set
  387. {
  388. _iGlowFactor = value;
  389. CreateGlow();
  390. }
  391. }
  392.  
  393. /// <summary>Get/Set Image Mirror effect</summary>
  394. [Browsable(true), Category("Behavior"), RefreshProperties(RefreshProperties.All),
  395. Description("Get/Set Image Mirror effect")]
  396. public bool ImageMirror
  397. {
  398. get { return _bImageMirror; }
  399. set
  400. {
  401. _bImageMirror = value;
  402. if (_bImageMirror)
  403. CreateMirror();
  404. if (this.Image != null && this.AutoSize && this.DesignMode)
  405. ResetSize();
  406. DrawButton();
  407. }
  408. }
  409.  
  410. // New property to control the hover/click effect
  411. private bool _applyDarkEffect = true;
  412. public bool ApplyDarkEffect
  413. {
  414. get { return _applyDarkEffect; }
  415. set
  416. {
  417. _applyDarkEffect = value;
  418. Invalidate(); // Redraw to reflect changes immediately
  419. }
  420. }
  421.  
  422. private string _buttonText = "Button"; // Default text
  423. public string ButtonText
  424. {
  425. get { return _buttonText; }
  426. set
  427. {
  428. _buttonText = value;
  429. Invalidate(); // Redraw the button when text changes
  430. }
  431. }
  432. #endregion
  433. #endregion
  434.  
  435. #region Overrides
  436. protected override void OnEnabledChanged(EventArgs e)
  437. {
  438. if (this.Enabled == false)
  439. _eButtonState = ButtonState.Disabled;
  440. else
  441. _eButtonState = ButtonState.Normal;
  442. DrawButton();
  443. base.OnEnabledChanged(e);
  444. }
  445.  
  446. protected override void OnGotFocus(EventArgs e)
  447. {
  448. _eButtonState = ButtonState.Focused;
  449. if (_bClicked)
  450. _bClicked = false;
  451. else
  452. DrawButton();
  453. base.OnGotFocus(e);
  454. }
  455.  
  456. protected override void OnHandleCreated(EventArgs e)
  457. {
  458. Create();
  459. base.OnHandleCreated(e);
  460. }
  461.  
  462. protected override void OnHandleDestroyed(EventArgs e)
  463. {
  464. Destroy();
  465. base.OnHandleDestroyed(e);
  466. }
  467.  
  468. protected override void OnLostFocus(EventArgs e)
  469. {
  470. _eButtonState = ButtonState.Normal;
  471. DrawButton();
  472. base.OnLostFocus(e);
  473. }
  474.  
  475. protected override void OnMouseClick(MouseEventArgs e)
  476. {
  477. if (e.Button == MouseButtons.Right && SystemInformation.MouseButtonsSwapped || e.Button == MouseButtons.Left)
  478. this.Checked = !this.Checked;
  479. DrawButton();
  480. base.OnMouseClick(e);
  481. }
  482.  
  483. protected override void OnMouseDown(MouseEventArgs e)
  484. {
  485. if (e.Button == MouseButtons.Right && SystemInformation.MouseButtonsSwapped || e.Button == MouseButtons.Left)
  486. {
  487. _eButtonState = ApplyDarkEffect ? ButtonState.Pressed : ButtonState.Focused;
  488. DrawButton();
  489. _bClicked = true;
  490. }
  491. base.OnMouseDown(e);
  492. }
  493.  
  494. protected override void OnMouseEnter(EventArgs e)
  495. {
  496. if (ApplyDarkEffect)
  497. {
  498. _eButtonState = ButtonState.Normal; // Make it glow when the mouse enters
  499. }
  500. base.OnMouseEnter(e);
  501. }
  502.  
  503. protected override void OnMouseLeave(EventArgs e)
  504. {
  505. _eButtonState = ButtonState.Normal; // Return to normal state when the mouse leaves
  506. base.OnMouseLeave(e);
  507. }
  508.  
  509. protected override void OnMouseUp(MouseEventArgs e)
  510. {
  511. _eButtonState = ButtonState.Normal;
  512. DrawButton();
  513. Invalidate();
  514. base.OnMouseUp(e);
  515. }
  516.  
  517. protected override void OnPaint(PaintEventArgs e)
  518. {
  519. base.OnPaint(e);
  520. DrawButton();
  521. DrawButtonText(e.Graphics);
  522. }
  523.  
  524. private void DrawButtonText(Graphics graphics)
  525. {
  526. using (Font font = new Font("Arial", 12, FontStyle.Bold))
  527. {
  528. SizeF textSize = graphics.MeasureString(_buttonText, font);
  529. PointF locationToDraw = new PointF((this.Width / 2) - (textSize.Width / 2), (this.Height / 2) - (textSize.Height / 2));
  530.  
  531. using (SolidBrush brush = new SolidBrush(Color.White)) // Ensure text color contrasts well with button
  532. {
  533. graphics.DrawString(_buttonText, font, brush, locationToDraw);
  534. }
  535. }
  536. }
  537.  
  538. protected override void OnResize(EventArgs e)
  539. {
  540. Create();
  541. base.OnResize(e);
  542. }
  543. #endregion
  544.  
  545. #region Methods
  546. /// <summary>Create the Glow image</summary>
  547. private void CreateGlow()
  548. {
  549. if (_bmpGlow != null)
  550. _bmpGlow.Dispose();
  551. Rectangle imageRect = new Rectangle(0, 0, this.Image.Width + this.ImageGlowFactor, this.Image.Height + this.ImageGlowFactor);
  552. _bmpGlow = new Bitmap(imageRect.Width, imageRect.Height);
  553. using (Graphics g = Graphics.FromImage(_bmpGlow))
  554. g.DrawImage(this.Image, imageRect);
  555. int i = 0;
  556. for (int x = 0; x < imageRect.Height; x++)
  557. {
  558. for (i = 0; i < imageRect.Width; i++)
  559. {
  560. if (_bmpGlow.GetPixel(i, x).A > GLOW_THRESHHOLD)
  561. _bmpGlow.SetPixel(i, x, Color.FromArgb(GLOW_LEVEL, this.ImageGlowColor));
  562. }
  563. i = 0;
  564. }
  565. }
  566.  
  567. /// <summary>Create the Mirror image</summary>
  568. private void CreateMirror()
  569. {
  570. if (_bmpMirror != null)
  571. _bmpMirror.Dispose();
  572.  
  573. int height = (int)(this.Image.Height * .7f);
  574. int width = (int)(this.Image.Width * 1f);
  575. Rectangle imageRect = new Rectangle(0, 0, width, height);
  576. _bmpMirror = new Bitmap(imageRect.Width, imageRect.Height);
  577.  
  578. using (Graphics g = Graphics.FromImage(_bmpMirror))
  579. g.DrawImage(this.Image, imageRect);
  580. _bmpMirror.RotateFlip(RotateFlipType.Rotate180FlipX);
  581. }
  582.  
  583. /// <summary>Create a rounded rectangle GraphicsPath</summary>
  584. private GraphicsPath CreateRoundRectanglePath(Graphics g, Rectangle bounds, float radius)
  585. {
  586. // create a path
  587. GraphicsPath pathBounds = new GraphicsPath();
  588. // arc top left
  589. pathBounds.AddArc(bounds.Left, bounds.Top, radius, radius, 180, 90);
  590. // line top
  591. pathBounds.AddLine(bounds.Left + radius, bounds.Top, bounds.Right - radius, bounds.Top);
  592. // arc top right
  593. pathBounds.AddArc(bounds.Right - radius, bounds.Top, radius, radius, 270, 90);
  594. // line right
  595. pathBounds.AddLine(bounds.Right, bounds.Top + radius, bounds.Right, bounds.Bottom - radius);
  596. // arc bottom right
  597. pathBounds.AddArc(bounds.Right - radius, bounds.Bottom - radius, radius, radius, 0, 90);
  598. // line bottom
  599. pathBounds.AddLine(bounds.Right - radius, bounds.Bottom, bounds.Left + radius, bounds.Bottom);
  600. // arc bottom left
  601. pathBounds.AddArc(bounds.Left, bounds.Bottom - radius, radius, radius, 90, 90);
  602. // line left
  603. pathBounds.AddLine(bounds.Left, bounds.Bottom - radius, bounds.Left, bounds.Top + radius);
  604. pathBounds.CloseFigure();
  605. return pathBounds;
  606. }
  607.  
  608. /// <summary>Backfill the buffer</summary>
  609. private void DrawBackGround(Graphics g, Rectangle bounds)
  610. {
  611. if (this.BackgroundImage != null)
  612. {
  613. using (ImageAttributes ia = new ImageAttributes())
  614. g.DrawImage(this.BackgroundImage, bounds, 0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height, GraphicsUnit.Pixel, ia);
  615. }
  616. else
  617. {
  618. using (Brush br = new SolidBrush(this.BackColor))
  619. g.FillRectangle(br, bounds);
  620. }
  621. }
  622.  
  623. /// <summary>Draw border on checked style</summary>
  624. private void DrawBorder(Graphics g, Rectangle bounds, Color clr)
  625. {
  626. bounds.Inflate(-2, -2);
  627. using (GraphicsPath borderPath = CreateRoundRectanglePath(g, bounds, 4))
  628. {
  629. // top-left bottom-right -dark
  630. using (LinearGradientBrush borderBrush = new LinearGradientBrush(
  631. bounds,
  632. Color.FromArgb(140, clr),
  633. Color.FromArgb(140, Color.White),
  634. LinearGradientMode.BackwardDiagonal))
  635. {
  636. Blend blnd = new Blend();
  637. blnd.Positions = new float[] { 0f, .5f, 1f };
  638. blnd.Factors = new float[] { 1f, 0f, 1f };
  639. borderBrush.Blend = blnd;
  640. using (Pen borderPen = new Pen(borderBrush, 2f))
  641. g.DrawPath(borderPen, borderPath);
  642. }
  643. }
  644. }
  645.  
  646. /// <summary>Drawing hub</summary>
  647. private void DrawButton()
  648. {
  649. if (this.Image == null)
  650. return;
  651.  
  652. Rectangle bounds = new Rectangle(0, 0, this.Width, this.Height);
  653. Rectangle imageBounds = GetImageBounds(bounds, this.Image);
  654.  
  655. // draw into a buffer
  656. using (Graphics g = Graphics.FromImage(_bmpBuffer))
  657. {
  658. g.SmoothingMode = SmoothingMode.HighQuality;
  659. DrawBackGround(g, bounds);
  660. if (this.ImageMirror)
  661. DrawMirror(g, imageBounds);
  662.  
  663. if (this.CheckStyle != CheckedStyle.None && this.Checked == true)
  664. _eButtonState = ButtonState.Checked;
  665. else if (this.CheckStyle != CheckedStyle.None && this.Checked == false && _eButtonState == ButtonState.Focused)
  666. _eButtonState = ButtonState.Normal;
  667. switch (_eButtonState)
  668. {
  669. case ButtonState.Checked:
  670. {
  671. if (this.CheckStyle == CheckedStyle.Border)
  672. {
  673. DrawBorder(g, bounds, this.CheckedBorderColor);
  674. DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
  675. }
  676. else if (this.CheckStyle == CheckedStyle.ColorChange)
  677. {
  678. DrawColoredImage(g, this.Image, imageBounds, this.ImageCheckedColor);
  679. }
  680. else
  681. {
  682. DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
  683. }
  684. break;
  685. }
  686. case ButtonState.Disabled:
  687. {
  688. if (this.ImageDisabledColor == Color.Transparent)
  689. DrawFadedImage(g, this.Image, imageBounds);
  690. else
  691. DrawColoredImage(g, this.Image, imageBounds, this.ImageDisabledColor);
  692. break;
  693. }
  694. case ButtonState.Focused:
  695. {
  696. DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
  697. if (this.FocusedMask)
  698. {
  699. DrawMask(g, bounds);
  700. DrawBorder(g, bounds, Color.DarkGray);
  701. }
  702. break;
  703. }
  704. case ButtonState.Hover:
  705. {
  706. DrawGlow(g, this.Image, imageBounds);
  707. DrawColoredImage(g, this.Image, imageBounds, this.ImageHoverColor);
  708. break;
  709. }
  710. case ButtonState.Normal:
  711. {
  712. DrawImage(g, this.Image, imageBounds);
  713. break;
  714. }
  715. case ButtonState.Pressed:
  716. {
  717. DrawColoredImage(g, this.Image, imageBounds, this.ImagePressedColor);
  718. break;
  719. }
  720. }
  721. }
  722. // draw the buffer
  723. using (Graphics g = Graphics.FromHwnd(this.Handle))
  724. DrawImage(g, _bmpBuffer, bounds);
  725. }
  726.  
  727. /// <summary>Draw a colored bitmap</summary>
  728. private void DrawColoredImage(Graphics g, Image img, Rectangle bounds, Color clr)
  729. {
  730. using (ImageAttributes ia = new ImageAttributes())
  731. {
  732. ColorMatrix cm = new ColorMatrix();
  733. // convert and refactor color palette
  734. cm.Matrix00 = ParseColor(clr.R);
  735. cm.Matrix11 = ParseColor(clr.G);
  736. cm.Matrix22 = ParseColor(clr.B);
  737. cm.Matrix33 = ParseColor(clr.A);
  738. cm.Matrix44 = 1f;
  739. // set matrix
  740. ia.SetColorMatrix(cm);
  741. // draw
  742. g.DrawImage(img, bounds, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
  743. }
  744. }
  745.  
  746. /// <summary>draw a faded bitmap</summary>
  747. private void DrawFadedImage(Graphics g, Image img, Rectangle bounds)
  748. {
  749. using (ImageAttributes ia = new ImageAttributes())
  750. {
  751. ColorMatrix cm = new ColorMatrix();
  752. cm.Matrix00 = 1f; //r
  753. cm.Matrix11 = 1f; //g
  754. cm.Matrix22 = 1f; //b
  755. cm.Matrix33 = FADE_LEVEL; //a
  756. cm.Matrix44 = 1f; //w
  757.  
  758. ia.SetColorMatrix(cm);
  759. g.DrawImage(img, bounds, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
  760. }
  761. }
  762.  
  763. /// <summary>Draw hover glow</summary>
  764. private void DrawGlow(Graphics g, Image img, Rectangle bounds)
  765. {
  766. bounds.Inflate(this.ImageGlowFactor, this.ImageGlowFactor);
  767. g.DrawImage(_bmpGlow, bounds);
  768. }
  769.  
  770. /// <summary>Draw an unaltered image</summary>
  771. private void DrawImage(Graphics g, Image img, Rectangle bounds)
  772. {
  773. g.DrawImage(img, bounds);
  774. }
  775.  
  776. /// <summary>Draw a gradient mask</summary>
  777. private void DrawMask(Graphics g, Rectangle bounds)
  778. {
  779. bounds.Inflate(-1, -1);
  780. // create an interior path
  781. using (GraphicsPath gp = CreateRoundRectanglePath(g, bounds, 4))
  782. {
  783. // fill the button with a subtle glow
  784. using (LinearGradientBrush fillBrush = new LinearGradientBrush(
  785. bounds,
  786. Color.FromArgb(160, Color.White),
  787. Color.FromArgb(5, Color.Silver),
  788. 75f))
  789. {
  790. Blend blnd = new Blend();
  791. blnd.Positions = new float[] { 0f, .1f, .2f, .3f, .4f, .5f, 1f };
  792. blnd.Factors = new float[] { 0f, .1f, .2f, .4f, .7f, .8f, 1f };
  793. fillBrush.Blend = blnd;
  794. g.FillPath(fillBrush, gp);
  795. }
  796. }
  797. }
  798.  
  799. /// <summary>Draw a mirror effect</summary>
  800. private void DrawMirror(Graphics g, Rectangle bounds)
  801. {
  802. // Rectangle imageRect = GetImageBounds(bounds, this.Image);
  803. bounds.Y = bounds.Bottom;
  804. bounds.Height = _bmpMirror.Height;
  805. bounds.Width = _bmpMirror.Width;
  806. using (ImageAttributes ia = new ImageAttributes())
  807. {
  808. ColorMatrix cm = new ColorMatrix();
  809. cm.Matrix00 = 1f; //r
  810. cm.Matrix11 = 1f; //g
  811. cm.Matrix22 = 1f; //b
  812. cm.Matrix33 = MIRROR_LEVEL; //a
  813. cm.Matrix44 = 1f; //w
  814.  
  815. ia.SetColorMatrix(cm);
  816. g.DrawImage(_bmpMirror, bounds, 0, 0, _bmpMirror.Width, _bmpMirror.Height, GraphicsUnit.Pixel, ia);
  817. }
  818. }
  819.  
  820. /// <summary>Get the image size and position</summary>
  821. private Rectangle GetImageBounds(Rectangle bounds, Image img)
  822. {
  823. int left = (int)((bounds.Width - img.Width) * .5f);
  824. int top = (int)((bounds.Height - img.Height) * .5f);
  825. if (this.ImageMirror)
  826. top = (int)((bounds.Height - (img.Height + (int)(img.Height * .7f))) * .5f);
  827. return new Rectangle(left, top, img.Width, img.Height);
  828. }
  829.  
  830. /// <summary>Convert rgb to float</summary>
  831. private float ParseColor(byte clr)
  832. {
  833. return clr == 0 ? 0 : ((float)clr / 255);
  834. }
  835.  
  836. private void ResetSize()
  837. {
  838. this.Width = this.Image.Width + this.ImageGlowFactor + SIZE_OFFSET;
  839. this.Height = this.Image.Height + this.ImageGlowFactor + SIZE_OFFSET;
  840. if (this.ImageMirror)
  841. this.Height += (int)(this.Image.Height * .7f);
  842. }
  843. #endregion
  844. }
  845. }
Advertisement
Add Comment
Please, Sign In to add comment