Chocolade19721309

Untitled

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