c语言圆角矩形实现,VC 利用SetWindowRgn实现程序窗口的圆角多角矩形 | 求索阁

下面是实现程序窗口圆角多角矩形的三种方法,但效果都比较差。只是简单的将边角裁

剪,从边框和标题栏上都可以看出来。不过可以通过这三个函数来学习下

SetWindowRgn()及创建一个HRGN的不同方法。

方法1

[cpp] view plaincopy

void SetWindowEllipseFrame1(HWND hwnd, int nWidthEllipse, int nHeightEllipse)

{

HRGN hRgn;

RECT rect;

GetWindowRect(hwnd, &rect);

hRgn = CreateRoundRectRgn(0, 0, rect.right - rect.left, rect.bottom - rect.top, nWidthEllipse, nHeightEllipse);

SetWindowRgn(hwnd, hRgn, TRUE);

}

方法2

void SetWindowEllipseFrame2(HWND hwnd, int nWidthEllipse, int nHeightEllipse)

{

HRGN hRgn;

RECT rect;

HDC hdc, hdcMem;

hdc = GetDC(hwnd);

hdcMem = CreateCompatibleDC(hdc);

ReleaseDC(hwnd, hdc);

GetWindowRect(hwnd, &rect);

BeginPath(hdcMem);

RoundRect(hdcMem, 0, 0, rect.right - rect.left, rect.bottom - rect.top, nWidthEllipse, nHeightEllipse); // 画一个圆角矩形。

EndPath(hdcMem);

hRgn = PathToRegion(hdcMem); // 最后把路径转换为区域。

SetWindowRgn(hwnd, hRgn, TRUE);

}

 
 

方法3

void SetWindowEllipseFrame3(HWND hwnd, int nWidthEllipse, int nHeightEllipse)

{

HRGN hRgn;

RECT rect;

int nHeight,nWidth;

GetWindowRect(hwnd, &rect);

nHeight = rect.bottom - rect.top; // 计算高度

nWidth = rect.right - rect.left; // 计算宽度

POINT point[8] = {

{0, nHeightEllipse}, // left-left-top

{nWidthEllipse, 0}, // left-top-left

{nWidth - nWidthEllipse, 0},

{nWidth, nHeightEllipse}, // right-top

{nWidth, nHeight - nHeightEllipse},// right-bottom-right

{nWidth - nWidthEllipse, nHeight}, // right-bottom-bottom

{nWidthEllipse, nHeight}, // left-bottom

{0, nHeight - nHeightEllipse}

};

hRgn = CreatePolygonRgn(point, 8, WINDING);

SetWindowRgn(hwnd,hRgn,TRUE);

}

 
 

再对SetWindowRgn()进行下说明

1. The coordinates of a window's window region are relative to the upper-left corner of the window, not the client area of the window.

窗口的RGN的坐标体系不是屏幕坐标,而是以窗口的左上角开始的。

2. After a successful call to SetWindowRgn, the system owns the region specified by the region handle hRgn. The system does not make a copy ofthe region. Thus, you should not make any further

function calls withthis region handle. In particular, do not delete this region handle. Thesystem deletes the region handle when it no longer needed.

设置SetWindowRgn()成功后,不用再管HRGN句柄了,系统会接管它。

调用方法

win32程序可以在WM_CREATET和WM_INITDIALOG消息处理中调用。

MFC程序可以OnInitDialog()中调用。

如:SetWindowEllispeFrame1(hwnd, 50, 50)

或SetWindowEllispeFrame1(this->GetSafeHwnd(), 50, 50);

代码在网上参考了一些资料,在此表示感谢。微笑void SetWindowEllipseFrame2(HWND hwnd, int nWidthEllipse, int nHeightEllipse)

{

HRGN hRgn;

RECT rect;

HDC hdc, hdcMem;

hdc = GetDC(hwnd);

hdcMem = CreateCompatibleDC(hdc);

ReleaseDC(hwnd, hdc);

GetWindowRect(hwnd, &rect);

BeginPath(hdcMem);

RoundRect(hdcMem, 0, 0, rect.right - rect.left, rect.bottom - rect.top, nWidthEllipse, nHeightEllipse); // 画一个圆角矩形。

EndPath(hdcMem);

hRgn = PathToRegion(hdcMem); // 最后把路径转换为区域。

SetWindowRgn(hwnd, hRgn, TRUE);

}

 
 

方法3

void SetWindowEllipseFrame3(HWND hwnd, int nWidthEllipse, int nHeightEllipse)

{

HRGN hRgn;

RECT rect;

int nHeight,nWidth;

GetWindowRect(hwnd, &rect);

nHeight = rect.bottom - rect.top; // 计算高度

nWidth = rect.right - rect.left; // 计算宽度

POINT point[8] = {

{0, nHeightEllipse}, // left-left-top

{nWidthEllipse, 0}, // left-top-left

{nWidth - nWidthEllipse, 0},

{nWidth, nHeightEllipse}, // right-top

{nWidth, nHeight - nHeightEllipse},// right-bottom-right

{nWidth - nWidthEllipse, nHeight}, // right-bottom-bottom

{nWidthEllipse, nHeight}, // left-bottom

{0, nHeight - nHeightEllipse}

};

hRgn = CreatePolygonRgn(point, 8, WINDING);

SetWindowRgn(hwnd,hRgn,TRUE);

}

 
 

再对SetWindowRgn()进行下说明

1. The coordinates of a window's window region are relative to the upper-left corner of the window, not the client area of the window.

窗口的RGN的坐标体系不是屏幕坐标,而是以窗口的左上角开始的。

2. After a successful call to SetWindowRgn, the system owns the region specified by the region handle hRgn. The system does not make a copy ofthe region. Thus, you should not make any further

function calls withthis region handle. In particular, do not delete this region handle. Thesystem deletes the region handle when it no longer needed.

设置SetWindowRgn()成功后,不用再管HRGN句柄了,系统会接管它。

调用方法

win32程序可以在WM_CREATET和WM_INITDIALOG消息处理中调用。

MFC程序可以OnInitDialog()中调用。

如:SetWindowEllispeFrame1(hwnd, 50, 50)

或SetWindowEllispeFrame1(this->GetSafeHwnd(), 50, 50);

代码在网上参考了一些资料,在此表示感谢。微笑

 

>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值