【问题标题】:Windows Forms Move Borderless WindowWindows 窗体移动无边框窗口
【发布时间】:2020-12-07 13:20:01
【问题描述】:

我有一个 Windows 窗体应用程序,它应该是无边界的(即使没有标题栏)并且可以调整大小和移动。

到目前为止,我已将 BorderStyle 设置为“无”,这会删除所有边框,让我的程序看起来很漂亮。

现在我添加了以下内容的隐形边框:

    private const int cGrip = 16;      // Grip size
    private const int cCaption = 50;   // Caption bar height;

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
        rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
        //e.Graphics.FillRectangle(Brushes.DarkBlue, rc);

    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84)
        {  // Trap WM_NCHITTEST
            Point pos = new Point(m.LParam.ToInt32());
            pos = this.PointToClient(pos);
            if (pos.Y < cCaption)
            {
                m.Result = (IntPtr)2;  // HTCAPTION
                return;
            }
            if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
            {
                m.Result = (IntPtr)17; // HTBOTTOMRIGHT
                return;
            }
        }

        base.WndProc(ref m);
    }

Program Layout

蓝色矩形通过 OnPaint() 方法呈现,并显示用户可以在按住鼠标左键时移动窗口的字段。

我的问题是,这个矩形在我的标签下方。 有谁知道如何获取标签前面的矩形?

另一种方法是禁用变成深灰色的标签。 如果我可以更改禁用标签的颜色,您将解决我的问题。

【问题讨论】:

  • 标签的BackColor 是什么?您可能应该尝试改写您对问题的描述。我也不确定我是否理解
  • 看起来很眼熟,这里需要注明出处。不要使用标签,在 OnPaint() 中使用 TextRenderer.DrawText()。
  • 感谢您的建议,即使不是所有人都明白我问了什么@41686d6564

标签: c# winforms


【解决方案1】:

让标签保持启用状态,以便拖动标签会导致表单也被拖动:

public const int HT_CAPTION = 0x2;
public const int WM_NCLBUTTONDOWN = 0xA1;

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

private void label1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多