博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
窗体四边阴影
阅读量:6242 次
发布时间:2019-06-22

本文共 7808 字,大约阅读时间需要 26 分钟。

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 }
View Code

 

转载于:https://www.cnblogs.com/maxin991025-/p/6169927.html

你可能感兴趣的文章
Java中菜单组件
查看>>
git reset revert 回退回滚取消提交返回上一版本
查看>>
适配mpvue平台的的微信小程序日历组件mpvue-calendar
查看>>
Consul Config 使用Git做版本控制的实现
查看>>
我们必须要知道的RESTful服务最佳实践
查看>>
百度调整Q2营收预期
查看>>
阿里巴巴智慧建筑(IB)峰会 与筑梦者共建新生态
查看>>
Apache Zeppelin安装及使用
查看>>
Redis实现微博后台业务逻辑系列(四)
查看>>
Power5连接使用DS8000遇到问题处理一例
查看>>
迈克菲实验室:Flame病毒的深度分析
查看>>
用十条命令在一分钟内检查Linux服务器性能[转]
查看>>
深入理解bash及字符串的处理
查看>>
Python异步IO --- 轻松管理10k+并发连接
查看>>
DNS多点部署IP Anycast+BGP实战分析
查看>>
iostat详细使用
查看>>
用户与组
查看>>
【12c新特性】12c中新加入的Enqueue Lock
查看>>
JavaScript语法详解(四)
查看>>
Fail to queue the whole FAL gap in dataguard一例
查看>>