带有圆角的窗体边框,格式为 c++建设者XE

发布于 2024-11-10 10:34:49 字数 370 浏览 0 评论 0原文

我借助以下代码制作了一个带圆角的 C++ Builder XE 表单

BorderStyle = bsNone; 

void __fastcall TForm1::FormCreate(TObject *Sender)
{
     HRGN frmrgn;  

     frmrgn = CreateRoundRectRgn (0, 0, ClientWidth, ClientHeight,12,12);
     SetWindowRgn(Handle,frmrgn,true);
}

它看起来很酷但边框丢失了,我尝试了很多方法但没有得到好的结果 所以请帮我绘制颜色 RGB(96,96,96) 的边框

,我想让整个表单可拖动。

I have made a C++ Builder XE form with rounded corner with the help of the following code

BorderStyle = bsNone; 

void __fastcall TForm1::FormCreate(TObject *Sender)
{
     HRGN frmrgn;  

     frmrgn = CreateRoundRectRgn (0, 0, ClientWidth, ClientHeight,12,12);
     SetWindowRgn(Handle,frmrgn,true);
}

It looks cool but the border is missing, I tried many thing but not get good result
so please help me to draw border of color RGB(96,96,96)

And I want to make whole form dragable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如荒岛 2024-11-17 10:34:49

1. 绘制深灰色边框

这个很简单,具体取决于您希望边框看起来有多复杂。如果您只想要深灰色的轮廓,可以使用直线和弧线的组合来绘制它,或者使用 FrameRgn 函数使用特定画笔在您的区域周围绘制轮廓。这样做是最好的解决方案,因为您已经有一个用于定义窗口形状的区域。

但是,SetWindowRgn< 的 MSDN 文档/code>表示,“成功调用 SetWindowRgn 后,系统拥有由区域句柄 hRgn 指定的区域。系统不制作该区域的副本。因此,您不应使用该区域句柄进行任何进一步的函数调用。” 您需要为 Paint 方法再次创建区域。

您的绘图方法的一些代码:

HRGN hRegion = ::CreateRoundRectRgn (0, 0, ClientWidth, ClientHeight,12,12);
Canvas->Brush->Style = bsSolid;
Canvas->Brush->Color = RGB(96, 96, 96);
::FrameRgn(Canvas->Handle, hRegion, Canvas->Brush->Handle, 2, 2);
::DeleteObject(hRegion); // Don't leak a GDI object

2. 使窗口在没有标题栏的情况下可拖动

简短的总结是您需要处理 WM_NCHITTEST 消息。 Windows 发送此消息以查看鼠标是否位于标题栏上(“NC”代表“非客户端”;它实际上是在测试它是否位于非客户端区域中的任何位置,该区域可以是任何窗口边框,而不仅仅是窗口边框) )您可以通过说“是的,鼠标现在位于标题中”来使窗口可拖动,即使事实并非如此。一些代码:

// In the 'protected' section of your form's class declaration
virtual void __fastcall WndProc(Messages::TMessage &Message);

// The implementation of that method:
void __fastcall TForm1::WndProc(Messages::TMessage& Message) {
  TForm::WndProc(Message); // inherited implementation
  if (Message.Msg == WM_NCHITTEST && Msg.Result == htClient) {
    Msg.Result = htCaption;
  }
}

您可以执行一些自己的命中测试,以限制窗口的哪些部分显示为标题栏,以便创建您自己的标题栏。

示例 Delphi 代码。

示例 Delphi 代码。 msdn.com/b/oldnewthing/archive/2011/02/18/10131176.aspx" rel="nofollow">一篇关于使用此消息以及需要注意的事项/不要陷入陷阱的好文章。

1. Painting a dark grey border

This one's easy, depending on how complex you want the border to look. If you just want an outline in dark grey, either draw it using a combination of lines and arcs, or use the FrameRgn function to draw an outline around your region with a specific brush. Doing this is the best solution since you already have a region you've used to define the shape of the window.

However, the MSDN documentation for SetWindowRgn says, "After a successful call to SetWindowRgn, the system owns the region specified by the region handle hRgn. The system does not make a copy of the region. Thus, you should not make any further function calls with this region handle." You'll need to create your region again for the paint method.

Some code for your paint method:

HRGN hRegion = ::CreateRoundRectRgn (0, 0, ClientWidth, ClientHeight,12,12);
Canvas->Brush->Style = bsSolid;
Canvas->Brush->Color = RGB(96, 96, 96);
::FrameRgn(Canvas->Handle, hRegion, Canvas->Brush->Handle, 2, 2);
::DeleteObject(hRegion); // Don't leak a GDI object

2. Making the window draggable without its title bar

The short summary is that you need to handle the WM_NCHITTEST message. Windows sends this to see if the mouse is over the title bar ('NC' stands for 'non-client'; it's actually testing to see if it's anywhere in the non-client area, which can be any window border, not just the top one.) You can make your window draggable by saying 'yes, the mouse is in the caption right now' even when it isn't. Some code:

// In the 'protected' section of your form's class declaration
virtual void __fastcall WndProc(Messages::TMessage &Message);

// The implementation of that method:
void __fastcall TForm1::WndProc(Messages::TMessage& Message) {
  TForm::WndProc(Message); // inherited implementation
  if (Message.Msg == WM_NCHITTEST && Msg.Result == htClient) {
    Msg.Result = htCaption;
  }
}

You can perform some hit testing of your own to restrict what parts of your window appear to be the title bar, in order to create a title bar of your own.

Example Delphi code.

A good article about using this message, and things to be aware of / traps not to fall into.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文