How do I create a blurred-glass effect on a border-less Form?

后端 未结 2 962
执笔经年
执笔经年 2021-02-01 11:16

How do I draw a smooth blurred-glass effect on a border-less Form? I have tried the code listed on Image Processing for Dummies with C and GDI+ page but I\'m sure it\'s not what

2条回答
  •  余生分开走
    2021-02-01 11:49

    Winform

    Using Win API like DwmEnableBlurBehindWindow

        [DllImport("gdi32")]
        private static extern IntPtr CreateEllipticRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
        [DllImport("dwmapi")]
        private static extern int DwmEnableBlurBehindWindow(IntPtr hWnd, ref DwmBlurbehind pBlurBehind);
        public struct DwmBlurbehind
        {
            public int DwFlags;
            public bool FEnable;
            public IntPtr HRgnBlur;
            public bool FTransitionOnMaximized;
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            var hr = CreateEllipticRgn(0, 0, Width, Height);
            var dbb = new DwmBlurbehind {FEnable = true, DwFlags = 1, HRgnBlur = hr, FTransitionOnMaximized = false};
            DwmEnableBlurBehindWindow(Handle, ref dbb);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, Width, Height));
        }
    

    Example 1:

    enter image description here

    Example 2: You can change the brush color (ex: DarkRed) to get nice effects

    enter image description here

    Thing to note here is that you can pick a region where this applies and you can have more then 1.

    Wpf

    You can use the same approach.

    Wpf has much better support for shaders and you can add effect like blur, but thy are more performance heavy. Also your image is most likely from a program created by WPF and Blend.

    If i remember correctly you can not use shaders like blur on solid color brush. Therefor following would not give the same effect. Set background 50% see through (Window properties AllowsTransparency="True" and WindowStyle="None")

    
        
    
    

    Native glass windows

    Wpf support has been described here: http://www.paulrohde.com/native-glass-with-wpf/

    I got following result with slightly different approach: enter image description here

提交回复
热议问题