C# WinForm 调用 DWMAPI.dll 实现无边框窗体 Aero 边缘阴影 DropShadow 效果

C# WinForm FormBorderStyle.None 无边框窗体程序,通过调用 DWMAPI.dll 来实现 原生 Aero DropShow 边缘阴影的效果。

Form1.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None; // 设置为 无边框窗体
}

public const int WM_NCPAINT = 0x0085;

[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);

[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

public struct Margins
{
public int Bottom;
public int Left;
public int Right;
public int Top;
}

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCPAINT:
DropShadowToWindow(m.HWnd);
break;
}
base.WndProc(ref m);
}

public static bool DropShadowToWindow(IntPtr hwnd)
{
try
{
int val = 2;
int ret1 = DwmSetWindowAttribute(hwnd, 2, ref val, 4);

if (ret1 == 0)
{
Margins m = new Margins { Bottom = 1, Left = 0, Right = 0, Top = 0 };
int ret2 = DwmExtendFrameIntoClientArea(hwnd, ref m);
return ret2 == 0;
}
else
{
return false;
}
}
catch
{
// Probably dwmapi.dll not found (incompatible OS)
return false;
}
}
}
}
本站文章除注明转载外均为原创,未经允许不要转载哇. ヾ(゚ー゚ヾ) http://qwqaq.com/5e466d74.html
分享到