ShadowForm.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. }
  29. private void IrregularForm_Load(object sender, EventArgs e)
  30. {
  31. if (!DesignMode)
  32. {
  33. Skin = new ShadowFormSkin(this);//创建皮肤层
  34. ShadowForm_LocationChanged(null, null);
  35. Skin.BackColor = Color.Red;
  36. Skin.Show();//显示皮肤层
  37. }
  38. }
  39. #region 减少闪烁
  40. private void SetStyles()
  41. {
  42. SetStyle(
  43. ControlStyles.UserPaint |
  44. ControlStyles.AllPaintingInWmPaint |
  45. ControlStyles.OptimizedDoubleBuffer |
  46. ControlStyles.ResizeRedraw |
  47. ControlStyles.DoubleBuffer, true);
  48. //强制分配样式重新应用到控件上
  49. UpdateStyles();
  50. base.AutoScaleMode = AutoScaleMode.None;
  51. }
  52. #endregion
  53. /// <summary>
  54. /// 窗体显示状态
  55. /// </summary>
  56. /// <param name="value"></param>
  57. public void Visibility(bool value)
  58. {
  59. if (value)
  60. {
  61. Show();
  62. Skin.Show();
  63. }
  64. else
  65. {
  66. Hide();
  67. Skin.Hide();
  68. }
  69. }
  70. protected override void OnMouseMove(MouseEventArgs e)
  71. {
  72. base.OnMouseMove(e);
  73. _MouseLocation = e.Location;
  74. if (e.Button == MouseButtons.Left)
  75. {
  76. FormStyleAPI.ReleaseCapture();
  77. FormStyleAPI.SendMessage(Handle, FormStyleAPI.WM_NCLBUTTONDOWN, FormStyleAPI.HTCAPTION, 0);
  78. }
  79. }
  80. private void ShadowForm_LocationChanged(object sender, EventArgs e)
  81. {
  82. if (Skin != null)
  83. {
  84. Skin.Location = new Point(Left - ShadowWidth, Top - ShadowWidth);
  85. Skin.DrawShadow();
  86. }
  87. }
  88. }
  89. }