Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the style of WinForm border?

Is it possible to change the style of a WinForm border? I know that if the border is removed, it takes away the functionality to resize the program. Therefore is there a way to change the style of it, but keep it resizable?

like image 554
Joey Morani Avatar asked Feb 02 '26 06:02

Joey Morani


2 Answers

What you seek is not simple because the border is drawn by the operating system. However, there is a library on CodePlex that does make possible to do this very thing.

Drawing Custom Borders in Windows Forms

like image 197
Thomas Avatar answered Feb 04 '26 18:02

Thomas


First write this in the InitializeComponent():

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_RIGHT = 0xB;

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();

this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Resize_Form);

Then, use a method similar to this. In this case, my form is only resizable from the right side, but should be easy to make it resize from any side:

    private void Resize_Form(object sender, MouseEventArgs e)
    {
        if ((e.Button == MouseButtons.Left) && (MousePosition.X >= this.Location.X + formWidth - 10))
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.SizeWE;
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_RIGHT, 0);
            formWidth = this.Width;
        }
    }
like image 41
Steve Avatar answered Feb 04 '26 20:02

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!