您的位置:首页 > 其它

VC 利用SetWindowRgn实现程序窗口的圆角多角矩形

2011-08-08 11:55 357 查看
下面是实现程序窗口圆角多角矩形的三种方法,但效果都比较差。只是简单的将边角裁剪,从边框和标题栏上都可以看出来。不过可以通过这三个函数来学习下
SetWindowRgn()及创建一个HRGN的不同方法。
方法1
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);

代码在网上参考了一些资料,在此表示感谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: