ShadowFormSkin.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = true;//显示阴影层到任务栏(win10可显示缩略图)
  29. Size = Main.Size;//统一大小
  30. Width += (Main.ShadowWidth * 2);
  31. Height += (Main.ShadowWidth * 2);
  32. Main.Owner = this;//设置控件层的拥有皮肤层
  33. DrawShadow();
  34. //SetBits();//绘制半透明不规则皮肤
  35. }
  36. private void ShadowFormSkin_Load(object sender, EventArgs e)
  37. {
  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. DrawRoundRectangle(g, p, new Rectangle(i, i, Width - (2 * i) - 1, Height - (2 * i) - 1), Main.ShadowWidth - i);
  78. }
  79. SetBits(bitmap);
  80. }
  81. catch { }
  82. finally
  83. {
  84. g?.Dispose();
  85. bitmap?.Dispose();
  86. }
  87. }
  88. public void SetBits(Bitmap bitmap)
  89. {
  90. if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat))
  91. throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
  92. IntPtr oldBits = IntPtr.Zero;
  93. IntPtr screenDC = FormStyleAPI.GetDC(IntPtr.Zero);
  94. IntPtr hBitmap = IntPtr.Zero;
  95. IntPtr memDc = FormStyleAPI.CreateCompatibleDC(screenDC);
  96. try
  97. {
  98. FormStyleAPI.Point topLoc = new FormStyleAPI.Point(Left, Top);
  99. FormStyleAPI.Size bitMapSize = new FormStyleAPI.Size(Width, Height);
  100. FormStyleAPI.BLENDFUNCTION blendFunc = new FormStyleAPI.BLENDFUNCTION();
  101. FormStyleAPI.Point srcLoc = new FormStyleAPI.Point(0, 0);
  102. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
  103. oldBits = FormStyleAPI.SelectObject(memDc, hBitmap);
  104. blendFunc.BlendOp = FormStyleAPI.AC_SRC_OVER;
  105. blendFunc.SourceConstantAlpha = Byte.Parse(((int)(Main.Opacity * 255)).ToString());
  106. blendFunc.AlphaFormat = FormStyleAPI.AC_SRC_ALPHA;
  107. blendFunc.BlendFlags = 0;
  108. FormStyleAPI.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, FormStyleAPI.ULW_ALPHA);
  109. }
  110. finally
  111. {
  112. if (hBitmap != IntPtr.Zero)
  113. {
  114. FormStyleAPI.SelectObject(memDc, oldBits);
  115. FormStyleAPI.DeleteObject(hBitmap);
  116. }
  117. FormStyleAPI.ReleaseDC(IntPtr.Zero, screenDC);
  118. FormStyleAPI.DeleteDC(memDc);
  119. }
  120. }
  121. #endregion
  122. #region MyRegion
  123. public static void DrawRoundRectangle(Graphics g, Pen pen, Rectangle rect, int cornerRadius)
  124. {
  125. using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
  126. {
  127. g.DrawPath(pen, path);
  128. }
  129. }
  130. public static void FillRoundRectangle(Graphics g, Brush brush, Rectangle rect, int cornerRadius)
  131. {
  132. using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
  133. {
  134. g.FillPath(brush, path);
  135. }
  136. }
  137. internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
  138. {
  139. GraphicsPath roundedRect = new GraphicsPath();
  140. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  141. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  142. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  143. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  144. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  145. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  146. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  147. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  148. roundedRect.CloseFigure();
  149. return roundedRect;
  150. }
  151. #endregion
  152. }
  153. }