How to disable Form Resize on Title Bar Double-Click in C#

By FoxLearn 1/17/2025 2:06:05 AM   455
To prevent the form from being resized or restored when a user double-clicks on the title bar (or when the title bar is clicked), you can use the WndProc method to intercept and handle Windows messages directly within your form.

This method is where you can prevent certain default behaviors, such as resizing or moving the form when double-clicking the title bar (or in the non-client area of the form). In this article, I will show you how to winforms disable resize in c#.

Why Disable Resize in WinForms?

By default, a c# form is resizable, allowing users to drag and adjust the form's size. However, in certain cases, such as when you need a fixed layout or specific design constraints, this feature may not be desirable. In such instances, you can winform disable resize and lock the form's size.

How to Prevent Form Resize/Restore on Title Bar Double-Click in C#?

To prevent the user from changing the window's state (such as minimizing, maximizing, or resizing), you can modify the form's properties and override certain behaviors.

Windows Form Disable Resize

To make your winforms disable resize, you can set the FormBorderStyle to None, remove the control box (minimize, maximize, close buttons), and handle the WM_SYSCOMMAND message to block certain system commands (like minimizing or maximizing).

this.MaximizeBox = false;

If you need certain controls to remain fixed while resizing is disabled, you can use the Anchor or Dock properties to control how controls adjust within the form.

The user can't move, resize, or restore the form by interacting with the title bar. The form remains borderless, without a control box, and cannot be resized.

Steps to WinForms Disable Resize in C#

There are several ways to disable resize for your Windows Form. Below is a simple and effective method using the WndProc method to intercept messages related to resizing.

To disable resizing in your WinForms on Title Bar Double-Click, you need to open your code-behind and then override the WndProc method as shown below.

For example, c# form disable resize

// Override the WndProc method to intercept Windows messages - winform disable resize
protected override void WndProc(ref Message msg)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MOVE = 0xF010;
    const int WM_NCLBUTTONDBLCLK = 0x00A3; // Message for double-clicking the title bar
    switch (msg.Msg)
    {
        case WM_SYSCOMMAND:
            int command = msg.WParam.ToInt32() & 0xfff0;
            if (command == SC_MOVE)
                return; // Prevent moving the form
            break;
    }
    // Intercept double-click on the title bar (non-client area)
    if (msg.Msg == WM_NCLBUTTONDBLCLK)
    {
        msg.Result = IntPtr.Zero; // Prevent restoring/resizing the form
        return;
    }
    // Call the base class to handle all other messages
    base.WndProc(ref msg);
}

In this example:

  • The WndProc method is where we handle Windows messages sent to the form. We override it to intercept the WM_SYSCOMMAND and WM_NCLBUTTONDBLCLK messages.
  • When a user tries to move the form (e.g., by clicking and dragging the title bar), the WM_SYSCOMMAND message is sent with the SC_MOVE command. We check for this and prevent the form from being moved by returning early.
  • WM_NCLBUTTONDBLCLK is triggered when the user double-clicks the non-client area (the title bar of the form). By setting m.Result = IntPtr.Zero, we prevent the form from being resized or restored to its normal state.

Another way to disable resize is by adjusting the form’s FormBorderStyle property. By setting it to FixedDialog, FixedSingle, or another fixed style, you prevent the user from resizing the form through the window’s edges.

this.FormBorderStyle = FormBorderStyle.FixedDialog;

This solution works well if you want to completely lock the form's size and remove the resizing handle from the window.

You can disable or customize the double-click behavior on a title bar, including preventing the window from maximizing, this works with any FormBorderStyle. Whether you choose to use the WndProc method or adjust the FormBorderStyle property, you can effectively winform disable resize and ensure your Windows Form stays at a fixed size.

C# Form Resize

When you disable resize in a c# form, keep in mind that users won’t be able to adjust the form’s dimensions manually. However, you can still control the form's size programmatically using the Width and Height properties.

this.Width = 800;
this.Height = 600;

This way, even though resizing is disabled, you can set the size of the c# form as needed.