Change the style of WinForm border?

15,073

Solution 1

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

Solution 2

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;
        }
    }
Share:
15,073
Joey Morani
Author by

Joey Morani

Updated on June 04, 2022

Comments

  • Joey Morani
    Joey Morani almost 2 years

    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?

  • anonymous
    anonymous about 14 years
    Resizing might not be trivial because the cursor no longer changes to the size cursor when you move the mouse to the edges of your form.
  • Joey Morani
    Joey Morani about 14 years
    Could I maybe put a panel on my form, like the image above. And then have that resize my form when someone resizes the panel? I could make the panel Anchor to the top, bottom, left and right so it would always be the same size as the form. Do you know any code I could use to do this?
  • SysAdmin
    SysAdmin about 14 years
    @MrRoys - changing cursor will be the most easiest thing one could ever do.
  • SysAdmin
    SysAdmin about 14 years
    @Dodi300 - you could do as you suggested. but you dont need that. you could eazily draw a rectangle on the form and check if the mouse pos overlaps the rectangle in MouseMove Event of the form. If it overlaps change the cursor and perform the logic to resize the form
  • Joey Morani
    Joey Morani about 14 years
    Please could you give some example code? How would I get the form to resize? Thanks.
  • anonymous
    anonymous about 14 years
    @SysAdmin - oops, didn't make myself clear - was referring to the manual resizing code, not the changing of the cursor - @Veer's and your suggestion sound good though.