用 C# Winform 做出全透明的磨砂玻璃窗体
时间:2011-05-08 来源:yaob
就是一个简单的例子, 调用系统的 dwm 去重绘窗口. 只能在 Vista 和 7 之后才可以, 并且要确保已经开启主题服务等等, 总之不是非常实用, 好玩而已.
首先, 调用系统 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 运行吧~
相关阅读 更多 +