SkinForm.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace HXX.Scanner.Starter
  12. {
  13. partial class SkinForm : Form
  14. {
  15. private MainForm Main;
  16. public SkinForm(MainForm main)
  17. {
  18. InitializeComponent();
  19. SetStyles();//减少闪烁
  20. Main = main;//获取控件层对象
  21. FormBorderStyle = FormBorderStyle.None;//设置无边框的窗口样式
  22. ShowInTaskbar = true;//使控件层显示到任务栏
  23. BackgroundImage = Main.BackgroundImage;//将控件层背景图应用到皮肤层
  24. BackgroundImageLayout = ImageLayout.Stretch;//自动拉伸背景图以适应窗口
  25. Size = Main.Size;//统一大小
  26. Main.Owner = this;//设置控件层的拥有皮肤层
  27. FormMovableEvent();//激活皮肤层窗体移动
  28. SetBits();//绘制半透明不规则皮肤
  29. Location = new Point(Main.Location.X, Main.Location.Y);//统一控件层和皮肤层的位置
  30. }
  31. private void SkinForm_Load(object sender, EventArgs e)
  32. {
  33. }
  34. #region 减少闪烁
  35. private void SetStyles()
  36. {
  37. SetStyle(
  38. ControlStyles.UserPaint |
  39. ControlStyles.AllPaintingInWmPaint |
  40. ControlStyles.OptimizedDoubleBuffer |
  41. ControlStyles.ResizeRedraw |
  42. ControlStyles.DoubleBuffer, true);
  43. //强制分配样式重新应用到控件上
  44. UpdateStyles();
  45. base.AutoScaleMode = AutoScaleMode.None;
  46. }
  47. #endregion
  48. #region 不规则无毛边方法
  49. protected override CreateParams CreateParams
  50. {
  51. get
  52. {
  53. CreateParams cParms = base.CreateParams;
  54. cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
  55. return cParms;
  56. }
  57. }
  58. public void SetBits()
  59. {
  60. if (BackgroundImage != null)
  61. {
  62. //绘制绘图层背景
  63. Bitmap bitmap = new Bitmap(BackgroundImage, base.Width, base.Height);
  64. if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
  65. throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
  66. IntPtr oldBits = IntPtr.Zero;
  67. IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
  68. IntPtr hBitmap = IntPtr.Zero;
  69. IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
  70. try
  71. {
  72. Win32.Point topLoc = new Win32.Point(Left, Top);
  73. Win32.Size bitMapSize = new Win32.Size(Width, Height);
  74. Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
  75. Win32.Point srcLoc = new Win32.Point(0, 0);
  76. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
  77. oldBits = Win32.SelectObject(memDc, hBitmap);
  78. blendFunc.BlendOp = Win32.AC_SRC_OVER;
  79. blendFunc.SourceConstantAlpha = Byte.Parse(((int)(Main.Opacity * 255)).ToString());
  80. blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
  81. blendFunc.BlendFlags = 0;
  82. Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
  83. }
  84. finally
  85. {
  86. if (hBitmap != IntPtr.Zero)
  87. {
  88. Win32.SelectObject(memDc, oldBits);
  89. Win32.DeleteObject(hBitmap);
  90. }
  91. Win32.ReleaseDC(IntPtr.Zero, screenDC);
  92. Win32.DeleteDC(memDc);
  93. }
  94. }
  95. }
  96. #endregion
  97. #region 无标题栏的窗口移动
  98. private Point mouseOffset; //记录鼠标指针的坐标
  99. private bool isMouseDown = false; //记录鼠标按键是否按下
  100. /// <summary>
  101. /// 窗体移动监听绑定
  102. /// </summary>
  103. private void FormMovableEvent()
  104. {
  105. //绘制层窗体移动
  106. this.MouseDown += new MouseEventHandler(Frm_MouseDown);
  107. this.MouseMove += new MouseEventHandler(Frm_MouseMove);
  108. this.MouseUp += new MouseEventHandler(Frm_MouseUp);
  109. this.LocationChanged += new EventHandler(Frm_LocationChanged);
  110. //控制层层窗体移动
  111. Main.MouseDown += new MouseEventHandler(Frm_MouseDown);
  112. Main.MouseMove += new MouseEventHandler(Frm_MouseMove);
  113. Main.MouseUp += new MouseEventHandler(Frm_MouseUp);
  114. Main.LocationChanged += new EventHandler(Frm_LocationChanged);
  115. }
  116. /// <summary>
  117. /// 窗体按下时
  118. /// </summary>
  119. /// <param name="sender"></param>
  120. /// <param name="e"></param>
  121. private void Frm_MouseDown(object sender, MouseEventArgs e)
  122. {
  123. if (Main.SkinMovable)
  124. {
  125. int xOffset;
  126. int yOffset;
  127. //点击窗体时,记录鼠标位置,启动移动
  128. if (e.Button == MouseButtons.Left)
  129. {
  130. xOffset = -e.X;
  131. yOffset = -e.Y;
  132. mouseOffset = new Point(xOffset, yOffset);
  133. isMouseDown = true;
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 窗体移动时
  139. /// </summary>
  140. /// <param name="sender"></param>
  141. /// <param name="e"></param>
  142. private void Frm_MouseMove(object sender, MouseEventArgs e)
  143. {
  144. if (Main.SkinMovable)
  145. {
  146. //将调用此事件的窗口保存下
  147. Form frm = (Form)sender;
  148. //确定开启了移动模式后
  149. if (isMouseDown)
  150. {
  151. //移动的位置计算
  152. Point mousePos = Control.MousePosition;
  153. mousePos.Offset(mouseOffset.X, mouseOffset.Y);
  154. //判断是绘图层还是控件层调用了移动事件,并作出相应回馈
  155. if (frm == this)
  156. {
  157. Location = mousePos;
  158. }
  159. else
  160. {
  161. Main.Location = mousePos;
  162. }
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 窗体按下并释放按钮时
  168. /// </summary>
  169. /// <param name="sender"></param>
  170. /// <param name="e"></param>
  171. private void Frm_MouseUp(object sender, MouseEventArgs e)
  172. {
  173. if (Main.SkinMovable)
  174. {
  175. // 修改鼠标状态isMouseDown的值
  176. // 确保只有鼠标左键按下并移动时,才移动窗体
  177. if (e.Button == MouseButtons.Left)
  178. {
  179. //松开鼠标时,停止移动
  180. isMouseDown = false;
  181. //Top高度小于0的时候,等于0
  182. if (this.Top < 0)
  183. {
  184. this.Top = 0;
  185. Main.Top = 0;
  186. }
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// 窗口移动时
  192. /// </summary>
  193. /// <param name="sender"></param>
  194. /// <param name="e"></param>
  195. void Frm_LocationChanged(object sender, EventArgs e)
  196. {
  197. //将调用此事件的窗口保存下
  198. Form frm = (Form)sender;
  199. if (frm == this)
  200. {
  201. Main.Location = new Point(this.Left, this.Top);
  202. }
  203. else
  204. {
  205. Location = new Point(Main.Left, Main.Top);
  206. }
  207. }
  208. #endregion
  209. private void InitializeComponent()
  210. {
  211. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SkinForm));
  212. this.SuspendLayout();
  213. //
  214. // SkinForm
  215. //
  216. this.ClientSize = new System.Drawing.Size(295, 276);
  217. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  218. this.Name = "SkinForm";
  219. this.ResumeLayout(false);
  220. }
  221. }
  222. }