19

I'm annoyed that the I'm promised a fixed window that the user can't resize, but then of course they're allowed to double click the title bar to maximize this 'unresizable' window. How can I turn this off? Can I do it with winforms code, or must I go down to Win32?

Thanks!

6 Answers 6

34

You could set the MaximizeBox property of the form to false

4
  • 1
    I didn't think to do this, because the clickable 'maximize box' button was already hidden in this borderstyle. Commented Mar 6, 2012 at 17:23
  • 5
    Dont help with doubleclick for me
    – Petr
    Commented Nov 20, 2012 at 18:26
  • 2
    This answer is also useful when you removed all buttons from a Form with ControlBox = false. Need to set both to do it right.
    – ygoe
    Commented Sep 21, 2014 at 9:35
  • 2
    Frustratingly, this only hides the specified box/control. It doesn't remove or disable the underlying functionality and double-clicking the title-bar will still function normally.
    – NetXpert
    Commented Sep 19, 2020 at 21:09
25

You can disable the double-click message on a title bar in general (or change the default behavior which is maximizing the window). it works on any FormBorderStyle:

private const int WM_NCLBUTTONDBLCLK = 0x00A3; //double click on a title bar a.k.a. non-client area of the form

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCLBUTTONDBLCLK)
            {
                m.Result = IntPtr.Zero;
                return;
            }
            base.WndProc(ref m);
        }

MSDN Source

Cheers!

1
  • 1
    This is the actual answer, just removing the maximise box or controls will not prevent the event.
    – tfcmad
    Commented Mar 4, 2022 at 15:26
11

/// /// This is we are overriding base WIN32 window procedure to prevent the form from being moved by the mouse as well as resized by the mouse double click. /// ///

    protected override void WndProc(ref Message m)
    {
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_MOVE = 0xF010;
        const int WM_NCLBUTTONDBLCLK = 0x00A3; //double click on a title bar a.k.a. non-client area of the form

        switch (m.Msg)
        {
            case WM_SYSCOMMAND:             //preventing the form from being moved by the mouse.
                int command = m.WParam.ToInt32() & 0xfff0;
                if (command == SC_MOVE)
                    return;
                break;
        }

       if(m.Msg== WM_NCLBUTTONDBLCLK)       //preventing the form being resized by the mouse double click on the title bar.
        {
            m.Result = IntPtr.Zero;                   
            return;                   
        }

        base.WndProc(ref m);
    }
0
3

I know I'm late to the party, May help someone who is searching for the same.

private const int WM_NCLBUTTONDBLCLK = 0x00A3; //double click on a title bar a.k.a. non-client area of the form

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{

    switch (msg)
    {                
        case WM_NCLBUTTONDBLCLK:    //preventing the form being resized by the mouse double click on the title bar.
            handled = true;
            break;                
        default:
            break;
    }
    return IntPtr.Zero;
}
3

I just checked it in VB.Net. Below code worked for me.

Private Const Win_FormTitleDoubleClick As Integer = 163

Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = Win_FormTitleDoubleClick Then
        m.Result = IntPtr.Zero
        Return
    End If
    MyBase.WndProc(m)
End Sub

Note: 163 is the event code

0

I approached the problem slightly differently. First I removed the minimize and maximize options from the control box via the MaximizeBox and MinimizeBox properties as you'd expect.

Then I added the following OnResizeEnd() event and attached it to the Form's ResizeEnd event handler:

/// <summary>Locks the form to fill the screen that it's placed on and remain in that state as long as it's open.</summary>
private void GuiMain_ResizeEnd( object sender, EventArgs e )
{
    this.Location = Screen.WorkingArea.Location;
    this.Size = Screen.WorkingArea.Size;
    this.MaximizedBounds = Screen.WorkingArea;
    this.MinimumSize = Screen.WorkingArea.Size;
    this.WindowState = FormWindowState.Normal;
}

This solution necessarily relies on the existence of the following accessor which you can copy, or you can simply replace each instance of Screen. in the above with Screen.FromHandle( this.Handle ).

protected Screen Screen => Screen.FromHandle( this.Handle );

Obviously, if ironically, this actually keeps the form in the FormWindowState.Normal state, but it mimics the effect of maximization, and resets this fullscreen state after any attempt to change it.

Interestingly, due to the use of the Screen.FromHandle() settings (as opposed to hard-coded ones), you can actually drag the form from one display to another, whereupon it immediately "snaps" to fill THAT screen instead. Something I found quite handy, but which may require additional code to correct if you don't want that functionality in your application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.