[StructLayout(LayoutKind.Sequential)]
public struct MARGINS{
public int Left;
public int Right;
public int Top;
public int Bottom;
}
[DllImport("dwmapi.dll", PreserveSig = false)]
static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
[DllImport("dwmapi.dll", PreserveSig = false)]
static extern bool DwmIsCompositionEnabled(); //Dll 导入 DwmApi
protected override void OnLoad(EventArgs e)
{
//如果启用Aero
if (DwmIsCompositionEnabled())
{
MARGINS m = new MARGINS();
m.Right = -1; //设为负数,则全窗体透明
DwmExtendFrameIntoClientArea(this.Handle, ref m); //开启全窗体透明效果
}
base.OnLoad(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
if (DwmIsCompositionEnabled())
{
e.Graphics.Clear(Color.Black); //将窗体用黑色填充(Dwm 会把黑色视为透明区域)
}
}