ShadowLayer.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 System.Runtime.InteropServices;
  10. using System.Drawing.Imaging;
  11. using System.Drawing.Drawing2D;
  12. using LYFZ.WinAPI;
  13. namespace LYFZ.ComponentLibrary
  14. {
  15. //控件层
  16. public class ShadowLayer : Form
  17. {
  18. //控件层
  19. private BaseMotherForm Main;
  20. private int _Difference_X = 5;
  21. /// <summary>
  22. /// 阴影水平徧移量
  23. /// </summary>
  24. public int Difference_X
  25. {
  26. get { return _Difference_X; }
  27. set { _Difference_X = value; }
  28. }
  29. private int _Difference_Y = 5;
  30. /// <summary>
  31. /// 阴影垂直徧移量
  32. /// </summary>
  33. public int Difference_Y
  34. {
  35. get { return _Difference_Y; }
  36. set { _Difference_Y = value; }
  37. }
  38. private int _Difference_Width = 10;
  39. /// <summary>
  40. /// 阴影宽度增量值
  41. /// </summary>
  42. public int Difference_Width
  43. {
  44. get { return _Difference_Width; }
  45. set { _Difference_Width = value; }
  46. }
  47. private int _Difference_Height = 12;
  48. /// <summary>
  49. /// 阴影高度增量值
  50. /// </summary>
  51. public int Difference_Height
  52. {
  53. get { return _Difference_Height; }
  54. set { _Difference_Height = value; }
  55. }
  56. //带参构造
  57. public ShadowLayer(BaseMotherForm main)
  58. {
  59. //将控制层传值过来
  60. this.Main = main;
  61. SetInit();
  62. }
  63. /// <summary>
  64. /// 设置初始化
  65. /// </summary>
  66. public void SetInit()
  67. {
  68. //置顶窗体
  69. TopMost = Main.TopMost;
  70. this.SendToBack();
  71. Main.BringToFront();
  72. //是否在任务栏显示
  73. ShowInTaskbar = false;
  74. //无边框模式
  75. FormBorderStyle = FormBorderStyle.None;
  76. //设置绘图层显示位置
  77. this.Location = new Point(Main.Location.X - Difference_X, Main.Location.Y - Difference_Y);
  78. //设置ICO
  79. Icon = Main.Icon;
  80. ShowIcon = Main.ShowIcon;
  81. //设置大小
  82. Width = Main.Width + Difference_Width;
  83. Height = Main.Height + Difference_Height;
  84. //设置标题名
  85. Text = Main.Text;
  86. //绘图层窗体移动
  87. Main.LocationChanged += new EventHandler(Main_LocationChanged);
  88. Main.SizeChanged += new EventHandler(Main_SizeChanged);
  89. Main.VisibleChanged += new EventHandler(Main_VisibleChanged);
  90. //还原任务栏右键菜单
  91. // CommonClass.SetTaskMenu(Main);
  92. //加载背景
  93. SetBits();
  94. //窗口鼠标穿透效果
  95. CanPenetrate();
  96. }
  97. #region 还原任务栏右键菜单
  98. protected override CreateParams CreateParams
  99. {
  100. get
  101. {
  102. CreateParams cParms = base.CreateParams;
  103. cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
  104. return cParms;
  105. }
  106. }
  107. public class CommonClass
  108. {
  109. [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
  110. static extern int GetWindowLong(HandleRef hWnd, int nIndex);
  111. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  112. static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
  113. public const int WS_SYSMENU = 0x00080000;
  114. public const int WS_MINIMIZEBOX = 0x20000;
  115. public static void SetTaskMenu(Form form)
  116. {
  117. int windowLong = (GetWindowLong(new HandleRef(form, form.Handle), -16));
  118. SetWindowLong(new HandleRef(form, form.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
  119. }
  120. }
  121. #endregion
  122. #region 减少闪烁
  123. private void SetStyles()
  124. {
  125. SetStyle(
  126. ControlStyles.UserPaint |
  127. ControlStyles.AllPaintingInWmPaint |
  128. ControlStyles.OptimizedDoubleBuffer |
  129. ControlStyles.ResizeRedraw |
  130. ControlStyles.DoubleBuffer, true);
  131. //强制分配样式重新应用到控件上
  132. UpdateStyles();
  133. base.AutoScaleMode = AutoScaleMode.None;
  134. }
  135. #endregion
  136. #region 控件层相关事件
  137. //移动主窗体时
  138. void Main_LocationChanged(object sender, EventArgs e)
  139. {
  140. Location = new Point(Main.Left - Difference_X, Main.Top - Difference_Y);
  141. }
  142. //主窗体大小改变时
  143. void Main_SizeChanged(object sender, EventArgs e)
  144. {
  145. //设置大小
  146. Width = Main.Width + Difference_Width;
  147. Height = Main.Height + Difference_Height;
  148. SetBits();
  149. }
  150. //主窗体显示或隐藏时
  151. void Main_VisibleChanged(object sender, EventArgs e)
  152. {
  153. this.Visible = Main.Visible;
  154. }
  155. #endregion
  156. #region 使窗口有鼠标穿透功能
  157. /// <summary>
  158. /// 使窗口有鼠标穿透功能
  159. /// </summary>
  160. private void CanPenetrate()
  161. {
  162. int intExTemp = Win32.GetWindowLong(this.Handle, Win32.GWL_EXSTYLE);
  163. int oldGWLEx = Win32.SetWindowLong(this.Handle, Win32.GWL_EXSTYLE, Win32.WS_EX_TRANSPARENT | Win32.WS_EX_LAYERED);
  164. }
  165. #endregion
  166. Bitmap GetImage()
  167. {
  168. Bitmap image = LYFZ.ComponentLibrary.Properties.Resources.window_shadow2;
  169. if (Main.IsReion) {
  170. image = LYFZ.ComponentLibrary.Properties.Resources.Vista_Aero3;
  171. }
  172. return image;
  173. }
  174. #region 不规则无毛边方法
  175. public void SetBits()
  176. {
  177. //绘制绘图层背景
  178. Bitmap bitmap = new Bitmap(Main.Width + Difference_Width, Main.Height + Difference_Height);
  179. Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界
  180. Graphics g = Graphics.FromImage(bitmap);
  181. g.SmoothingMode = SmoothingMode.HighQuality; //高质量
  182. g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
  183. ImageDrawRect.DrawRect(g, GetImage(), ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1);
  184. if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
  185. throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
  186. IntPtr oldBits = IntPtr.Zero;
  187. IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
  188. IntPtr hBitmap = IntPtr.Zero;
  189. IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
  190. try
  191. {
  192. Win32.Point topLoc = new Win32.Point(Left, Top);
  193. Win32.Size bitMapSize = new Win32.Size(Width, Height);
  194. Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
  195. Win32.Point srcLoc = new Win32.Point(0, 0);
  196. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
  197. oldBits = Win32.SelectObject(memDc, hBitmap);
  198. blendFunc.BlendOp = Win32.AC_SRC_OVER;
  199. blendFunc.SourceConstantAlpha = Byte.Parse("255");
  200. blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
  201. blendFunc.BlendFlags = 0;
  202. Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
  203. }
  204. finally
  205. {
  206. if (hBitmap != IntPtr.Zero)
  207. {
  208. Win32.SelectObject(memDc, oldBits);
  209. Win32.DeleteObject(hBitmap);
  210. }
  211. Win32.ReleaseDC(IntPtr.Zero, screenDC);
  212. Win32.DeleteDC(memDc);
  213. }
  214. }
  215. #endregion
  216. private void InitializeComponent()
  217. {
  218. this.SuspendLayout();
  219. //
  220. // ShadowLayer
  221. //
  222. this.ClientSize = new System.Drawing.Size(284, 262);
  223. this.Name = "ShadowLayer";
  224. this.ResumeLayout(false);
  225. }
  226. }
  227. }