ShadowForm.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.4.27 - 2017.8.25
  5. // desc: 工具描述
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.ComponentModel;
  10. using System.Drawing;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using Y.Utils.WindowsUtils.APIUtils;
  15. namespace Y.Skin.YoForm.Shadow
  16. {
  17. public partial class ShadowForm : Form
  18. {
  19. private Point _MouseLocation;
  20. internal Point MouseLocation { get { return _MouseLocation; } }
  21. internal int ShadowWidth = 15;
  22. private ShadowFormSkin Skin;
  23. public ShadowForm()
  24. {
  25. InitializeComponent();
  26. //SetStyles();//减少闪烁
  27. FormBorderStyle = FormBorderStyle.None;//设置无边框的窗口样式
  28. ShowInTaskbar = false;
  29. }
  30. private void IrregularForm_Load(object sender, EventArgs e)
  31. {
  32. if (!DesignMode)
  33. {
  34. Skin = new ShadowFormSkin(this);//创建皮肤层
  35. ShadowForm_LocationChanged(null, null);
  36. Skin.BackColor = Color.Red;
  37. Skin.Show();//显示皮肤层
  38. }
  39. }
  40. #region 减少闪烁
  41. private void SetStyles()
  42. {
  43. SetStyle(
  44. ControlStyles.UserPaint |
  45. ControlStyles.AllPaintingInWmPaint |
  46. ControlStyles.OptimizedDoubleBuffer |
  47. ControlStyles.ResizeRedraw |
  48. ControlStyles.DoubleBuffer, true);
  49. //强制分配样式重新应用到控件上
  50. UpdateStyles();
  51. base.AutoScaleMode = AutoScaleMode.None;
  52. }
  53. #endregion
  54. /// <summary>
  55. /// 窗体显示状态
  56. /// </summary>
  57. /// <param name="value"></param>
  58. public void Visibility(bool value)
  59. {
  60. if (value)
  61. {
  62. Show();
  63. Skin.Show();
  64. }
  65. else
  66. {
  67. Hide();
  68. Skin.Hide();
  69. }
  70. }
  71. protected override void OnMouseMove(MouseEventArgs e)
  72. {
  73. base.OnMouseMove(e);
  74. _MouseLocation = e.Location;
  75. if (e.Button == MouseButtons.Left)
  76. {
  77. FormStyleAPI.ReleaseCapture();
  78. FormStyleAPI.SendMessage(Handle, FormStyleAPI.WM_NCLBUTTONDOWN, FormStyleAPI.HTCAPTION, 0);
  79. }
  80. }
  81. private void ShadowForm_LocationChanged(object sender, EventArgs e)
  82. {
  83. if (Skin != null)
  84. {
  85. Skin.Location = new Point(Left - ShadowWidth, Top - ShadowWidth);
  86. Skin.DrawShadow();
  87. }
  88. }
  89. /// <summary>
  90. /// 控件可用
  91. /// </summary>
  92. /// <param name="ctrl"></param>
  93. /// <param name="enable"></param>
  94. public void UIEnable(Control ctrl, bool enable = true)
  95. {
  96. Invoke(new Action(() =>
  97. {
  98. ctrl.Enabled = enable;
  99. }));
  100. }
  101. /// <summary>
  102. /// 控件显示
  103. /// </summary>
  104. /// <param name="ctrl"></param>
  105. /// <param name="enable"></param>
  106. public void UIVisible(Control ctrl, bool enable = true)
  107. {
  108. Invoke(new Action(() =>
  109. {
  110. ctrl.Visible = enable;
  111. }));
  112. }
  113. /// <summary>
  114. /// UICLose
  115. /// </summary>
  116. public void UIClose()
  117. {
  118. Invoke(new Action(() =>
  119. {
  120. Close();
  121. Skin.Close();
  122. }));
  123. }
  124. /// <summary>
  125. /// 最小化
  126. /// </summary>
  127. /// <param name="sender"></param>
  128. /// <param name="e"></param>
  129. public void UIMin()
  130. {
  131. Invoke(new Action(() =>
  132. {
  133. WindowState = FormWindowState.Minimized;
  134. Skin.WindowState = FormWindowState.Minimized;
  135. }));
  136. }
  137. /// <summary>
  138. /// 最大化及还原
  139. /// </summary>
  140. /// <param name="sender"></param>
  141. /// <param name="e"></param>
  142. public void UIMax()
  143. {
  144. Invoke(new Action(() =>
  145. {
  146. if (WindowState != FormWindowState.Maximized)
  147. {
  148. WindowState = FormWindowState.Maximized;
  149. Skin.WindowState = FormWindowState.Maximized;
  150. }
  151. else
  152. {
  153. WindowState = FormWindowState.Normal;
  154. Skin.WindowState = FormWindowState.Normal;
  155. }
  156. }));
  157. }
  158. public void UIShow()
  159. {
  160. Invoke(new Action(() =>
  161. {
  162. Show();
  163. Skin.Show();
  164. }));
  165. }
  166. public void UIHide()
  167. {
  168. Invoke(new Action(() =>
  169. {
  170. Hide();
  171. Skin.Hide();
  172. }));
  173. }
  174. public void DrawShadow()
  175. {
  176. if (Skin != null)
  177. {
  178. Invoke(new Action(() =>
  179. {
  180. Skin.DrawShadow();
  181. }));
  182. }
  183. }
  184. }
  185. }