IrregularForm.cs 3.5 KB

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