【问题标题】:Custom Form Resizing by Border自定义表单按边框调整大小
【发布时间】:2014-01-24 10:52:45
【问题描述】:

我有一个没有边框和标题栏的自定义表单。我正在使用面板(宽度 = 1px)来模拟边框。一切都很好,除了左边框和上边框。当我尝试减小表单的大小(通过将其拖动到右侧)时,它可以正常工作,但是当表单的大小 == this.MinimumSize 时。它开始向右侧移动。我只想改变大小,而不是移动...... 这是我的leftBorder代码。我如何修改它以仅更改大小?

    private void borderW_MouseDown(object sender, MouseEventArgs e)
    {
        Active = true;

    }


    private void borderW_MouseMove(object sender, MouseEventArgs e)
    {
        if (Active)
        {
            if (e.X < 0)
            {
                this.Location = new Point(this.Left + e.X, this.Top);
                this.Size = new Size(this.Width - e.X, this.Height);
            }
            else
            {
                this.Size = new Size(this.Width - e.X, this.Height);
                this.Location = new Point(this.Left + e.X, this.Top);
            }
        }
    }

    private void borderW_MouseUp(object sender, MouseEventArgs e)
    {
        Active = false;
    }

【问题讨论】:

    标签: c# winforms forms resize


    【解决方案1】:

    将此函数粘贴到您的表单中。它是防止表单移动的覆盖。

    但是,您必须使其符合一个条件,使其仅在您的表单左侧与 form.left + form.width 相同时才处于活动状态(根据我从您的问题中了解到的。

    protected override void WndProc( ref Message m )
    {
    const int WM_NCLBUTTONDOWN = 161;
    const int WM_SYSCOMMAND = 274;
    const int HTCAPTION = 2;
    const int SC_MOVE = 61456;
    if ( (m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE) )
    return;
    if ( (m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION)
    )
    return;
    base.WndProc( ref m );
    }
    

    【讨论】:

    • 不,FormStartPostion 对表单位置有何影响,当我更改位置时(this.Location = new Point(this.Left + e.X, this.Top);)
    • 你是对的。 Startposition 仅在表单加载时产生影响。这个答案:bytes.com/topic/c-sharp/answers/… 指向一个解决方案。如果你的 form.left 等于 form.left + form.width,你可以启动这个 winproc 来防止窗口的移动。
    • 我该如何使用它?我必须将它粘贴到哪里?
    • 这不起作用。也许我不明白您的意思-@“它仅在表单的左侧与 form.left + form.width 相同时才有效”。我必须做这个检查吗?
    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多