SimpleShadowBackForm.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Azylee.WinformSkin.APIUtils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace Azylee.WinformSkin.FormUI.SimpleShadow
  12. {
  13. partial class SimpleShadowBackForm : Form
  14. {
  15. #region 属性
  16. private Point _MouseLocation;
  17. internal Point MouseLocation { get { return _MouseLocation; } }
  18. private SimpleShadowForm Main;
  19. #endregion
  20. public SimpleShadowBackForm(SimpleShadowForm main)
  21. {
  22. InitializeComponent();
  23. SetStyles();//减少闪烁
  24. Main = main;//获取控件层对象
  25. Text = main.Text;//设置标题栏文本
  26. Icon = main.Icon;//设置图标
  27. TopMost = main.TopMost;
  28. FormBorderStyle = FormBorderStyle.None;//设置无边框的窗口样式
  29. ShowInTaskbar = true;//显示阴影层到任务栏(win10可显示缩略图)
  30. Size = Main.Size;//统一大小
  31. Width += (Main.ShadowWidth * 2);
  32. Height += (Main.ShadowWidth * 2);
  33. Main.Owner = this;//设置控件层的拥有皮肤层
  34. DrawShadow();
  35. //SetBits();//绘制半透明不规则皮肤
  36. }
  37. private void SimpleShadowBackForm_Load(object sender, EventArgs e)
  38. {
  39. }
  40. #region 减少闪烁
  41. private void SetStyles()
  42. {
  43. SetStyle(
  44. ControlStyles.UserPaint |
  45. ControlStyles.AllPaintingInWmPaint |
  46. ControlStyles.OptimizedDoubleBuffer |
  47. ControlStyles.ResizeRedraw |
  48. ControlStyles.DoubleBuffer, true);
  49. //强制分配样式重新应用到控件上
  50. UpdateStyles();
  51. base.AutoScaleMode = AutoScaleMode.None;
  52. }
  53. #endregion
  54. #region 绘图
  55. protected override CreateParams CreateParams
  56. {
  57. get
  58. {
  59. CreateParams cParms = base.CreateParams;
  60. cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
  61. return cParms;
  62. }
  63. }
  64. internal void DrawShadow()
  65. {
  66. Bitmap bitmap = null;
  67. Graphics g = null;
  68. try
  69. {
  70. bitmap = new Bitmap(Width, Height);
  71. g = Graphics.FromImage(bitmap);
  72. g.SmoothingMode = SmoothingMode.AntiAlias;
  73. Color c = Color.FromArgb(0, 0, 0, 0);
  74. Pen p = new Pen(c, 3);
  75. for (int i = 0; i < Main.ShadowWidth; i++)
  76. {
  77. p.Color = Color.FromArgb((255 / 10 / Main.ShadowWidth) * i, c);
  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. }