親測win7下可用,win8下由於系統不支持Aero效果,所以效果不是半透明的。
代碼:
博客園插入不了代碼了。。。。。
public partial class Form1 : Form { int en; public struct MARGINS { public int m_Left; public int m_Right; public int m_Top; public int m_Buttom; }; [DllImport("dwmapi.dll")] private static extern void DwmIsCompositionEnabled(ref int enabledptr); [DllImport("dwmapi.dll")] private static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margin); public Form1() { InitializeComponent(); en = 0; MARGINS mg = new MARGINS(); //定義透明擴展區域的大小,這里全部-1,即全部透明 mg.m_Buttom = -1; mg.m_Left = -1; mg.m_Right = -1; mg.m_Top = -1; //判斷是否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); } private void Form1_Paint(object sender, PaintEventArgs e) { if (en > 0) { Graphics g = e.Graphics; SolidBrush bsh = new SolidBrush(Color.Black); g.FillRectangle(bsh, this.ClientRectangle); bsh.Dispose(); } } private void Form1_Load(object sender, EventArgs e) { } }
win7下的效果為半透明毛玻璃效果,win8下的效果:
轉載請注明出處。