当前位置: 编程技术>.net/c#/asp.net
用 C# Winform做出全透明的磨砂玻璃窗体效果代码
来源: 互联网 发布时间:2014-10-14
本文导语: 首先, 调用系统 API, 这里如果要引用神马的, 就不一一列出了, 大家自己引用一下. 代码如下: [StructLayout(LayoutKind.Sequential)] public struct MARGINS { public int Left; public int Right; public int Top; public int Bottom; } [DllImport("dwmapi.dll", PreserveSig = fa...
首先, 调用系统 API, 这里如果要引用神马的, 就不一一列出了, 大家自己引用一下.
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int Left;
public int Right;
public int Top;
public int Bottom;
}
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmExtendFrameIntoClientArea
(IntPtr hwnd, ref MARGINS margins);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();
然后重写两个事件:
protected override void OnLoad(EventArgs e)
{
if (DwmIsCompositionEnabled())
{
MARGINS margin = new MARGINS();
margin.Right = margin.Left = margin.Bottom = margin.Top = -1;
DwmExtendFrameIntoClientArea(this.Handle, ref margin);
}
base.OnLoad(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
if (DwmIsCompositionEnabled())
{
e.Graphics.Clear(Color.Black);
}
}
最后.. 木有了, F5 运行吧~
代码如下:
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int Left;
public int Right;
public int Top;
public int Bottom;
}
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmExtendFrameIntoClientArea
(IntPtr hwnd, ref MARGINS margins);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();
然后重写两个事件:
代码如下:
protected override void OnLoad(EventArgs e)
{
if (DwmIsCompositionEnabled())
{
MARGINS margin = new MARGINS();
margin.Right = margin.Left = margin.Bottom = margin.Top = -1;
DwmExtendFrameIntoClientArea(this.Handle, ref margin);
}
base.OnLoad(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
if (DwmIsCompositionEnabled())
{
e.Graphics.Clear(Color.Black);
}
}
最后.. 木有了, F5 运行吧~