改变WinForm边框的风格?

时间:2010-04-10 12:52:29

标签: c# winforms coding-style resize border

是否可以更改WinForm边框的样式?我知道如果边框被删除,它会夺走调整程序大小的功能。因此有没有办法改变它的风格,但保持它可调整大小?

4 个答案:

答案 0 :(得分:6)

您寻求的并不简单,因为边框是由操作系统绘制的。但是,CodePlex上有一个库可以做到这一点。

Drawing Custom Borders in Windows Forms

答案 1 :(得分:1)

首先在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);

然后,使用与此类似的方法。在这种情况下,我的表单只能从右侧调整大小,但应该很容易从任何一侧调整大小:

    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;
        }
    }

答案 2 :(得分:0)

我认为没有直接的方法可以做到这一点。

但是,您可以将表单边框样式设置为None。 并在您的表单中实现调整大小(我认为这很难)

答案 3 :(得分:0)

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:尚未测试

希望您已将FormBorderStyle设置为无