1、添加引用
using System.Runtime.InteropServices;
2、定义常量值及函数
private const int CS_DropSHADOW = 0x20000;
private const int GCL_STYLE = (-26);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassLong(IntPtr hwnd, int nIndex);
3、在你的窗体Load或构造事件中调用这个函数即可
SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
完整代码如下:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ShadowDemo
{
public partial class Frm_Main : Form
{
private const int CS_DropSHADOW = 0x20000;
private const int GCL_STYLE = (-26);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassLong(IntPtr hwnd, int nIndex);
public Frm_Main()
{
InitializeComponent();
// 在构造中调用此函数
SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
}
}
}