ShadowFormSkin.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Y.Utils.WindowsUtils.APIUtils;
  12. namespace Y.Skin.YoForm.Shadow
  13. {
  14. partial class ShadowFormSkin : Form
  15. {
  16. private Point _MouseLocation;
  17. internal Point MouseLocation { get { return _MouseLocation; } }
  18. private ShadowForm Main;
  19. public ShadowFormSkin(ShadowForm main)
  20. {
  21. InitializeComponent();
  22. SetStyles();//减少闪烁
  23. Main = main;//获取控件层对象
  24. Text = main.Text;//设置标题栏文本
  25. Icon = main.Icon;//设置图标
  26. TopMost = main.TopMost;
  27. FormBorderStyle = FormBorderStyle.None;//设置无边框的窗口样式
  28. ShowInTaskbar = false;//使控件层显示到任务栏
  29. Size = Main.Size;//统一大小
  30. Width += (Main.ShadowWidth * 2);
  31. Height += (Main.ShadowWidth * 2);
  32. Main.Owner = this;//设置控件层的拥有皮肤层
  33. //SetBits();//绘制半透明不规则皮肤
  34. }
  35. private void ShadowFormSkin_Load(object sender, EventArgs e)
  36. {
  37. DrawShadow();
  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. #region 绘图
  54. protected override CreateParams CreateParams
  55. {
  56. get
  57. {
  58. CreateParams cParms = base.CreateParams;
  59. cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
  60. return cParms;
  61. }
  62. }
  63. internal void DrawShadow()
  64. {
  65. Bitmap bitmap = null;
  66. Graphics g = null;
  67. try
  68. {
  69. bitmap = new Bitmap(Width, Height);
  70. g = Graphics.FromImage(bitmap);
  71. g.SmoothingMode = SmoothingMode.AntiAlias;
  72. Color c = Color.FromArgb(0, 0, 0, 0);
  73. Pen p = new Pen(c, 3);
  74. for (int i = 0; i < Main.ShadowWidth; i++)
  75. {
  76. p.Color = Color.FromArgb((255 / 10 / Main.ShadowWidth) * i, c);
  77. //g.DrawRectangle(p, new Rectangle(i, i, Width - (2 * i) - 1, Height - (2 * i) - 1));
  78. DrawRoundRectangle(g, p, new Rectangle(i, i, Width - (2 * i) - 1, Height - (2 * i) - 1), Main.ShadowWidth - i);
  79. }
  80. SetBits(bitmap);
  81. }
  82. catch { }
  83. finally
  84. {
  85. g?.Dispose();
  86. bitmap?.Dispose();
  87. }
  88. }
  89. public void SetBits(Bitmap bitmap)
  90. {
  91. if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat))
  92. throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
  93. IntPtr oldBits = IntPtr.Zero;
  94. IntPtr screenDC = FormStyleAPI.GetDC(IntPtr.Zero);
  95. IntPtr hBitmap = IntPtr.Zero;
  96. IntPtr memDc = FormStyleAPI.CreateCompatibleDC(screenDC);
  97. try
  98. {
  99. FormStyleAPI.Point topLoc = new FormStyleAPI.Point(Left, Top);
  100. FormStyleAPI.Size bitMapSize = new FormStyleAPI.Size(Width, Height);
  101. FormStyleAPI.BLENDFUNCTION blendFunc = new FormStyleAPI.BLENDFUNCTION();
  102. FormStyleAPI.Point srcLoc = new FormStyleAPI.Point(0, 0);
  103. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
  104. oldBits = FormStyleAPI.SelectObject(memDc, hBitmap);
  105. blendFunc.BlendOp = FormStyleAPI.AC_SRC_OVER;
  106. blendFunc.SourceConstantAlpha = Byte.Parse(((int)(Main.Opacity * 255)).ToString());
  107. blendFunc.AlphaFormat = FormStyleAPI.AC_SRC_ALPHA;
  108. blendFunc.BlendFlags = 0;
  109. FormStyleAPI.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, FormStyleAPI.ULW_ALPHA);
  110. }
  111. finally
  112. {
  113. if (hBitmap != IntPtr.Zero)
  114. {
  115. FormStyleAPI.SelectObject(memDc, oldBits);
  116. FormStyleAPI.DeleteObject(hBitmap);
  117. }
  118. FormStyleAPI.ReleaseDC(IntPtr.Zero, screenDC);
  119. FormStyleAPI.DeleteDC(memDc);
  120. }
  121. }
  122. #endregion
  123. #region MyRegion
  124. public static void DrawRoundRectangle(Graphics g, Pen pen, Rectangle rect, int cornerRadius)
  125. {
  126. using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
  127. {
  128. g.DrawPath(pen, path);
  129. }
  130. }
  131. public static void FillRoundRectangle(Graphics g, Brush brush, Rectangle rect, int cornerRadius)
  132. {
  133. using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
  134. {
  135. g.FillPath(brush, path);
  136. }
  137. }
  138. internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
  139. {
  140. GraphicsPath roundedRect = new GraphicsPath();
  141. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  142. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  143. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  144. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  145. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  146. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  147. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  148. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  149. roundedRect.CloseFigure();
  150. return roundedRect;
  151. }
  152. #endregion
  153. }
  154. }