ShadowForm.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  90. }