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

分享一下我的偶像大神的人工智能教程!http://blog.csdn.net/jiangjunshow

也欢迎转载我的文章,转载请注明出处 https://blog.csdn.net/weixin_43417960

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

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

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

方法1


   
   
  1. void SetWindowEllipseFrame1(HWND hwnd, int nWidthEllipse, int nHeightEllipse)
  2. {
  3. HRGN hRgn;
  4. RECT rect;
  5. GetWindowRect(hwnd, &rect);
  6. hRgn = CreateRoundRectRgn( 0, 0, rect.right - rect.left, rect.bottom - rect.top, nWidthEllipse, nHeightEllipse);
  7. SetWindowRgn(hwnd, hRgn, TRUE);
  8. }

方法2


   
   
  1. void SetWindowEllipseFrame2(HWND hwnd, int nWidthEllipse, int nHeightEllipse)
  2. {
  3. HRGN hRgn;
  4. RECT rect;
  5. HDC hdc, hdcMem;
  6. hdc = GetDC(hwnd);
  7. hdcMem = CreateCompatibleDC(hdc);
  8. ReleaseDC(hwnd, hdc);
  9. GetWindowRect(hwnd, &rect);
  10. BeginPath(hdcMem);
  11. RoundRect(hdcMem, 0, 0, rect.right - rect.left, rect.bottom - rect.top, nWidthEllipse, nHeightEllipse); // 画一个圆角矩形。
  12. EndPath(hdcMem);
  13. hRgn = PathToRegion(hdcMem); // 最后把路径转换为区域。
  14. SetWindowRgn(hwnd, hRgn, TRUE);
  15. }

方法3


   
   
  1. void SetWindowEllipseFrame3(HWND hwnd, int nWidthEllipse, int nHeightEllipse)
  2. {
  3. HRGN hRgn;
  4. RECT rect;
  5. int nHeight,nWidth;
  6. GetWindowRect(hwnd, &rect);
  7. nHeight = rect.bottom - rect.top; // 计算高度
  8. nWidth = rect.right - rect.left; // 计算宽度
  9. POINT point[ 8] = {
  10. { 0, nHeightEllipse}, // left-left-top
  11. {nWidthEllipse, 0}, // left-top-left
  12. {nWidth - nWidthEllipse, 0},
  13. {nWidth, nHeightEllipse}, // right-top
  14. {nWidth, nHeight - nHeightEllipse}, // right-bottom-right
  15. {nWidth - nWidthEllipse, nHeight}, // right-bottom-bottom
  16. {nWidthEllipse, nHeight}, // left-bottom
  17. { 0, nHeight - nHeightEllipse}
  18. };
  19. hRgn = CreatePolygonRgn(point, 8, WINDING);
  20. SetWindowRgn(hwnd,hRgn,TRUE);
  21. }

再对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_CREATETWM_INITDIALOG消息处理中调用。MFC程序可以OnInitDialog()中调用。如:

SetWindowEllispeFrame1(hwnd, 50, 50)   

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

 

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



 

给我偶像的人工智能教程打call!http://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/weixin_43417960/article/details/83113342
vc