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