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!

Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90

6 Answers6

33

You could set the MaximizeBox property of the form to false

ionden
  • 12,536
  • 1
  • 45
  • 37
  • 1
    I didn't think to do this, because the clickable 'maximize box' button was already hidden in this borderstyle. – Isaac Bolinger Mar 06 '12 at 17:23
  • 5
    Dont help with doubleclick for me – Petr Nov 20 '12 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 Sep 21 '14 at 09: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 Sep 19 '20 at 21:09
24

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!

LazyZebra
  • 1,097
  • 16
  • 24
  • 1
    This is the actual answer, just removing the maximise box or controls will not prevent the event. – tfcmad Mar 04 '22 at 15:26
9

/// /// 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);
    }
ismail
  • 91
  • 1
  • 1
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;
}
Gopichandar
  • 2,742
  • 2
  • 24
  • 54
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

Prakash Joshi
  • 572
  • 3
  • 10
  • 25
Ash18
  • 121
  • 3
  • 12
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.

NetXpert
  • 511
  • 5
  • 14