简体   繁体   English

如何在Win32中使用圆角创建可调整大小的窗口

[英]How to create a resizable window with rounded-corners in win32

I'm trying to create a Win32 window that has rounded corners and is resizable both horizontally and vertically. 我试图创建一个Win32窗口,该窗口具有圆角并且可以在水平和垂直方向上调整大小。 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. 我的第一种方法是创建一个圆角矩形的BITMAP并将其绘制到屏幕上,同时将Windows样式设置为WS_EX_LAYERED并将透明度键设置为圆角矩形的外部颜色。

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. 最终,我还想向窗口添加一个阴影。

I would try to mix the Windows API functions CreateRoundRectRgn and SetWindowRgn . 我会尝试将Windows API函数CreateRoundRectRgnSetWindowRgn混合使用。 A very simple example can be found at pInvoke.net web site. pInvoke.net网站上可以找到一个非常简单的示例

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

You have to adjust what you draw to the current size of the window, of course. 当然,您必须将绘制的内容调整为当前窗口的大小。 Typically, you would compose it from different elements - eg 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 (ie moving and sizing the window) to arbitrary areas of your window. 另外,您可以处理WM_NCHITTEST以将“标题” /“边框” /“角”功能(即,移动和调整窗口大小)分配给窗口的任意区域。

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. 例如,您可以选择一个椭圆,每个半径为该方向上的窗口大小的5%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM