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

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

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

```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);
}```

```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);
}```

```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);
}```

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.

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.

SetWindowEllispeFrame1(hwnd, 50, 50)

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