IrregularForm.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 _skinmobile = true;
  37. [Category("Skin")]
  38. [Description("窗体是否可以移动")]
  39. [DefaultValue(typeof(bool), "true")]
  40. public bool SkinMovable
  41. {
  42. get { return _skinmobile; }
  43. set
  44. {
  45. if (_skinmobile != value)
  46. {
  47. _skinmobile = value;
  48. }
  49. }
  50. }
  51. #endregion
  52. #region 减少闪烁
  53. private void SetStyles()
  54. {
  55. SetStyle(
  56. ControlStyles.UserPaint |
  57. ControlStyles.AllPaintingInWmPaint |
  58. ControlStyles.OptimizedDoubleBuffer |
  59. ControlStyles.ResizeRedraw |
  60. ControlStyles.DoubleBuffer, true);
  61. //强制分配样式重新应用到控件上
  62. UpdateStyles();
  63. base.AutoScaleMode = AutoScaleMode.None;
  64. }
  65. #endregion
  66. private void AnimateShow()
  67. {
  68. Task.Factory.StartNew(() =>
  69. {
  70. for (int i = 0; i <= 10; i++)
  71. {
  72. BeginInvoke(new Action(() =>
  73. {
  74. Opacity = i / 10.0;
  75. Skin.SetBits();
  76. }));
  77. Thread.Sleep(25);
  78. }
  79. });
  80. }
  81. /// <summary>
  82. /// 窗体显示状态
  83. /// </summary>
  84. /// <param name="value"></param>
  85. public void Visibility(bool value)
  86. {
  87. if (value)
  88. {
  89. Show();
  90. Skin.Show();
  91. }
  92. else
  93. {
  94. Hide();
  95. Skin.Hide();
  96. }
  97. }
  98. }
  99. }