Chocolade19721309

Untitled

May 6th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.51 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. /// <summary>Get/Set Checkbox state</summary>
  240. [Browsable(true), Category("Behavior"),
  241. Description("Get/Set Checkbox state")]
  242. public bool Checked
  243. {
  244. get { return _bCheckState; }
  245. set
  246. {
  247. _bCheckState = value;
  248. if (!_bCheckState)
  249. _eButtonState = ButtonState.Normal;
  250. DrawButton();
  251. }
  252. }
  253.  
  254. /// <summary>Get/Set the checked border Color</summary>
  255. [Browsable(true), Category("Appearance"),
  256. Description("Get/Set the checked border Color")]
  257. public Color CheckedBorderColor
  258. {
  259. get { return _clrCheckedBorderColor; }
  260. set { _clrCheckedBorderColor = value; }
  261. }
  262.  
  263. /// <summary>Get/Set Checkbox style</summary>
  264. [Browsable(true), Category("Appearance"),
  265. Description("Get/Set Checkbox style")]
  266. public CheckedStyle CheckStyle
  267. {
  268. get { return _eUseCheckStyle; }
  269. set { _eUseCheckStyle = value; }
  270. }
  271.  
  272. /// <summary>Get/Set Focus on button hover</summary>
  273. [Browsable(true), Category("Behavior"),
  274. Description("Get/Set Focus on button hover")]
  275. public bool FocusOnHover
  276. {
  277. get { return _bFocusOnHover; }
  278. set { _bFocusOnHover = value; }
  279. }
  280.  
  281. /// <summary>Get/Set Draw a Focused mask</summary>
  282. [Browsable(true), Category("Appearance"),
  283. Description("Get/Set Draw a Focused mask")]
  284. public bool FocusedMask
  285. {
  286. get { return _bFocusedMask; }
  287. set { _bFocusedMask = value; }
  288. }
  289.  
  290. /// <summary>Get/Set button image</summary>
  291. [Browsable(true), Category("Appearance"), RefreshProperties(RefreshProperties.All),
  292. Description("Get/Set button Image")]
  293. public Image Image
  294. {
  295. get { return _imgImage; }
  296. set
  297. {
  298. _imgImage = value;
  299. if (_imgImage != null)
  300. {
  301. if (this.AutoSize && this.DesignMode)
  302. ResetSize();
  303. if (this.ImageMirror)
  304. CreateMirror();
  305. CreateGlow();
  306. DrawButton();
  307. }
  308. }
  309. }
  310.  
  311. /// <summary>Get/Set the image checked Color</summary>
  312. [Browsable(true), Category("Appearance"),
  313. Description("Get/Set the image checked Color")]
  314. public Color ImageCheckedColor
  315. {
  316. get { return _clrImageCheckedColor; }
  317. set { _clrImageCheckedColor = value; }
  318. }
  319.  
  320. /// <summary>Get/Set the image disabled Color</summary>
  321. [Browsable(true), Category("Appearance"),
  322. Description("Get/Set the image disabled Color")]
  323. public Color ImageDisabledColor
  324. {
  325. get { return _clrImageDisabledColor; }
  326. set { _clrImageDisabledColor = value; }
  327. }
  328.  
  329. /// <summary>Get/Set the image focused Color</summary>
  330. [Browsable(true), Category("Appearance"),
  331. Description("Get/Set the image focused Color")]
  332. public Color ImageFocusedColor
  333. {
  334. get { return _clrImageFocusedColor; }
  335. set { _clrImageFocusedColor = value; }
  336. }
  337.  
  338. /// <summary>Get/Set the glow Color</summary>
  339. [Browsable(true), Category("Appearance"), RefreshProperties(RefreshProperties.All),
  340. Description("Get/Set the glow Color")]
  341. public Color ImageGlowColor
  342. {
  343. get { return _clrImageGlowColor; }
  344. set
  345. {
  346. _clrImageGlowColor = value;
  347. CreateGlow();
  348. }
  349. }
  350.  
  351. /// <summary>Get/Set the hover Color</summary>
  352. [Browsable(true), Category("Appearance"),
  353. Description("Get/Set the hover Color")]
  354. public Color ImageHoverColor
  355. {
  356. get { return _clrImageHoverColor; }
  357. set { _clrImageHoverColor = value; }
  358. }
  359.  
  360. /// <summary>Get/Set the pressed Color</summary>
  361. [Browsable(true), Category("Appearance"),
  362. Description("Get/Set the pressed Color")]
  363. public Color ImagePressedColor
  364. {
  365. get { return _clrImagePressedColor; }
  366. set { _clrImagePressedColor = value; }
  367. }
  368.  
  369. /// <summary>Get/Set Glow factor Depth</summary>
  370. [Browsable(true), Category("Appearance"), RefreshProperties(RefreshProperties.All),
  371. Description("Get/Set Glow factor Depth")]
  372. public int ImageGlowFactor
  373. {
  374. get { return _iGlowFactor; }
  375. set
  376. {
  377. _iGlowFactor = value;
  378. CreateGlow();
  379. }
  380. }
  381.  
  382. /// <summary>Get/Set Image Mirror effect</summary>
  383. [Browsable(true), Category("Behavior"), RefreshProperties(RefreshProperties.All),
  384. Description("Get/Set Image Mirror effect")]
  385. public bool ImageMirror
  386. {
  387. get { return _bImageMirror; }
  388. set
  389. {
  390. _bImageMirror = value;
  391. if (_bImageMirror)
  392. CreateMirror();
  393. if (this.Image != null && this.AutoSize && this.DesignMode)
  394. ResetSize();
  395. DrawButton();
  396. }
  397. }
  398.  
  399. // New property to control the hover/click effect
  400. private bool _applyDarkEffect = true;
  401. public bool ApplyDarkEffect
  402. {
  403. get { return _applyDarkEffect; }
  404. set
  405. {
  406. _applyDarkEffect = value;
  407. Invalidate(); // Redraw to reflect changes immediately
  408. }
  409. }
  410.  
  411. private string _buttonText = "Button"; // Default text
  412. public string ButtonText
  413. {
  414. get { return _buttonText; }
  415. set
  416. {
  417. _buttonText = value;
  418. Invalidate(); // Redraw the button when text changes
  419. }
  420. }
  421. #endregion
  422. #endregion
  423.  
  424. #region Overrides
  425. protected override void OnEnabledChanged(EventArgs e)
  426. {
  427. if (this.Enabled == false)
  428. _eButtonState = ButtonState.Disabled;
  429. else
  430. _eButtonState = ButtonState.Normal;
  431. DrawButton();
  432. base.OnEnabledChanged(e);
  433. }
  434.  
  435. protected override void OnGotFocus(EventArgs e)
  436. {
  437. _eButtonState = ButtonState.Focused;
  438. if (_bClicked)
  439. _bClicked = false;
  440. else
  441. DrawButton();
  442. base.OnGotFocus(e);
  443. }
  444.  
  445. protected override void OnHandleCreated(EventArgs e)
  446. {
  447. Create();
  448. base.OnHandleCreated(e);
  449. }
  450.  
  451. protected override void OnHandleDestroyed(EventArgs e)
  452. {
  453. Destroy();
  454. base.OnHandleDestroyed(e);
  455. }
  456.  
  457. protected override void OnLostFocus(EventArgs e)
  458. {
  459. _eButtonState = ButtonState.Normal;
  460. DrawButton();
  461. base.OnLostFocus(e);
  462. }
  463.  
  464. protected override void OnMouseClick(MouseEventArgs e)
  465. {
  466. if (e.Button == MouseButtons.Right && SystemInformation.MouseButtonsSwapped || e.Button == MouseButtons.Left)
  467. this.Checked = !this.Checked;
  468. DrawButton();
  469. base.OnMouseClick(e);
  470. }
  471.  
  472. protected override void OnMouseDown(MouseEventArgs e)
  473. {
  474. if (e.Button == MouseButtons.Right && SystemInformation.MouseButtonsSwapped || e.Button == MouseButtons.Left)
  475. {
  476. _eButtonState = ApplyDarkEffect ? ButtonState.Pressed : ButtonState.Focused;
  477. DrawButton();
  478. _bClicked = true;
  479. }
  480. base.OnMouseDown(e);
  481. }
  482.  
  483. protected override void OnMouseEnter(EventArgs e)
  484. {
  485. if (ApplyDarkEffect)
  486. {
  487. _eButtonState = ButtonState.Normal; // Make it glow when the mouse enters
  488. }
  489. base.OnMouseEnter(e);
  490. }
  491.  
  492. protected override void OnMouseLeave(EventArgs e)
  493. {
  494. _eButtonState = ButtonState.Normal; // Return to normal state when the mouse leaves
  495. base.OnMouseLeave(e);
  496. }
  497.  
  498. protected override void OnMouseUp(MouseEventArgs e)
  499. {
  500. _eButtonState = ButtonState.Normal;
  501. DrawButton();
  502. base.OnMouseUp(e);
  503. }
  504.  
  505. protected override void OnPaint(PaintEventArgs e)
  506. {
  507. DrawButton();
  508. DrawButtonText(e.Graphics);
  509. base.OnPaint(e);
  510. }
  511.  
  512. private void DrawButtonText(Graphics graphics)
  513. {
  514. using (Font font = new Font("Arial", 12, FontStyle.Bold))
  515. {
  516. SizeF textSize = graphics.MeasureString(_buttonText, font);
  517. PointF locationToDraw = new PointF((this.Width / 2) - (textSize.Width / 2), (this.Height / 2) - (textSize.Height / 2));
  518.  
  519. using (SolidBrush brush = new SolidBrush(Color.White)) // Ensure text color contrasts well with button
  520. {
  521. graphics.DrawString(_buttonText, font, brush, locationToDraw);
  522. }
  523. }
  524. }
  525.  
  526. protected override void OnResize(EventArgs e)
  527. {
  528. Create();
  529. base.OnResize(e);
  530. }
  531. #endregion
  532.  
  533. #region Methods
  534. /// <summary>Create the Glow image</summary>
  535. private void CreateGlow()
  536. {
  537. if (_bmpGlow != null)
  538. _bmpGlow.Dispose();
  539. Rectangle imageRect = new Rectangle(0, 0, this.Image.Width + this.ImageGlowFactor, this.Image.Height + this.ImageGlowFactor);
  540. _bmpGlow = new Bitmap(imageRect.Width, imageRect.Height);
  541. using (Graphics g = Graphics.FromImage(_bmpGlow))
  542. g.DrawImage(this.Image, imageRect);
  543. int i = 0;
  544. for (int x = 0; x < imageRect.Height; x++)
  545. {
  546. for (i = 0; i < imageRect.Width; i++)
  547. {
  548. if (_bmpGlow.GetPixel(i, x).A > GLOW_THRESHHOLD)
  549. _bmpGlow.SetPixel(i, x, Color.FromArgb(GLOW_LEVEL, this.ImageGlowColor));
  550. }
  551. i = 0;
  552. }
  553. }
  554.  
  555. /// <summary>Create the Mirror image</summary>
  556. private void CreateMirror()
  557. {
  558. if (_bmpMirror != null)
  559. _bmpMirror.Dispose();
  560.  
  561. int height = (int)(this.Image.Height * .7f);
  562. int width = (int)(this.Image.Width * 1f);
  563. Rectangle imageRect = new Rectangle(0, 0, width, height);
  564. _bmpMirror = new Bitmap(imageRect.Width, imageRect.Height);
  565.  
  566. using (Graphics g = Graphics.FromImage(_bmpMirror))
  567. g.DrawImage(this.Image, imageRect);
  568. _bmpMirror.RotateFlip(RotateFlipType.Rotate180FlipX);
  569. }
  570.  
  571. /// <summary>Create a rounded rectangle GraphicsPath</summary>
  572. private GraphicsPath CreateRoundRectanglePath(Graphics g, Rectangle bounds, float radius)
  573. {
  574. // create a path
  575. GraphicsPath pathBounds = new GraphicsPath();
  576. // arc top left
  577. pathBounds.AddArc(bounds.Left, bounds.Top, radius, radius, 180, 90);
  578. // line top
  579. pathBounds.AddLine(bounds.Left + radius, bounds.Top, bounds.Right - radius, bounds.Top);
  580. // arc top right
  581. pathBounds.AddArc(bounds.Right - radius, bounds.Top, radius, radius, 270, 90);
  582. // line right
  583. pathBounds.AddLine(bounds.Right, bounds.Top + radius, bounds.Right, bounds.Bottom - radius);
  584. // arc bottom right
  585. pathBounds.AddArc(bounds.Right - radius, bounds.Bottom - radius, radius, radius, 0, 90);
  586. // line bottom
  587. pathBounds.AddLine(bounds.Right - radius, bounds.Bottom, bounds.Left + radius, bounds.Bottom);
  588. // arc bottom left
  589. pathBounds.AddArc(bounds.Left, bounds.Bottom - radius, radius, radius, 90, 90);
  590. // line left
  591. pathBounds.AddLine(bounds.Left, bounds.Bottom - radius, bounds.Left, bounds.Top + radius);
  592. pathBounds.CloseFigure();
  593. return pathBounds;
  594. }
  595.  
  596. /// <summary>Backfill the buffer</summary>
  597. private void DrawBackGround(Graphics g, Rectangle bounds)
  598. {
  599. if (this.BackgroundImage != null)
  600. {
  601. using (ImageAttributes ia = new ImageAttributes())
  602. g.DrawImage(this.BackgroundImage, bounds, 0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height, GraphicsUnit.Pixel, ia);
  603. }
  604. else
  605. {
  606. using (Brush br = new SolidBrush(this.BackColor))
  607. g.FillRectangle(br, bounds);
  608. }
  609. }
  610.  
  611. /// <summary>Draw border on checked style</summary>
  612. private void DrawBorder(Graphics g, Rectangle bounds, Color clr)
  613. {
  614. bounds.Inflate(-2, -2);
  615. using (GraphicsPath borderPath = CreateRoundRectanglePath(g, bounds, 4))
  616. {
  617. // top-left bottom-right -dark
  618. using (LinearGradientBrush borderBrush = new LinearGradientBrush(
  619. bounds,
  620. Color.FromArgb(140, clr),
  621. Color.FromArgb(140, Color.White),
  622. LinearGradientMode.BackwardDiagonal))
  623. {
  624. Blend blnd = new Blend();
  625. blnd.Positions = new float[] { 0f, .5f, 1f };
  626. blnd.Factors = new float[] { 1f, 0f, 1f };
  627. borderBrush.Blend = blnd;
  628. using (Pen borderPen = new Pen(borderBrush, 2f))
  629. g.DrawPath(borderPen, borderPath);
  630. }
  631. }
  632. }
  633.  
  634. /// <summary>Drawing hub</summary>
  635. private void DrawButton()
  636. {
  637. if (this.Image == null)
  638. return;
  639.  
  640. Rectangle bounds = new Rectangle(0, 0, this.Width, this.Height);
  641. Rectangle imageBounds = GetImageBounds(bounds, this.Image);
  642.  
  643. // draw into a buffer
  644. using (Graphics g = Graphics.FromImage(_bmpBuffer))
  645. {
  646. g.SmoothingMode = SmoothingMode.HighQuality;
  647. DrawBackGround(g, bounds);
  648. if (this.ImageMirror)
  649. DrawMirror(g, imageBounds);
  650.  
  651. if (this.CheckStyle != CheckedStyle.None && this.Checked == true)
  652. _eButtonState = ButtonState.Checked;
  653. else if (this.CheckStyle != CheckedStyle.None && this.Checked == false && _eButtonState == ButtonState.Focused)
  654. _eButtonState = ButtonState.Normal;
  655. switch (_eButtonState)
  656. {
  657. case ButtonState.Checked:
  658. {
  659. if (this.CheckStyle == CheckedStyle.Border)
  660. {
  661. DrawBorder(g, bounds, this.CheckedBorderColor);
  662. DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
  663. }
  664. else if (this.CheckStyle == CheckedStyle.ColorChange)
  665. {
  666. DrawColoredImage(g, this.Image, imageBounds, this.ImageCheckedColor);
  667. }
  668. else
  669. {
  670. DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
  671. }
  672. break;
  673. }
  674. case ButtonState.Disabled:
  675. {
  676. if (this.ImageDisabledColor == Color.Transparent)
  677. DrawFadedImage(g, this.Image, imageBounds);
  678. else
  679. DrawColoredImage(g, this.Image, imageBounds, this.ImageDisabledColor);
  680. break;
  681. }
  682. case ButtonState.Focused:
  683. {
  684. DrawColoredImage(g, this.Image, imageBounds, this.ImageFocusedColor);
  685. if (this.FocusedMask)
  686. {
  687. DrawMask(g, bounds);
  688. DrawBorder(g, bounds, Color.DarkGray);
  689. }
  690. break;
  691. }
  692. case ButtonState.Hover:
  693. {
  694. DrawGlow(g, this.Image, imageBounds);
  695. DrawColoredImage(g, this.Image, imageBounds, this.ImageHoverColor);
  696. break;
  697. }
  698. case ButtonState.Normal:
  699. {
  700. DrawImage(g, this.Image, imageBounds);
  701. break;
  702. }
  703. case ButtonState.Pressed:
  704. {
  705. DrawColoredImage(g, this.Image, imageBounds, this.ImagePressedColor);
  706. break;
  707. }
  708. }
  709. }
  710. // draw the buffer
  711. using (Graphics g = Graphics.FromHwnd(this.Handle))
  712. DrawImage(g, _bmpBuffer, bounds);
  713. }
  714.  
  715. /// <summary>Draw a colored bitmap</summary>
  716. private void DrawColoredImage(Graphics g, Image img, Rectangle bounds, Color clr)
  717. {
  718. using (ImageAttributes ia = new ImageAttributes())
  719. {
  720. ColorMatrix cm = new ColorMatrix();
  721. // convert and refactor color palette
  722. cm.Matrix00 = ParseColor(clr.R);
  723. cm.Matrix11 = ParseColor(clr.G);
  724. cm.Matrix22 = ParseColor(clr.B);
  725. cm.Matrix33 = ParseColor(clr.A);
  726. cm.Matrix44 = 1f;
  727. // set matrix
  728. ia.SetColorMatrix(cm);
  729. // draw
  730. g.DrawImage(img, bounds, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
  731. }
  732. }
  733.  
  734. /// <summary>draw a faded bitmap</summary>
  735. private void DrawFadedImage(Graphics g, Image img, Rectangle bounds)
  736. {
  737. using (ImageAttributes ia = new ImageAttributes())
  738. {
  739. ColorMatrix cm = new ColorMatrix();
  740. cm.Matrix00 = 1f; //r
  741. cm.Matrix11 = 1f; //g
  742. cm.Matrix22 = 1f; //b
  743. cm.Matrix33 = FADE_LEVEL; //a
  744. cm.Matrix44 = 1f; //w
  745.  
  746. ia.SetColorMatrix(cm);
  747. g.DrawImage(img, bounds, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
  748. }
  749. }
  750.  
  751. /// <summary>Draw hover glow</summary>
  752. private void DrawGlow(Graphics g, Image img, Rectangle bounds)
  753. {
  754. bounds.Inflate(this.ImageGlowFactor, this.ImageGlowFactor);
  755. g.DrawImage(_bmpGlow, bounds);
  756. }
  757.  
  758. /// <summary>Draw an unaltered image</summary>
  759. private void DrawImage(Graphics g, Image img, Rectangle bounds)
  760. {
  761. g.DrawImage(img, bounds);
  762. }
  763.  
  764. /// <summary>Draw a gradient mask</summary>
  765. private void DrawMask(Graphics g, Rectangle bounds)
  766. {
  767. bounds.Inflate(-1, -1);
  768. // create an interior path
  769. using (GraphicsPath gp = CreateRoundRectanglePath(g, bounds, 4))
  770. {
  771. // fill the button with a subtle glow
  772. using (LinearGradientBrush fillBrush = new LinearGradientBrush(
  773. bounds,
  774. Color.FromArgb(160, Color.White),
  775. Color.FromArgb(5, Color.Silver),
  776. 75f))
  777. {
  778. Blend blnd = new Blend();
  779. blnd.Positions = new float[] { 0f, .1f, .2f, .3f, .4f, .5f, 1f };
  780. blnd.Factors = new float[] { 0f, .1f, .2f, .4f, .7f, .8f, 1f };
  781. fillBrush.Blend = blnd;
  782. g.FillPath(fillBrush, gp);
  783. }
  784. }
  785. }
  786.  
  787. /// <summary>Draw a mirror effect</summary>
  788. private void DrawMirror(Graphics g, Rectangle bounds)
  789. {
  790. // Rectangle imageRect = GetImageBounds(bounds, this.Image);
  791. bounds.Y = bounds.Bottom;
  792. bounds.Height = _bmpMirror.Height;
  793. bounds.Width = _bmpMirror.Width;
  794. using (ImageAttributes ia = new ImageAttributes())
  795. {
  796. ColorMatrix cm = new ColorMatrix();
  797. cm.Matrix00 = 1f; //r
  798. cm.Matrix11 = 1f; //g
  799. cm.Matrix22 = 1f; //b
  800. cm.Matrix33 = MIRROR_LEVEL; //a
  801. cm.Matrix44 = 1f; //w
  802.  
  803. ia.SetColorMatrix(cm);
  804. g.DrawImage(_bmpMirror, bounds, 0, 0, _bmpMirror.Width, _bmpMirror.Height, GraphicsUnit.Pixel, ia);
  805. }
  806. }
  807.  
  808. /// <summary>Get the image size and position</summary>
  809. private Rectangle GetImageBounds(Rectangle bounds, Image img)
  810. {
  811. int left = (int)((bounds.Width - img.Width) * .5f);
  812. int top = (int)((bounds.Height - img.Height) * .5f);
  813. if (this.ImageMirror)
  814. top = (int)((bounds.Height - (img.Height + (int)(img.Height * .7f))) * .5f);
  815. return new Rectangle(left, top, img.Width, img.Height);
  816. }
  817.  
  818. /// <summary>Convert rgb to float</summary>
  819. private float ParseColor(byte clr)
  820. {
  821. return clr == 0 ? 0 : ((float)clr / 255);
  822. }
  823.  
  824. private void ResetSize()
  825. {
  826. this.Width = this.Image.Width + this.ImageGlowFactor + SIZE_OFFSET;
  827. this.Height = this.Image.Height + this.ImageGlowFactor + SIZE_OFFSET;
  828. if (this.ImageMirror)
  829. this.Height += (int)(this.Image.Height * .7f);
  830. }
  831. #endregion
  832. }
  833. }
Advertisement
Add Comment
Please, Sign In to add comment