winform(C#)透明方法

1.设置窗体opacity属性:

2.窗体的BackColor和TransparencyKey属性设置相同的值:

2.加using System.Runtime.InteropServices;引用然后加上以下代码:

        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }
        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();
        protected override void OnLoad(EventArgs e)
        {
            if (DwmIsCompositionEnabled())
            {
                MARGINS m = new MARGINS();
                m.Right = m.Left = m.Top = this.Width + this.Height;
                DwmExtendFrameIntoClientArea(this.Handle, ref m);
            }
            base.OnLoad(e);
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            if (DwmIsCompositionEnabled())
            {
                e.Graphics.Clear(Color.Black);
            }
        }
View Code

 

待续...

posted @ 2014-02-17 18:44  China-jin  阅读(1945)  评论(0编辑  收藏  举报