NoTitleForm.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. using Y.Utils.DrawUtils.ColorUtils;
  7. using Y.Utils.WindowsUtils.APIUtils;
  8. namespace Y.Skin.YoForm.NoTitle
  9. {
  10. public partial class NoTitleForm : Form
  11. {
  12. #region 属性
  13. //窗体边框粗细
  14. private int _Border = 1;
  15. [Category("Style")]
  16. [Description("窗体边框粗细")]
  17. [DefaultValue(typeof(int), "1")]
  18. public int Border
  19. {
  20. get { return _Border; }
  21. set
  22. {
  23. if (_Border != value)
  24. {
  25. _Border = value;
  26. SetBorder();
  27. }
  28. }
  29. }
  30. //窗体边框颜色
  31. private Color _BorderColor = Color.Black;
  32. [Category("Style")]
  33. [Description("窗体边框颜色")]
  34. [DefaultValue(typeof(Color), "Black")]
  35. public Color BorderColor
  36. {
  37. get { return _BorderColor; }
  38. set
  39. {
  40. if (_BorderColor != value)
  41. {
  42. _BorderColor = value;
  43. SetBorder();
  44. }
  45. }
  46. }
  47. //设置炫彩模式
  48. private bool _Colorful = false;
  49. [Category("Style")]
  50. [Description("炫彩模式")]
  51. [DefaultValue(typeof(bool), "false")]
  52. public bool Colorful
  53. {
  54. get { return _Colorful; }
  55. set
  56. {
  57. if (_Colorful != value)
  58. {
  59. _Colorful = value;
  60. if (value) SetColorful();
  61. }
  62. }
  63. }
  64. #endregion
  65. public NoTitleForm()
  66. {
  67. InitializeComponent();
  68. }
  69. private void NoTitleForm_Load(object sender, EventArgs e)
  70. {
  71. SetBorder();
  72. if (_Colorful) SetColorful();
  73. }
  74. /// <summary>
  75. /// 设置无标题窗口可拖动
  76. /// </summary>
  77. /// <param name="e"></param>
  78. protected override void OnMouseMove(MouseEventArgs e)
  79. {
  80. base.OnMouseMove(e);
  81. if (e.Button == MouseButtons.Left)
  82. {
  83. FormStyleAPI.ReleaseCapture();
  84. FormStyleAPI.SendMessage(Handle, FormStyleAPI.WM_NCLBUTTONDOWN, FormStyleAPI.HTCAPTION, 0);
  85. }
  86. }
  87. /// <summary>
  88. /// 设置窗口边框
  89. /// </summary>
  90. protected void SetBorder()
  91. {
  92. if (_Border > 0)
  93. {
  94. Label BorderTop = new Label();
  95. BorderTop.BackColor = _BorderColor;
  96. BorderTop.Width = Width;
  97. BorderTop.Height = _Border;
  98. Controls.Add(BorderTop);
  99. BorderTop.BringToFront();
  100. BorderTop.Top = 0;
  101. BorderTop.Left = 0;
  102. Label BorderBottom = new Label();
  103. BorderBottom.BackColor = _BorderColor;
  104. BorderBottom.Width = Width;
  105. BorderBottom.Height = _Border;
  106. Controls.Add(BorderBottom);
  107. BorderBottom.BringToFront();
  108. BorderBottom.Top = Height - _Border;
  109. BorderBottom.Left = 0;
  110. Label BorderLeft = new Label();
  111. BorderLeft.BackColor = _BorderColor;
  112. BorderLeft.Width = _Border;
  113. BorderLeft.Height = Height;
  114. Controls.Add(BorderLeft);
  115. BorderLeft.BringToFront();
  116. BorderLeft.Top = 0;
  117. BorderLeft.Left = 0;
  118. Label BorderRight = new Label();
  119. BorderRight.BackColor = _BorderColor;
  120. BorderRight.Width = _Border;
  121. BorderRight.Height = Height;
  122. Controls.Add(BorderRight);
  123. BorderRight.BringToFront();
  124. BorderRight.Top = 0;
  125. BorderRight.Left = Width - _Border;
  126. }
  127. }
  128. /// <summary>
  129. /// 设置炫彩模式
  130. /// </summary>
  131. protected void SetColorful()
  132. {
  133. string[] colors = ColorStyle.Warm.Concat(ColorStyle.Silence).ToArray();
  134. int index = new Random().Next(colors.Length);
  135. BackColor = ColorTranslator.FromHtml(colors[index]);
  136. }
  137. protected override void OnPaint(PaintEventArgs e)
  138. {
  139. base.OnPaint(e);
  140. //Graphics g = CreateGraphics();
  141. //g.DrawRectangle(new Pen(Color.Red, 1), new Rectangle(0, 0, Width, Height));
  142. }
  143. /// <summary>
  144. /// 设置控件是否可用
  145. /// </summary>
  146. /// <param name="ctrl"></param>
  147. /// <param name="enable"></param>
  148. public void UIEnable(Control ctrl, bool enable = true)
  149. {
  150. try
  151. {
  152. BeginInvoke(new Action(() =>
  153. {
  154. ctrl.Enabled = enable;
  155. }));
  156. }
  157. catch (Exception e) { }
  158. }
  159. public void UIVisible(Control ctrl, bool enable = true)
  160. {
  161. try
  162. {
  163. BeginInvoke(new Action(() =>
  164. {
  165. ctrl.Visible = enable;
  166. }));
  167. }
  168. catch (Exception e) { }
  169. }
  170. public void UIClose()
  171. {
  172. try
  173. {
  174. BeginInvoke(new Action(() =>
  175. {
  176. Close();
  177. }));
  178. }
  179. catch (Exception e) { }
  180. }
  181. }
  182. }