1.Win7系统小开启“窗体阴影”效果后,VS2012环境
2.无边框窗体通过SetClassLong、GetClassLongAPI可以实现窗体阴影效果。但该窗体添加至控件中时,阴影效果消失,如何能强制显示阴影效果?
3.自定义的控件如何实现阴影效果?
谢谢!
运行结果:外层窗体(Form1)有阴影效果,内层窗体(f)无阴影效果。
代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//API函数加载,实现窗体边框阴影效果
SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
Form f = new Form();
f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f.BackColor = Color.Peru;
f.Width = 200;
f.Height = 200;
f.Location = new Point(50, 50);
f.TopLevel = false;
this.Controls.Add(f);
f.Show();
SetClassLong(f.Handle, GCL_STYLE, GetClassLong(f.Handle, GCL_STYLE) | CS_DropSHADOW);
}
#region 窗体边框阴影效果变量申明
const int CS_DropSHADOW = 0x20000;
const int GCL_STYLE = (-26);
//声明Win32 API
[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);
#endregion
}