改变WinForm边框的样式?

发布于 2024-08-28 12:58:09 字数 78 浏览 7 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

噩梦成真你也成魔 2024-09-04 12:58:09

你所寻求的并不简单,因为边界是由操作系统划定的。然而,CodePlex 上有一个库确实可以做到这一点。

在 Windows 窗体中绘制自定义边框

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

没︽人懂的悲伤 2024-09-04 12:58:09

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

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;
        }
    }
放肆 2024-09-04 12:58:09

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

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

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)

我只土不豪 2024-09-04 12:58:09
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;
}

PS:尚未测试

希望您已将 FormBorderStyle 设置为 None

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文