现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

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

2012-08-31 23:35 工业·编程 ⁄ 共 5669字 ⁄ 字号 暂无评论

下面是实现程序窗口圆角多角矩形的三种方法,但效果都比较差。只是简单的将边角裁
剪,从边框和标题栏上都可以看出来。不过可以通过这三个函数来学习下

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

方法1

[cpp] view plaincopy
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
<pre class="cpp" name="code">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); 

</pre> 
<pre></pre> 
<p><span style="font-size:18px">方法3</span></p> 
<pre class="cpp" name="code"><pre class="cpp" name="code">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); 
}</pre> 
<pre></pre> 
<p><span style="font-size:18px">再对SetWindowRgn()进行下说明<br> 
1. The coordinates of a window's window region are relative to the upper</span><span style="font-size:18px">-left corner of the window, not the client area of the window.<br> 
窗口的RGN的坐标体系不是屏幕坐标,而是以窗口的左上角开始的。<br> 
2. After a successful call to SetWindowRgn, the system owns the region </span><span style="font-size:18px">specified by the region handle hRgn. The system does not make a copy of</span><span style="font-size:18px">the region. Thus, you should not make any further 
function calls with</span><span style="font-size:18px">this region handle. In particular, do not delete this region handle. The</span><span style="font-size:18px">system deletes the region handle when it no longer needed.<br> 
设置SetWindowRgn()成功后,不用再管HRGN句柄了,系统会接管它。</span></p> 
<p><span style="font-size:18px"> </span></p> 
<p><span style="font-size:18px">调用方法<br> 
win32程序可以在WM_CREATET和WM_INITDIALOG消息处理中调用。<br> 
MFC程序可以OnInitDialog()中调用。<br> 
如:SetWindowEllispeFrame1(hwnd, 50, 50)<br> 
    或SetWindowEllispeFrame1(this->GetSafeHwnd(), 50, 50);</span></p> 
<p> </p> 
<p><span style="font-size:16px">代码在网上参考了一些资料,在此表示感谢。</span><img alt="微笑" src="

下面是实现程序窗口圆角多角矩形的三种方法,但效果都比较差。只是简单的将边角裁
剪,从边框和标题栏上都可以看出来。不过可以通过这三个函数来学习下

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

方法1

[cpp] view plaincopy
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

<pre class="cpp" name="code">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); 

</pre> 
<pre></pre> 
<p><span style="font-size:18px">方法3</span></p> 
<pre class="cpp" name="code"><pre class="cpp" name="code">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); 
}</pre> 
<pre></pre> 
<p><span style="font-size:18px">再对SetWindowRgn()进行下说明<br> 
1. The coordinates of a window's window region are relative to the upper</span><span style="font-size:18px">-left corner of the window, not the client area of the window.<br> 
窗口的RGN的坐标体系不是屏幕坐标,而是以窗口的左上角开始的。<br> 
2. After a successful call to SetWindowRgn, the system owns the region </span><span style="font-size:18px">specified by the region handle hRgn. The system does not make a copy of</span><span style="font-size:18px">the region. Thus, you should not make any further 
function calls with</span><span style="font-size:18px">this region handle. In particular, do not delete this region handle. The</span><span style="font-size:18px">system deletes the region handle when it no longer needed.<br> 
设置SetWindowRgn()成功后,不用再管HRGN句柄了,系统会接管它。</span></p> 
<p><span style="font-size:18px"> </span></p> 
<p><span style="font-size:18px">调用方法<br> 
win32程序可以在WM_CREATET和WM_INITDIALOG消息处理中调用。<br> 
MFC程序可以OnInitDialog()中调用。<br> 
如:SetWindowEllispeFrame1(hwnd, 50, 50)<br> 
    或SetWindowEllispeFrame1(this->GetSafeHwnd(), 50, 50);</span></p> 
<p> </p> 
<p><span style="font-size:16px">代码在网上参考了一些资料,在此表示感谢。</span><img alt="微笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif"></p> 
 
</pre> 

></p> 
 
</pre> 

给我留言

留言无头像?