的WinForms绘制边框和移动时FormBorderStyle设置为无 [英] winforms draw border and move when FormBorderStyle set to None

查看:295
本文介绍了的WinForms绘制边框和移动时FormBorderStyle设置为无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示一个winform为(通过主窗口的ShowDialog)的对话框。
所以,我的FormBorderStyle设置为无,因为我想无论是控制箱也没有标题栏。
虽然,我想画的边框(例如蓝色边框像正常视窗),并保持移动形式的能力。
我并不需要调整它的能力。
我试图通过重写的OnPaint绘制边框,但它永远不会被调用。
这里是我的代码:

 保护覆盖无效的OnPaint(PaintEventArgs的E)
{
base.OnPaint(E);
INT边框宽度= 2;
颜色BORDERCOLOR = Color.Blue;
ControlPaint.DrawBorder(e.Graphics,e.ClipRectangle,BORDERCOLOR,
边框宽度,ButtonBorderStyle.Solid,BORDERCOLOR,边框宽度,
ButtonBorderStyle.Solid,BORDERCOLOR,边框宽度,ButtonBorderStyle.Solid,
BORDERCOLOR,边框宽度,ButtonBorderStyle.Solid);
}



任何帮助将不胜感激。


< DIV CLASS =h2_lin>解决方案

油漆的方法是错在这里,因为它不画的所谓的非客户区形式,例如:边框和标题栏。



要隐藏标题栏,你需要设置控制盒属性和清除表单的文本属性。设置边境 FixedDialog 来使窗体unresizable。



要保留移动窗体没有标题的能力酒吧,你需要重写的WndProc

 保护覆盖无效的WndProc(参考消息M)
{
开关(m.Msg)
{
案的0x84:m.Result =新的IntPtr(0X2);
的回报;
}
base.WndProc(REF米);
}



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


I display a winform as a dialog (with ShowDialog over a main window). So, I set the FormBorderStyle to None because I wanted neither the control boxes nor the title bar. Though, I would like a border drawn (for example a blue border like normal windows) and keep the ability to move the form. I don't need the ability to resize it. I tried to draw a border by overriding OnPaint but it is never called. Here is my code :

  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); 
  }

Any help would be greatly appreciated.

解决方案

The Paint method is wrong here since it does not paint the so-called non-client area of the form, e.g. the border and the title bar.

To hide the title bar, you need to set the ControlBoxproperty to false and clear the form's Text property. Set the border to FixedDialog to make the form unresizable.

To retain the ability to move the form without the title bar, you need to override WndProc.

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

Basically this is the standard way of handling WM_NCHITTEST message and cheating, saying - the mouse cursor is on the window's caption [return value 0x2], so you will be able to move your form even if you click in the client area and drag it.

这篇关于的WinForms绘制边框和移动时FormBorderStyle设置为无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆