IrregularForm.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.ComponentModel;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace Y.Skin.YoForm.Irregular
  11. {
  12. public partial class IrregularForm : Form
  13. {
  14. private IrregularFormSkin Skin;
  15. public IrregularForm()
  16. {
  17. InitializeComponent();
  18. //SetStyles();//减少闪烁
  19. ShowInTaskbar = false;//禁止控件层显示到任务栏
  20. FormBorderStyle = FormBorderStyle.None;//设置无边框的窗口样式
  21. TransparencyKey = BackColor;//使控件层背景透明
  22. }
  23. private void IrregularForm_Load(object sender, EventArgs e)
  24. {
  25. if (!DesignMode)
  26. {
  27. Opacity = 0;
  28. Skin = new IrregularFormSkin(this);//创建皮肤层
  29. BackgroundImage = null;//去除控件层背景
  30. Skin.Show();//显示皮肤层
  31. AnimateShow();
  32. if (ContextMenuStrip != null) Skin.ContextMenuStrip = ContextMenuStrip;//设置右键菜单
  33. }
  34. }
  35. #region 属性
  36. private bool _Moveable = true;
  37. [Category("Skin")]
  38. [Description("窗体是否可以移动")]
  39. [DefaultValue(typeof(bool), "true")]
  40. public bool Movable
  41. {
  42. get { return _Moveable; }
  43. set
  44. {
  45. if (_Moveable != value)
  46. {
  47. _Moveable = value;
  48. }
  49. }
  50. }
  51. private bool _InTaskbar = true;
  52. [Category("Skin")]
  53. [Description("窗体是否显示到任务栏")]
  54. [DefaultValue(typeof(bool), "true")]
  55. public bool InTaskbar
  56. {
  57. get { return _InTaskbar; }
  58. set
  59. {
  60. if (_InTaskbar != value)
  61. {
  62. _InTaskbar = value;
  63. }
  64. }
  65. }
  66. #endregion
  67. #region 减少闪烁
  68. private void SetStyles()
  69. {
  70. SetStyle(
  71. ControlStyles.UserPaint |
  72. ControlStyles.AllPaintingInWmPaint |
  73. ControlStyles.OptimizedDoubleBuffer |
  74. ControlStyles.ResizeRedraw |
  75. ControlStyles.DoubleBuffer, true);
  76. //强制分配样式重新应用到控件上
  77. UpdateStyles();
  78. base.AutoScaleMode = AutoScaleMode.None;
  79. }
  80. #endregion
  81. private void AnimateShow()
  82. {
  83. Task.Factory.StartNew(() =>
  84. {
  85. for (int i = 0; i <= 10; i++)
  86. {
  87. BeginInvoke(new Action(() =>
  88. {
  89. Opacity = i / 10.0;
  90. Skin.SetBits();
  91. }));
  92. Thread.Sleep(25);
  93. }
  94. });
  95. }
  96. /// <summary>
  97. /// 窗体显示状态
  98. /// </summary>
  99. /// <param name="value"></param>
  100. public void Visibility(bool value)
  101. {
  102. if (value)
  103. {
  104. Show();
  105. Skin.Show();
  106. }
  107. else
  108. {
  109. Hide();
  110. Skin.Hide();
  111. }
  112. }
  113. }
  114. }