[DllImport("dwmapi.dll")]
private static extern void DwmIsCompositionEnabled(ref int enabledptr);
[DllImport("dwmapi.dll")]
private static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margin);
Step Two:定义区域这个结构体
public struct MARGINS
{
public int m_Left;
public int m_Right;
public int m_Top;
public int m_Buttom;
};
Step Three:在构造方法中加入如下代码:
//判断是否Vista及以上的系统
if (System.Environment.OSVersion.Version.Major >= 6)
{
DwmIsCompositionEnabled(ref en); //检测Aero是否为打开
if (en > 0)
{
DwmExtendFrameIntoClientArea(this.Handle, ref mg); //透明
}
}
this.Paint += new PaintEventHandler(Form1_Paint);//给窗口画制添加事件函数Form1_Paint
Step Four:
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (en > 0)
{
Graphics g = e.Graphics;
Color myColor = Color.FromArgb(131,175,155);//自定义一个舒服的色号
SolidBrush bsh = new SolidBrush(myColor);
g.FillRectangle(bsh, this.ClientRectangle);
// e.Graphics.Clear(Color.White); 也可以直接使用清楚函数来填充
bsh.Dispose();
}
}
---------------------------------------------------------------------------------------------------------------------------------
实现的效果如下: