惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
小众软件
小众软件
I
InfoQ
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Martin Fowler
Martin Fowler
月光博客
月光博客
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
V
Visual Studio Blog
博客园 - 叶小钗
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
GbyAI
GbyAI
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - vidorlee

GitHub退款申请经验分享 C# 如何避免在基类中实现所有接口同时也不让每个子类重复实现 CoreXY运动结构工作原理 在VS2022上安装pygame模块 漫谈Winform控件优化之圆角矩形边框处理 C#中设置自定义控件工具箱图标 讨论TableLayoutPanel加载缓慢和闪烁问题解决方案 关闭Windows 10自动更新的方法 MVVM模式开发WinForm-ReactiveUI C#单例模式的用途 TensorFlow基本使用教程 C#自定义事件的写法 C#多线程Task创建 PropertyChanged.Fody自动通知属性插件 5、Pico Robot 机器人课程 4、Pico Robot 传感器进阶课程 3、Pico Robot 基础开发课程 2、Pico Robot 开发环境搭建 1、Pico Robot 智能小车平台介绍
最佳的实现Winfrom无边框四周阴影方案
vidorlee · 2024-12-07 · via 博客园 - vidorlee

网上不靠谱的东西太多了,都是两边阴影,什么窗口叠加、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 }