如何在 win32 中创建带圆角的可调整大小的窗口

发布于 2024-08-21 00:05:51 字数 229 浏览 14 评论 0原文

我正在尝试创建一个具有圆角并且可以水平和垂直调整大小的 Win32 窗口。我的第一种方法是创建圆角矩形的位图并将其绘制到屏幕上,同时将窗口样式设置为 WS_EX_LAYERED 并将透明度键设置为圆角矩形的外部颜色。

这可行,但只有当窗口大小固定时才看起来不错。如果我允许用​​户调整窗口大小并随之拉伸位图,圆角看起来也会被拉伸。有没有办法创建一个具有漂亮、光滑的圆角并且可以在窗口中调整大小的窗口?最后,我还想为窗口添加阴影。

I'm trying to create a Win32 window that has rounded corners and is resizable both horizontally and vertically. My first approach was to create BITMAP of a rounded rectangle and draw it to the screen in conjunction with setting the windows style to WS_EX_LAYERED and setting the transparency key to the outside color of the rounded rectangle.

This works, but it only looks good if the window is a fixed size. If I allow the user the resize the window and stretch the bitmap along with it, the rounded corners look stretched out, too. Is there a way to create a window that has nice, smooth rounded corners and is resizable in windows? Eventually, I would also like to add a drop shadow to the window as well.

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

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

发布评论

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

评论(3

此刻的回忆 2024-08-28 00:05:51

我会尝试混合 Windows API 函数 CreateRoundRectRgnSetWindowRgn。可以在 pInvoke.net 网站 找到一个非常简单的示例。

I would try to mix the Windows API functions CreateRoundRectRgn and SetWindowRgn. A very simple example can be found at pInvoke.net web site.

雪若未夕 2024-08-28 00:05:51

您可以创建一个没有任何框架的窗口,使用 WS_EX_LAYERED 获得透明度,然后在 WM_PAINT 中“正常”绘制包含自定义框架的窗口,或者编写一个离屏位图,并使用 UpdateLayeredWindow (后一种方法更有效)。

当然,您必须将绘制的内容调整为窗口的当前大小。通常,您可以由不同的元素组成 - 例如,使用四个“角”位图(或椭圆函数)来绘制角,然后绘制边框等。

此外,您可以处理 WM_NCHITTEST 来分配“标题”/“边框”/“角”功能(即移动窗口并调整窗口大小)到窗口的任意区域。

You can create a window without any frame, use WS_EX_LAYERED to get transparency, then either draw the window including your custom frame "normally" in WM_PAINT, or you compose an off-screen bitmap, and use UpdateLayeredWindow (the latter method is more efficient).

You have to adjust what you draw to the current size of the window, of course. Typically, you would compose it from different elements - e.g. use four "corner" bitmaps (or an ellipse function) to draw the corners, then draw the border, etc.

Also, you can handle WM_NCHITTEST to assign "title" / "border" / "corner" functionality (i.e. moving and sizing the window) to arbitrary areas of your window.

北风几吹夏 2024-08-28 00:05:51

您需要/需要以正确的形状绘制椭圆(或其他形状),然后将其合成到屏幕上,而不是使用预先存在的位图绘制角。例如,您可能决定使用一个椭圆形,其半径为该方向上窗口大小的 5%。

Instead of drawing the corner with a pre-existing bitmap, you'll want/need to draw an ellipse (or whatever) in the correct shape, then composite that to the screen. For example, you might decide on an ellipse with each radius being 5% of the window size in that direction.

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