0

I have a window on which I have some blur effects running.
I want this window to be maximized so I set the Window State field to be Maximized in the designer.
But the Window is not maximized and leaves some uncovered area in the top left corner.
Ive tried multiple Start Position settings but none of them solve the problem.

The Settings

enter image description here

The Window

enter image description here

The code for the blurry Window

using System.Runtime.InteropServices;

namespace WF4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.EnableBlur();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.LimeGreen;
            TransparencyKey = Color.LimeGreen;
            InitializeComponent();
            FormBorderStyle = FormBorderStyle.None;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MessageBox.Show("Hllo");
            }
        }

    }
    public static class WindowExtension
    {
        [DllImport("user32.dll")]
        static internal extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);

        public static void EnableBlur(this Form @this)
        {
            var accent = new AccentPolicy();
            accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;
            var accentStructSize = Marshal.SizeOf(accent);
            var accentPtr = Marshal.AllocHGlobal(accentStructSize);
            Marshal.StructureToPtr(accent, accentPtr, false);
            var Data = new WindowCompositionAttributeData();
            Data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;
            Data.SizeOfData = accentStructSize;
            Data.Data = accentPtr;
            SetWindowCompositionAttribute(@this.Handle, ref Data);
            Marshal.FreeHGlobal(accentPtr);
        }

    }
    enum AccentState
    {
        ACCENT_DISABLED = 0,
        ACCENT_ENABLE_GRADIENT = 1,
        ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
        ACCENT_ENABLE_BLURBEHIND = 3,
        ACCENT_INVALID_STATE = 4
    }

    struct AccentPolicy
    {
        public AccentState AccentState;
        public int AccentFlags;
        public int GradientColor;
        public int AnimationId;
    }

    struct WindowCompositionAttributeData
    {
        public WindowCompositionAttribute Attribute;
        public IntPtr Data;
        public int SizeOfData;
    }

    enum WindowCompositionAttribute
    {
        WCA_ACCENT_POLICY = 19
    }

}
  • Make sure your app is DpiAware and the Form scales to Dpi. -- A border-less Form still has borders, (7+1 pixels), so consider this when you position that window. You can use the current Screen working area as measure (since your app will be DpiAware, you'll be able to read non-virtualized values) and position your Form accordingly -- The notes here may help: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103) – Jimi Dec 07 '21 at 11:15
  • 2
    Such is the misery when using undocumented winapi functions. Try to get ahead by setting the FormBorderStyle property to None and calling EnableBlur() *after* InitializeComponent(). – Hans Passant Dec 07 '21 at 11:42

1 Answers1

1

I tested Hans's suggestion and it worked well.

The solution is to set the FormBorderStyle property to None.

enter image description here

Make sure the background color is Control.

enter image description here

Calling EnableBlur() after InitializeComponent():

InitializeComponent();
this.EnableBlur();

Output:

enter image description here

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21
  • I have made an answer. If you don't mind, you could click '✔' to mark my reply as the accepted answer. It will also help others to solve the similar issue. – Jiale Xue - MSFT Dec 16 '21 at 08:18