用 C# Winform做出全透明的磨砂玻璃窗体效果代码
程序员文章站
2023-08-17 10:37:15
首先, 调用系统 api, 这里如果要引用神马的, 就不一一列出了, 大家自己引用一下. 复制代码 代码如下: [structlayout(layoutkind.seque...
首先, 调用系统 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 运行吧~