[开发笔记]-实现winform半透明毛玻璃效果

亲测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下的效果:

转载请注明出处。

原文地址:https://www.cnblogs.com/babycool/p/3724842.html