VC 利用SetWindowRgn實現程式視窗的圓角多角矩形 .

來源:互聯網
上載者:User

 下面是實現程式視窗圓角多角矩形的三種方法,但效果都比較差。只是簡單的將邊角裁

剪,從邊框和標題列上都可以看出來。不過可以通過這三個函數來學習下

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

代碼在網上參考了一些資料,在此表示感謝。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.