最佳的实现Winfrom无边框四周阴影方案

网上不靠谱的东西太多了,都是两边阴影,什么窗口叠加、ps作图啥的,什么玩意?本文来自Google找的,老外的方法比较实在,简洁有效。

复制代码
  1 public partial class Form1 : Form
  2 {
  3     [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
  4     private static extern IntPtr CreateRoundRectRgn
  5     (
  6         int nLeftRect, // x-coordinate of upper-left corner
  7         int nTopRect, // y-coordinate of upper-left corner
  8         int nRightRect, // x-coordinate of lower-right corner
  9         int nBottomRect, // y-coordinate of lower-right corner
 10         int nWidthEllipse, // height of ellipse
 11         int nHeightEllipse // width of ellipse
 12      );        
 13 
 14     [DllImport("dwmapi.dll")]
 15     public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
 16 
 17     [DllImport("dwmapi.dll")]
 18     public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
 19 
 20     [DllImport("dwmapi.dll")]
 21     public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
 22 
 23     private bool m_aeroEnabled;                     // variables for box shadow
 24     private const int CS_DROPSHADOW = 0x00020000;
 25     private const int WM_NCPAINT = 0x0085;
 26     private const int WM_ACTIVATEAPP = 0x001C;
 27 
 28     public struct MARGINS                           // struct for box shadow
 29     {
 30         public int leftWidth;
 31         public int rightWidth;
 32         public int topHeight;
 33         public int bottomHeight;
 34     }
 35 
 36     private const int WM_NCHITTEST = 0x84;          // variables for dragging the form
 37     private const int HTCLIENT = 0x1;
 38     private const int HTCAPTION = 0x2;
 39 
 40     protected override CreateParams CreateParams
 41     {
 42         get
 43         {
 44             m_aeroEnabled = CheckAeroEnabled();
 45 
 46             CreateParams cp = base.CreateParams;
 47             if (!m_aeroEnabled)
 48                 cp.ClassStyle |= CS_DROPSHADOW;
 49 
 50             return cp;
 51         }
 52     }
 53 
 54     private bool CheckAeroEnabled()
 55     {
 56         if (Environment.OSVersion.Version.Major >= 6)
 57         {
 58             int enabled = 0;
 59             DwmIsCompositionEnabled(ref enabled);
 60             return (enabled == 1) ? true : false;
 61         }
 62         return false;
 63     }
 64 
 65     protected override void WndProc(ref Message m)
 66     {
 67         switch (m.Msg)
 68         {
 69             case WM_NCPAINT:                        // box shadow
 70                 if (m_aeroEnabled)
 71                 {
 72                     var v = 2;
 73                     DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
 74                     MARGINS margins = new MARGINS()
 75                     {
 76                         bottomHeight = 1,
 77                         leftWidth = 1,
 78                         rightWidth = 1,
 79                         topHeight = 1
 80                     };
 81                     DwmExtendFrameIntoClientArea(this.Handle, ref margins);
 82 
 83                 }
 84                 break;
 85             default:
 86                 break;
 87         }
 88         base.WndProc(ref m);
 89 
 90         if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)     // drag the form
 91             m.Result = (IntPtr)HTCAPTION;
 92 
 93     }
 94 
 95     public Form1()
 96     {
 97         m_aeroEnabled = false;
 98 
 99         this.FormBorderStyle = FormBorderStyle.None;
100 
101         InitializeComponent();
102     }
103 }
复制代码

 

posted @   soliang  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
点击右上角即可分享
微信分享提示