PanelEx.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.ComponentModel;
  9. using System.Runtime.InteropServices;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. public class PanelEx : System.Windows.Forms.Panel
  13. {
  14. [DllImport("user32.dll")]
  15. private static extern IntPtr GetWindowDC(IntPtr hwnd);
  16. [DllImport("user32.dll")]
  17. private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
  18. private Color _borderColor = LYFZ.ComponentLibrary.GetUIResources.BorderAreaColor;
  19. private int _borderWidth = 1;
  20. //
  21. // 摘要:
  22. // 获取或设置控件的边框颜色。
  23. //
  24. // 返回结果:
  25. // 控件的边框颜色 System.Drawing.Color。默认为 System.Drawing.Color.Black
  26. // 属性的值。
  27. [Description("组件的边框颜色。"), Category("Appearance")]
  28. public Color BorderColor
  29. {
  30. get
  31. {
  32. return _borderColor;
  33. }
  34. set
  35. {
  36. _borderColor = value;
  37. this.Invalidate();
  38. }
  39. }
  40. //
  41. // 摘要:
  42. // 获取或设置控件的边框宽度。
  43. //
  44. // 返回结果:
  45. // 控件的边框宽度 int。默认为 1
  46. // 属性的值。
  47. [Description("组件的边框宽度。"), Category("Appearance")]
  48. public int BorderWidth
  49. {
  50. get
  51. {
  52. return _borderWidth;
  53. }
  54. set
  55. {
  56. _borderWidth = value;
  57. this.Invalidate();
  58. }
  59. }
  60. public PanelEx()
  61. {
  62. SetStyle(ControlStyles.DoubleBuffer, true);
  63. SetStyle(ControlStyles.AllPaintingInWmPaint, false);
  64. SetStyle(ControlStyles.ResizeRedraw, true);
  65. SetStyle(ControlStyles.UserPaint, true);
  66. SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  67. this.Paint += new PaintEventHandler(PanelEx_Paint);
  68. this.DoubleBuffered = true;
  69. }
  70. private PlBorderType _BorderType = PlBorderType.all;
  71. public PlBorderType BorderType
  72. {
  73. get { return _BorderType; }
  74. set { _BorderType = value; }
  75. }
  76. private void PanelEx_Paint(object sender, PaintEventArgs e)
  77. {
  78. if (this.BorderStyle == BorderStyle.FixedSingle)
  79. {
  80. IntPtr hDC = GetWindowDC(this.Handle);
  81. Graphics g = Graphics.FromHdc(hDC);
  82. switch (this.BorderType)
  83. {
  84. case PlBorderType.all:
  85. ControlPaint.DrawBorder(
  86. g,
  87. new Rectangle(0, 0, this.Width, this.Height),
  88. _borderColor,
  89. _borderWidth,
  90. ButtonBorderStyle.Solid,
  91. _borderColor,
  92. _borderWidth,
  93. ButtonBorderStyle.Solid,
  94. _borderColor,
  95. _borderWidth,
  96. ButtonBorderStyle.Solid,
  97. _borderColor,
  98. _borderWidth,
  99. ButtonBorderStyle.Solid);
  100. break;
  101. case PlBorderType.top:
  102. ControlPaint.DrawBorder(
  103. g,
  104. new Rectangle(0, 0, this.Width, this.Height),
  105. BackColor,
  106. _borderWidth,
  107. ButtonBorderStyle.Solid,
  108. _borderColor,
  109. _borderWidth,
  110. ButtonBorderStyle.Solid,
  111. BackColor,
  112. _borderWidth,
  113. ButtonBorderStyle.Solid,
  114. BackColor,
  115. _borderWidth,
  116. ButtonBorderStyle.Solid);
  117. break;
  118. case PlBorderType.bottom:
  119. ControlPaint.DrawBorder(
  120. g,
  121. new Rectangle(0, 0, this.Width, this.Height),
  122. BackColor,
  123. _borderWidth,
  124. ButtonBorderStyle.Solid,
  125. BackColor,
  126. _borderWidth,
  127. ButtonBorderStyle.Solid,
  128. BackColor,
  129. _borderWidth,
  130. ButtonBorderStyle.Solid,
  131. _borderColor,
  132. _borderWidth,
  133. ButtonBorderStyle.Solid);
  134. break;
  135. case PlBorderType.left:
  136. ControlPaint.DrawBorder(
  137. g,
  138. new Rectangle(0, 0, this.Width, this.Height),
  139. _borderColor,
  140. _borderWidth,
  141. ButtonBorderStyle.Solid,
  142. BackColor,
  143. _borderWidth,
  144. ButtonBorderStyle.Solid,
  145. BackColor,
  146. _borderWidth,
  147. ButtonBorderStyle.Solid,
  148. BackColor,
  149. _borderWidth,
  150. ButtonBorderStyle.Solid);
  151. break;
  152. case PlBorderType.right:
  153. ControlPaint.DrawBorder(
  154. g,
  155. new Rectangle(0, 0, this.Width, this.Height),
  156. BackColor,
  157. _borderWidth,
  158. ButtonBorderStyle.Solid,
  159. BackColor,
  160. _borderWidth,
  161. ButtonBorderStyle.Solid,
  162. _borderColor,
  163. _borderWidth,
  164. ButtonBorderStyle.Solid,
  165. BackColor,
  166. _borderWidth,
  167. ButtonBorderStyle.Solid);
  168. break;
  169. }
  170. g.Dispose();
  171. ReleaseDC(Handle, hDC);
  172. }
  173. }
  174. }
  175. public enum PlBorderType
  176. {
  177. all,
  178. top,
  179. bottom,
  180. left,
  181. right
  182. }
  183. }