如何移动和调整没有边框的表单?

Joe*_*ani 30 c# resize border winforms formborderstyle

有没有人知道如何在没有边框的情况下调整winform的大小.我不想要Windows的默认边框,所以我将属性"FormBorderStyle"更改为"None".这删除了边框,虽然现在无法调整大小.我已经想出如何移动表单,我只需要知道如何调整它.

Han*_*ant 55

一些允许移动和调整表单大小的示例代码:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      this.FormBorderStyle = FormBorderStyle.None;
      this.DoubleBuffered = true;
      this.SetStyle(ControlStyles.ResizeRedraw, true);
    }
    private const int cGrip = 16;      // Grip size
    private const int cCaption = 32;   // 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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

  • 也许你喜欢Brushes.Chartreuse更好.样式当然完全取决于你,而不是像这样编写代码.如果您不想照顾它,那就不要打扰无边框窗户.或聘请设计师. (9认同)
  • 不需要粗鲁.Havnt看到有一种带有那种颜色的插入刷子.您可以将此信息附加到您的问题中,而不仅仅是复制和粘贴.不管怎么说,还是要谢谢你.用刷子颜色修复它. (5认同)
  • 要删除顶部边框并且仅具有 sizer 手柄,只需删除 OnPaint 方法的最后两行: `rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption); e.Graphics.FillRectangle(Brushes.DarkBlue, rc);` (2认同)
  • 没关系。我的表单中的面板带有“dock: fill”,因此它完全覆盖了客户区域,并且鼠标始终悬停在控件上方,并且从未在客户区域,因此“WM_NCHITTEST”从未触发...只需取消对接就可以了。 (2认同)

use*_*322 17

这是一个包含所有8个调整大小的自定义表单的完整示例:

public partial class Form1 : Form {
public Form1() {
  InitializeComponent();
  this.FormBorderStyle = FormBorderStyle.None; // no borders
  this.DoubleBuffered = true;
  this.SetStyle(ControlStyles.ResizeRedraw, true); // this is to avoid visual artifacts
}

protected override void OnPaint(PaintEventArgs e) // you can safely omit this method if you want
{
    e.Graphics.FillRectangle(Brushes.Green, Top);
    e.Graphics.FillRectangle(Brushes.Green, Left);
    e.Graphics.FillRectangle(Brushes.Green, Right);
    e.Graphics.FillRectangle(Brushes.Green, Bottom);
}

private const int
    HTLEFT = 10,
    HTRIGHT = 11,
    HTTOP = 12,
    HTTOPLEFT = 13,
    HTTOPRIGHT = 14,
    HTBOTTOM = 15,
    HTBOTTOMLEFT = 16,
    HTBOTTOMRIGHT = 17;

const int _ = 10; // you can rename this variable if you like

Rectangle Top { get { return new Rectangle(0, 0, this.ClientSize.Width, _); } }
Rectangle Left { get { return new Rectangle(0, 0, _, this.ClientSize.Height); } }
Rectangle Bottom { get { return new Rectangle(0, this.ClientSize.Height - _, this.ClientSize.Width, _); } }
Rectangle Right { get { return new Rectangle(this.ClientSize.Width - _, 0, _, this.ClientSize.Height); } }

Rectangle TopLeft { get { return new Rectangle(0, 0, _, _); } }
Rectangle TopRight { get { return new Rectangle(this.ClientSize.Width - _, 0, _, _); } }
Rectangle BottomLeft { get { return new Rectangle(0, this.ClientSize.Height - _, _, _); } }
Rectangle BottomRight { get { return new Rectangle(this.ClientSize.Width - _, this.ClientSize.Height - _, _, _); } }


protected override void WndProc(ref Message message)
{
    base.WndProc(ref message);

    if (message.Msg == 0x84) // WM_NCHITTEST
    {
        var cursor = this.PointToClient(Cursor.Position);

        if (TopLeft.Contains(cursor)) message.Result = (IntPtr)HTTOPLEFT;
   else if (TopRight.Contains(cursor)) message.Result = (IntPtr)HTTOPRIGHT;
   else if (BottomLeft.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMLEFT;
   else if (BottomRight.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMRIGHT;

   else if (Top.Contains(cursor)) message.Result = (IntPtr)HTTOP;
   else if (Left.Contains(cursor)) message.Result = (IntPtr)HTLEFT;
   else if (Right.Contains(cursor)) message.Result = (IntPtr)HTRIGHT;
   else if (Bottom.Contains(cursor)) message.Result = (IntPtr)HTBOTTOM;
    }
}}
Run Code Online (Sandbox Code Playgroud)

  • 有点错误。边和角是重叠的,因此例如返回 `new Rectangle(0, 0, this.ClientSize.Width, _);` 的顶部一个必须返回 `new Rectangle(_, 0, this.ClientSize.Width - _, _) );` 为了留下小方块作为角方块...其他边缘也会有相应的偏移 (2认同)

小智 5

"Sizer"是右下角的浅蓝色面板,在 此处输入图像描述

    int Mx;
    int My;
    int Sw;
    int Sh;

    bool mov;

    void SizerMouseDown(object sender, MouseEventArgs e)
    {
        mov = true;
        My = MousePosition.Y;
        Mx = MousePosition.X;
        Sw = Width;
        Sh = Height;
    }

    void SizerMouseMove(object sender, MouseEventArgs e)
    {
        if (mov == true) {
            Width = MousePosition.X - Mx + Sw;
            Height = MousePosition.Y - My + Sh;
        }
    }

    void SizerMouseUp(object sender, MouseEventArgs e)
    {
        mov = false;
    }
Run Code Online (Sandbox Code Playgroud)