BaseShadowForm.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using LYFZ.WinAPI;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. /// <summary>
  13. /// 无边框四边阴影基窗体
  14. /// </summary>
  15. public partial class BaseShadowForm : BaseMotherForm
  16. {
  17. ShadowLayer layer = null;
  18. bool _isShadow = true;
  19. /// <summary>
  20. /// 是否开启四边阴影
  21. /// </summary>
  22. [DescriptionAttribute("是否开启四边阴影"), CategoryAttribute("自定义窗体属性"), DefaultValueAttribute(typeof(bool), "true")]
  23. public bool IsShadow
  24. {
  25. get { return _isShadow; }
  26. set { _isShadow = value; }
  27. }
  28. Rectangle _layerRel = new Rectangle(5,5,10,12);
  29. /// <summary>
  30. /// 在开启阴影效果时设置阴影位置和大小
  31. /// </summary>
  32. [DescriptionAttribute("在开启阴影效果时设置阴影位置和大小"), CategoryAttribute("自定义窗体属性")]
  33. public Rectangle LayerRel
  34. {
  35. get { return _layerRel; }
  36. set { _layerRel = value; }
  37. }
  38. public BaseShadowForm()
  39. {
  40. InitializeComponent();
  41. }
  42. //Show或Hide被调用时
  43. protected override void OnVisibleChanged(EventArgs e)
  44. {
  45. if (Visible)
  46. {
  47. //启用窗口淡入淡出
  48. if (!DesignMode)
  49. {
  50. //淡入特效
  51. // Win32.AnimateWindow(this.Handle, 150, Win32.AW_BLEND | Win32.AW_ACTIVATE);
  52. }
  53. //判断不是在设计器中
  54. if (!DesignMode && (layer == null ||layer.IsDisposed))
  55. {
  56. if (IsShadow)
  57. {
  58. layer = new ShadowLayer(this);
  59. layer.Difference_X = LayerRel.X;
  60. layer.Difference_Y = LayerRel.Y;
  61. layer.Difference_Width = LayerRel.Width;
  62. layer.Difference_Height = LayerRel.Height;
  63. if (this.IsReion && this.Width == 780 && this.Height == 580)
  64. {
  65. layer.Difference_X = 19;
  66. layer.Difference_Y = 27;
  67. layer.Difference_Width = 38;
  68. layer.Difference_Height = 46;
  69. layer.SetInit();
  70. }
  71. try
  72. {
  73. layer.Show(this);
  74. }
  75. catch { }
  76. }
  77. }
  78. base.OnVisibleChanged(e);
  79. }
  80. else
  81. {
  82. base.OnVisibleChanged(e);
  83. // Win32.AnimateWindow(this.Handle, 150, Win32.AW_BLEND | Win32.AW_HIDE);
  84. }
  85. }
  86. //窗体关闭时
  87. protected override void OnClosing(CancelEventArgs e)
  88. {
  89. base.OnClosing(e);
  90. try
  91. {
  92. //先关闭阴影窗体
  93. if (layer != null && !layer.IsDisposed)
  94. {
  95. layer.Close();
  96. layer.Dispose();
  97. layer = null;
  98. }
  99. }
  100. catch { }
  101. //在Form_FormClosing中添加代码实现窗体的淡出
  102. // Win32.AnimateWindow(this.Handle, 150, Win32.AW_BLEND | Win32.AW_HIDE);
  103. }
  104. }
  105. }