Change the style of WinForm border?

2020-03-25 07:16发布

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?

4条回答
劳资没心,怎么记你
2楼-- · 2020-03-25 07:51

I dont think there is a direct way to do this.

But, you could set the form border style to None. And implement the resizing in your form(which I don't think its difficult)

查看更多
冷血范
3楼-- · 2020-03-25 08:06

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;
        }
    }
查看更多
Evening l夕情丶
4楼-- · 2020-03-25 08:10

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

查看更多
We Are One
5楼-- · 2020-03-25 08:10
string position = String.Empty;
Point mouseDownPosition = new Point();

private void myForm_MouseDown(object sender, MouseEventArgs e)
{
    position = (e.X == 0) ? "Left" : ((e.X == myForm.Width) ? "Right" : String.Empty;
    position += (e.Y == 0) ? "Top" : ((e.Y == myForm.Height) ? "Bottom" : String.Empty;
    if(position != String.Empty)
    {
        mouseDownPosition = e.Location;
    }
}

private void myForm_MouseMove(object sender, MouseEventArgs e)
{
    if(position != String.Empty)
    {        
        Point movementOffset = new Point(e.Location.X - mouseDownPosition.X, e.Location.Y - mouseDownPosition.Y);
        Switch(position)
        {
            Case "LeftTop":
                myForm.Location.X += movementOffset.X;
                myForm.Location.Y += movementOffset.Y;
                myForm.Width -= movementOffset.X;
                myForm.Height -= movementOffset.Y;
            Case "Left":
                myForm.Location.X += movementOffset.X;
                myForm.Width -= movementOffset.X;
            // Complete the remaining please :)
        }
    }
}

private void myForm_MouseUp(object sender, MouseEventArgs e)
{
    position = String.Empty;
}

P.S: Have not yet tested it

Hope you've set FormBorderStyle to None

查看更多
登录 后发表回答