Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to design a custom close, minimize and maximize button in windows form application?

Tags:

c#

winforms

I am creating a windows form application project in which I want a custom border and three buttons(close, minimize and maximize) of my own design. I have no idea how to do it and I'm not even sure if it is possible. But if its possible please let me know the solution. Thanks

like image 609
Karedia Noorsil Avatar asked Mar 13 '15 04:03

Karedia Noorsil


People also ask

Which buttons are used to minimize maximize and close the program?

Minimizing, maximizing, and restoring windowsThe Minimize button is among the three buttons at the right end of the title bar. This button has a small dash (or minus sign). The Minimize button shrinks the window and places it on the taskbar while leaving the program running.

How do you remove minimize and maximize button in Windows form?

To remove the minimize, maximize, and close buttons from your form, open your form in Design View. Under the View menu, select Properties. When the Properties window appears, set the "Control Box" property to "No". Now when the form is opened, the buttons will no longer appear in the top right of the form.


1 Answers

Yes, it's possible without additional libraries.

First, hide the window's original border.

public Form1()
{
    InitializeComponent();

    FormBorderStyle = FormBorderStyle.None;
}

Next, create a panel, or whatever you want really, with your three buttons (I know it's ugly, for demo purposes):

enter image description here

Then, assign the correct action to each of them, using the WindowState:

private void minimizeButton_Click(object sender, System.EventArgs e)
{
    WindowState = FormWindowState.Minimized;
}

private void maximizeButton_Click(object sender, System.EventArgs e)
{
    WindowState = FormWindowState.Maximized;
}

private void closeButton_Click(object sender, System.EventArgs e)
{
    Close();
}

Finally, make the form draggable with our panel. Add those at the class level:

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

and plug them in a MouseDown event of the panel:

private void OnMouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    }
}

And now you have a draggable form, with your own bar at the top.

If you want it to be resizable, as @PhilWright mentionned, you can trap the WM_NCHITTEST message from WndProc and return HTBOTTOMRIGHT to trigger the resizing :

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x84)
    { 
        const int resizeArea = 10;
        Point cursorPosition = PointToClient(new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16));
        if (cursorPosition.X >= ClientSize.Width - resizeArea && cursorPosition.Y >= ClientSize.Height - resizeArea )
        {
            m.Result = (IntPtr)17;
            return;
        }
    }

    base.WndProc(ref m);
}

As @BenVoigt mentionned, you can use the Dock and Anchor properties on the buttons/panel so they can resize properly. If you don't, they will not follow the form on resize.

like image 143
Pierre-Luc Pineault Avatar answered Nov 15 '22 07:11

Pierre-Luc Pineault