BorderLabel.cs
上传用户:nnpulika
上传日期:2013-02-15
资源大小:597k
文件大小:2k
源码类别:

状态条

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Drawing.Drawing2D;
  5. using System.ComponentModel;
  6. namespace UtilityLibrary.WinControls
  7. {
  8. /// <summary>
  9. /// Summary description for BorderLabel.
  10. /// </summary>
  11. [ToolboxItem(false)]
  12. public class BorderLabel : Label
  13. {
  14. #region Class Variables
  15. Pen pen = null;
  16. Pen hoverPen = null;
  17. bool highlight = false;
  18. int width = -1;
  19. int gap = 0;
  20. #endregion
  21. #region Constructors
  22. public BorderLabel(Color BorderColor, Color HoverColor): this(BorderColor, HoverColor, 1)
  23. {
  24. }
  25. public BorderLabel(Color BorderColor, Color HoverColor, int Width)
  26. {
  27. highlight = true;
  28. width = Width;
  29. pen = new Pen(BorderColor, Width);
  30. hoverPen = new Pen(HoverColor, Width);
  31. pen.Alignment = PenAlignment.Inset;
  32. hoverPen.Alignment = PenAlignment.Inset;
  33. if ( Width == 1 )
  34. gap = 1;
  35. }
  36. public BorderLabel(Color BorderColor): this(BorderColor, 1)
  37. {
  38. }
  39. public BorderLabel(Color BorderColor, int Width)
  40. {
  41. highlight = false;
  42. width = Width;
  43. pen = new Pen(BorderColor, Width);
  44. pen.Alignment = PenAlignment.Inset;
  45. if ( Width == 1 )
  46. gap = 1;
  47. }
  48. #endregion
  49. #region
  50. override protected void OnPaint(PaintEventArgs e)
  51. {
  52. base.OnPaint(e);
  53. // Draw a border around the label
  54. Rectangle rc = Bounds;
  55. e.Graphics.DrawRectangle(pen, 0, 0, rc.Width-gap, rc.Height-gap);
  56. }
  57. override protected void OnMouseMove(MouseEventArgs e)
  58. {
  59. base.OnMouseMove(e);
  60. if ( highlight ) 
  61. {
  62. Graphics g = CreateGraphics();
  63. Rectangle rc = Bounds;
  64. g.DrawRectangle(hoverPen, 0, 0, rc.Width-gap, rc.Height-gap);
  65. g.Dispose();
  66. }
  67. }
  68. override protected void OnMouseLeave(EventArgs e)
  69. {
  70. base.OnMouseLeave(e);
  71. if ( highlight ) 
  72. {
  73. Graphics g = CreateGraphics();
  74. Rectangle rc = Bounds;
  75. g.DrawRectangle(pen, 0, 0, rc.Width-gap, rc.Height-gap);
  76. g.Dispose();
  77. }
  78. }
  79. #endregion
  80. }
  81. }