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);
}
}
}
本文介绍如何使用C#为Windows窗体添加阴影边框效果。通过调用user32.dll中的API函数并设置特定的窗口样式,可以轻松地为应用程序的窗体增加美观的阴影效果。
1020

被折叠的 条评论
为什么被折叠?



