LockMakeCode.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Text;
  6. using System.Windows.Forms;
  7. using UAS_MES_NEW.Enum;
  8. using UAS_MES_NEW.Properties;
  9. using UAS_MES_NEW.PublicMethod;
  10. namespace UAS_MES_NEW.CustomControl.ButtonUtil
  11. {
  12. public partial class LockMakeCode : Button
  13. {
  14. #region 变量
  15. /// <summary>
  16. /// 鼠标状态
  17. /// </summary>
  18. private EMouseState _mouseState = EMouseState.Normal;
  19. /// <summary>
  20. /// 文本对齐方式
  21. /// </summary>
  22. private TextFormatFlags _textAlign = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
  23. /// <summary>
  24. /// 默认时的按钮图片
  25. /// </summary>
  26. private Image _normalImage = null;
  27. /// <summary>
  28. /// 鼠标按下时的图片
  29. /// </summary>
  30. private Image _downImage = null;
  31. /// <summary>
  32. /// 鼠标划过时的图片
  33. /// </summary>
  34. private Image _moveImage = null;
  35. /// <summary>
  36. /// 是否按下了鼠标
  37. /// </summary>
  38. private bool _isShowBorder = true;
  39. /// <summary>
  40. /// 权限标识符
  41. /// </summary>
  42. string Power1;
  43. public string Power
  44. {
  45. get
  46. {
  47. return Power1;
  48. }
  49. set
  50. {
  51. Power1 = value;
  52. }
  53. }
  54. string AllPower1;
  55. public string AllPower
  56. {
  57. get
  58. {
  59. return AllPower1;
  60. }
  61. set
  62. {
  63. AllPower1 = value;
  64. }
  65. }
  66. #endregion
  67. #region 构造函数
  68. public LockMakeCode()
  69. {
  70. InitializeComponent();
  71. this.SetStyle(
  72. ControlStyles.AllPaintingInWmPaint |
  73. ControlStyles.OptimizedDoubleBuffer |
  74. ControlStyles.ResizeRedraw |
  75. ControlStyles.Selectable |
  76. ControlStyles.DoubleBuffer |
  77. ControlStyles.SupportsTransparentBackColor |
  78. ControlStyles.UserPaint, true);
  79. this.SetStyle(ControlStyles.Opaque, false);
  80. base.BackColor = Color.Transparent;
  81. this.UpdateStyles();
  82. }
  83. #endregion
  84. #region 属性
  85. /// <summary>
  86. /// 默认大小
  87. /// </summary>
  88. protected override Size DefaultSize
  89. {
  90. get { return new Size(75, 28); }
  91. }
  92. /// <summary>
  93. /// 默认图片
  94. /// </summary>
  95. [Description("默认图片")]
  96. public virtual Image NormalImage
  97. {
  98. get
  99. {
  100. if (this._normalImage == null)
  101. this._normalImage = Resources.normal;
  102. return this._normalImage;
  103. }
  104. set
  105. {
  106. this._normalImage = value;
  107. base.Invalidate();
  108. }
  109. }
  110. /// <summary>
  111. /// 鼠标按下时的图片
  112. /// </summary>
  113. [Description("鼠标按下时的图片")]
  114. public virtual Image DownImage
  115. {
  116. get
  117. {
  118. if (this._downImage == null)
  119. this._downImage = Resources.down;
  120. return this._downImage;
  121. }
  122. set
  123. {
  124. this._downImage = value;
  125. base.Invalidate();
  126. }
  127. }
  128. /// <summary>
  129. /// 鼠标划过时的图片
  130. /// </summary>
  131. [Description("鼠标划过时的图片")]
  132. public virtual Image MoveImage
  133. {
  134. get
  135. {
  136. if (this._moveImage == null)
  137. this._moveImage = Resources.highlight;
  138. return this._moveImage;
  139. }
  140. set
  141. {
  142. this._moveImage = value;
  143. base.Invalidate();
  144. }
  145. }
  146. /// <summary>
  147. /// 是否显示发光边框
  148. /// </summary>
  149. [Description("是否显示发光边框")]
  150. public virtual bool IsShowBorder
  151. {
  152. get { return this._isShowBorder; }
  153. set { this._isShowBorder = value; }
  154. }
  155. /// <summary>
  156. /// 与控件相关的文本
  157. /// </summary>
  158. [DefaultValue("QQButton")]
  159. public override string Text
  160. {
  161. get { return base.Text; }
  162. set
  163. {
  164. base.Text = value;
  165. base.Invalidate(this.TextRect);
  166. }
  167. }
  168. /// <summary>
  169. /// 按钮上显示的图片
  170. /// </summary>
  171. [Description("按钮上显示的图片")]
  172. public virtual new Image Image
  173. {
  174. get { return base.Image; }
  175. set
  176. {
  177. base.Image = value;
  178. base.Invalidate();
  179. }
  180. }
  181. /// <summary>
  182. /// 按钮上文字的对齐方式
  183. /// </summary>
  184. [Description("按钮上文字的对齐方式")]
  185. new public ContentAlignment TextAlign
  186. {
  187. get { return base.TextAlign; }
  188. set
  189. {
  190. base.TextAlign = value;
  191. switch (base.TextAlign)
  192. {
  193. case ContentAlignment.BottomCenter:
  194. this._textAlign = TextFormatFlags.Bottom |
  195. TextFormatFlags.HorizontalCenter |
  196. TextFormatFlags.SingleLine;
  197. break;
  198. case ContentAlignment.BottomLeft:
  199. this._textAlign = TextFormatFlags.Bottom |
  200. TextFormatFlags.Left |
  201. TextFormatFlags.SingleLine;
  202. break;
  203. case ContentAlignment.BottomRight:
  204. this._textAlign = TextFormatFlags.Bottom |
  205. TextFormatFlags.Right |
  206. TextFormatFlags.SingleLine;
  207. break;
  208. case ContentAlignment.MiddleCenter:
  209. this._textAlign = TextFormatFlags.SingleLine |
  210. TextFormatFlags.HorizontalCenter |
  211. TextFormatFlags.VerticalCenter;
  212. break;
  213. case ContentAlignment.MiddleLeft:
  214. this._textAlign = TextFormatFlags.Left |
  215. TextFormatFlags.VerticalCenter |
  216. TextFormatFlags.SingleLine;
  217. break;
  218. case ContentAlignment.MiddleRight:
  219. this._textAlign = TextFormatFlags.Right |
  220. TextFormatFlags.VerticalCenter |
  221. TextFormatFlags.SingleLine;
  222. break;
  223. case ContentAlignment.TopCenter:
  224. this._textAlign = TextFormatFlags.Top |
  225. TextFormatFlags.HorizontalCenter |
  226. TextFormatFlags.SingleLine;
  227. break;
  228. case ContentAlignment.TopLeft:
  229. this._textAlign = TextFormatFlags.Top |
  230. TextFormatFlags.Left |
  231. TextFormatFlags.SingleLine;
  232. break;
  233. case ContentAlignment.TopRight:
  234. this._textAlign = TextFormatFlags.Top |
  235. TextFormatFlags.Right |
  236. TextFormatFlags.SingleLine;
  237. break;
  238. }
  239. base.Invalidate(this.TextRect);
  240. }
  241. }
  242. /// <summary>
  243. /// 整个按钮的区域
  244. /// </summary>
  245. internal Rectangle AllRect
  246. {
  247. get { return new Rectangle(0, 0, this.Width, this.Height); }
  248. }
  249. /// <summary>
  250. /// 文字区域
  251. /// </summary>
  252. internal Rectangle TextRect
  253. {
  254. get { return new Rectangle(2, 2, this.AllRect.Width - 4, this.AllRect.Height - 4); }
  255. }
  256. /// <summary>
  257. /// 鼠标状态
  258. /// </summary>
  259. internal EMouseState MouseState
  260. {
  261. get { return this._mouseState; }
  262. set
  263. {
  264. this._mouseState = value;
  265. base.Invalidate();
  266. }
  267. }
  268. #endregion
  269. #region Override 方法
  270. /// <summary>
  271. /// 引发 System.Windows.Forms.Form.Paint 事件。
  272. /// </summary>
  273. /// <param name="pevent">包含事件数据的 System.Windows.Forms.PaintEventArgs。</param>
  274. protected override void OnPaint(PaintEventArgs pevent)
  275. {
  276. Graphics g = pevent.Graphics;
  277. g.SmoothingMode = SmoothingMode.AntiAlias;
  278. g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  279. //g.DrawImage(AssemblyHelper.GetAssemblyImage("Button.all_btn_White-side.png"), this.AllRect);
  280. if (base.Enabled)
  281. {
  282. if (this.IsShowBorder)
  283. {
  284. if (this.Focused)//得到焦点的时候,绘制边框
  285. {
  286. using (Image light = Resources.Light)
  287. {
  288. g.DrawImage(light, this.AllRect);
  289. //DrawHelper.RendererBackground(g, this.AllRect, light, true);
  290. }
  291. }
  292. }
  293. switch (this.MouseState)
  294. {
  295. case EMouseState.Leave:
  296. case EMouseState.Normal:
  297. if (base.Focused)
  298. {
  299. if (this.IsShowBorder)
  300. {
  301. using (Image focus = Resources.focus)
  302. {
  303. DrawHelper.RendererBackground(g, this.TextRect, focus, true);
  304. }
  305. }
  306. else
  307. {
  308. DrawHelper.RendererBackground(g, this.TextRect, this.NormalImage, true);
  309. }
  310. }
  311. else
  312. {
  313. DrawHelper.RendererBackground(g, this.TextRect, this.NormalImage, true);
  314. }
  315. break;
  316. case EMouseState.Up:
  317. case EMouseState.Move:
  318. DrawHelper.RendererBackground(g, this.TextRect, this.MoveImage, true);
  319. break;
  320. case EMouseState.Down:
  321. DrawHelper.RendererBackground(g, this.TextRect, this.DownImage, true);
  322. break;
  323. }
  324. //if (this.Image != null)
  325. //{
  326. //int y = (this.Height - this.Image.Height) / 2;
  327. //int x = 10;
  328. //if (!string.IsNullOrEmpty(this.Text))
  329. // x = (this.AllRect.Width - (TextHelper.GetStringLen(this.Text) * 6 + this.Image.Width)) / 2;
  330. //else
  331. // x = (this.Width - this.Image.Width) / 2;//将图片绘制在按钮居中位置
  332. //Rectangle rect = new Rectangle(x, y, this.Image.Width, this.Image.Height);
  333. //Rectangle imgRect = new Rectangle(0, 0, this.Image.Width, this.Image.Height);
  334. ////绘制图片
  335. //g.DrawImage(this.Image, rect, imgRect, GraphicsUnit.Pixel);
  336. ////绘制文字
  337. //Rectangle textRect = new Rectangle(this.TextRect.X + this.Image.Width, this.TextRect.Y, this.TextRect.Width, this.TextRect.Height);
  338. //TextRenderer.DrawText(g, this.Text, this.Font, textRect, this.ForeColor, _textAlign);
  339. //}
  340. //else
  341. //{
  342. //绘制按钮上的文字
  343. TextRenderer.DrawText(g, this.Text, this.Font, this.TextRect, this.ForeColor, this._textAlign);
  344. //}
  345. }
  346. else
  347. {
  348. using (Image gray = Resources.gray)
  349. {
  350. DrawHelper.RendererBackground(g, this.TextRect, gray, true);
  351. }
  352. TextRenderer.DrawText(g, this.Text, this.Font, this.TextRect, Color.Gray, this._textAlign);
  353. }
  354. }
  355. /// <summary>
  356. /// 引发 System.Windows.Forms.Form.MouseEnter 事件。
  357. /// </summary>
  358. /// <param name="e">包含事件数据的 System.EventArgs。</param>
  359. protected override void OnMouseEnter(EventArgs e)
  360. {
  361. base.OnMouseEnter(e);
  362. this.MouseState = EMouseState.Move;
  363. }
  364. /// <summary>
  365. /// 引发 System.Windows.Forms.Form.MouseLeave 事件。
  366. /// </summary>
  367. /// <param name="e">包含事件数据的 System.EventArgs。</param>
  368. protected override void OnMouseLeave(EventArgs e)
  369. {
  370. base.OnMouseLeave(e);
  371. this.MouseState = EMouseState.Leave;
  372. }
  373. /// <summary>
  374. /// 引发 System.Windows.Forms.Form.MouseDown 事件。
  375. /// </summary>
  376. /// <param name="mevent">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
  377. protected override void OnMouseDown(MouseEventArgs mevent)
  378. {
  379. base.OnMouseDown(mevent);
  380. this.MouseState = EMouseState.Down;
  381. }
  382. /// <summary>
  383. /// 引发 System.Windows.Forms.Form.MouseUp 事件。
  384. /// </summary>
  385. /// <param name="mevent">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param>
  386. protected override void OnMouseUp(MouseEventArgs mevent)
  387. {
  388. base.OnMouseUp(mevent);
  389. this.MouseState = EMouseState.Up;
  390. }
  391. #endregion
  392. }
  393. }