下面是實現程式視窗圓角多角矩形的三種方法,但效果都比較差。只是簡單的將邊角裁
剪,從邊框和標題列上都可以看出來。不過可以通過這三個函數來學習下
SetWindowRgn()及建立一個HRGN的不同方法。
方法1
void SetWindowEllispeFrame1(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 SetWindowEllispeFrame2(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 SetWindowEllispeFrame3(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);
代碼在網上參考了一些資料,在此表示感謝。