Search code examples
c#.netwinformswndproc

Prevent Form resize on SW_MAXIMIZE


I want to simulate Aero Snap functionality by my own. And for stick Form to a screen's side I'm using solution from this question. The problem is that after I call:

ShowWindow(Handle, SW_MAXIMIZE);

Form immediately maximizes and after call MoveWindow changes it's size to needed size. And this jump of the Form is visible and annoying. To prevent it I tried to disable handling of messages WM_GETMINMAXINFO, WM_SIZE, WM_MOVE, WM_NCCALCSIZE, WM_WINDOWPOSCHANGING, WM_WINDOWPOSCHANGED in WndProc. It helps but not completely. Is there any analog SuspendLayout()/ResumeLayout() for Form resizing?


Solution

  • Disabling message handling in WndProc helped me to reduce flickering (all except WM_NCPAINT):

    bool ignoreMessages = false;
    
    public void DockWindow()
    {
        ignoreMessages = true;
        ShowWindow(handle, SW_MAXIMIZE);
        ignoreMessages = false;
        MoveWindow(handle, 0, 0, Screen.PrimaryScreen.WorkingArea.Width / 2, Screen.PrimaryScreen.WorkingArea.Height, true);
    }
    
    protected override void WndProc(ref Message message)
    {
        if (ignoreMessages &&
            message.Msg != WM_NCPAINT)
            return;
    
        base.WndProc(ref message);
    }