SimpleShadowForm.cs 5.6 KB

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