2010-10-25 75 views
2

我将一个winform作为对话框显示(在主窗口上使用ShowDialog)。 因此,我将FormBorderStyle设置为None,因为我既不需要控制箱也不需要标题栏。 虽然,我想绘制一个边框(例如像普通窗口一样的蓝色边框)并保留移动窗体的能力。 我不需要调整它的大小。 我试图通过重写OnPaint来绘制边框,但它从不被调用。 这里是我的代码:当FormBorderStyle设置为None时,winforms绘制边框并移动。

protected override void OnPaint (PaintEventArgs e) 
    { 
    base.OnPaint (e); 
    int borderWidth = 2; 
    Color borderColor = Color.Blue; 
    ControlPaint.DrawBorder (e.Graphics, e.ClipRectangle, borderColor, 
     borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, 
     ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, 
     borderColor, borderWidth, ButtonBorderStyle.Solid); 
    } 

任何帮助将不胜感激。

+0

请使用可用来标记代码格式化工具,使得它更容易阅读的问题这样... – JohnoBoy 2010-10-25 09:24:41

+0

你的代码工作正常,当我把它贴到表单中。 – 2010-10-25 09:43:02

+0

我想代码是好的,它只是不叫! – Pierre 2010-10-25 10:04:39

回答

2

Paint方法在这里是错误的,因为它不会绘制窗体的所谓的非客户区域,例如,边框和标题栏。

要隐藏标题栏,您需要将ControlBox属性设置为false并清除表单的Text属性。将边框设置为FixedDialog以使表单不可重新定义。

要保留无标题栏移动窗体的能力,您需要覆盖WndProc

protected override void WndProc(ref Message m) 
{ 
    switch (m.Msg) 
    { 
     case 0x84: m.Result = new IntPtr(0x2); 
      return; 
    } 
    base.WndProc(ref m); 
} 

基本上,这是处理WM_NCHITTEST消息,欺骗,说的标准的方式 - 鼠标光标在窗口的标题[返回值0X2],这样你就可以,即使你单击移动窗体客户区并拖动它。

+0

OnPaint方法没问题,他将FormBorderStyle设置为None。 – 2010-10-25 09:44:11

+0

谢谢,它有帮助。 – Pierre 2010-10-25 10:12:28

+0

虽然移动的代码不起作用,但是案例不存在,Result是IntPtr,它不能被分配为0x2。此外,我想改变边界的大小和颜色。 – Pierre 2010-10-25 10:13:58

0

由于没有更多信息可用,我将按照建议离开边界,将其设置为FixedDialog,将ControlBox属性设置为false并清除表单的文本。 我更喜欢边框的另一种颜色,以及移动窗口的能力。 无论如何非常感谢答案。

+0

要有一个自定义的边框,你需要做你自己的非客户区域计算和绘画。让我说,使用托管代码时有点复杂。也许最好将边框设置为None,然后使用代码或其他(DrawRect或其他)中列出的函数来绘制“边框”(这将是一个假的,看起来只是一个样子)。 – liggett78 2010-11-07 15:21:21

1

我的问题是有一个薄边框可调整大小的窗体。

我设置FormBorderStyle为None

我用一个停靠面板谁包含我的所有控件。

我使用面板填充来设置我的边框宽度。

然后:

Point ResizeLocation = Point.Empty; 
     void panResize_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) { 
       ResizeLocation = e.Location; 
       ResizeLocation.Offset(-panResize.Width, -panResize.Height); 
       if (!(ResizeLocation.X > -16 || ResizeLocation.Y > -16)) 
        ResizeLocation = Point.Empty; 
      } 
      else 
       ResizeLocation = Point.Empty; 
     } 
     void panResize_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left && !ResizeLocation.IsEmpty) { 
       if (panResize.Cursor == Cursors.SizeNWSE) 
        Size = new Size(e.Location.X - ResizeLocation.X, e.Location.Y - ResizeLocation.Y); 
       else if (panResize.Cursor == Cursors.SizeWE) 
        Size = new Size(e.Location.X - ResizeLocation.X, Size.Height); 
       else if (panResize.Cursor == Cursors.SizeNS) 
        Size = new Size(Size.Width, e.Location.Y - ResizeLocation.Y); 
      } 
      else if (e.X - panResize.Width > -16 && e.Y - panResize.Height > -16) 
       panResize.Cursor = Cursors.SizeNWSE; 
      else if (e.X - panResize.Width > -16) 
       panResize.Cursor = Cursors.SizeWE; 
      else if (e.Y - panResize.Height > -16) 
       panResize.Cursor = Cursors.SizeNS; 
      else { 
       panResize.Cursor = Cursors.Default; 
      } 

     } 

     void panResize_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
     { 
      ResizeLocation = Point.Empty; 
     } 
+0

我改变了你的代码,并为我工作。感谢分享! – MiBol 2016-11-18 18:13:44

相关问题