2013-04-17 86 views
0

图纸问题与启动,这是我想要实现但与白色区域的圆角,而不是在这样 desired look but with rounded corner of white region子窗口在Win32

为了实现这一目标完全成功所需的外观,我已经确定要被制成白色矩形的屏幕坐标和创建的静态文本窗口和使用该设定圆形区域:

case WM_CREATE: 
    SetRect(&Rect,...); 
       hSubWnd = CreateWindow("STATIC",NULL,SS_LEFT|WS_CHILD|WS_VISIBLE,Rect.left,Rect.top,(Rect.right-Rect.left),(Rect.bottom-Rect.top),hFrame,(HMENU)NULL,NULL,NULL); 


       hrgn = CreateRoundRectRgn(Rect.left, Rect.top, Rect.right, Rect.bottom,15,15); 
       SetWindowRgn(hSubWnd,hrgn,TRUE); 

然后到着色区域SETT上述我使用了以下内容:

case WM_CTLCOLORSTATIC: 
      //SetBkColor((HDC)wParam, RGB(0,0,0)); 
      if((HWND)lParam == hSubWnd) 
      { 
       SetBkMode((HDC)wParam,TRANSPARENT); 

       return (INT_PTR)CreateSolidBrush(RGB(255,255,255)); 
      } 
      break; 

这使得该区域变成白色,但白色区域未按我的预期舍入。 这里是我的问题:

1-如何使SetWindowRgn()工作的子控件?我的方法是否正确或我需要采取其他方式来实现我的目标(四舍五入小孩的角落)?

2-父窗口启用了WS_CLIPCHILDREN样式,这意味着无论我在主窗口的WM_PAINT中做什么都不会绘制子窗口区域。我还需要将一些文本放入子窗口的白色区域。我在哪里做? TextOut()在WM_CTLCOLORSTATIC处理程序中似乎不起作用。

我应该将孩子的窗口类从“STATIC”更改为一些自定义类,并为孩子编写WindowProc(),其中我处理WM_PAINT以在其上绘制文本?

请提供您的建议。

+0

是变量'hrgn'全球?对'SetWindowRgn()'的调用返回一个非零值吗? –

回答

2

既然你说你正在为你的主窗口处理WM_PAINT来绘制文本,我建议完全跳过子控件和区域的复杂性。

我的意思是,所有你想要的是窗口背景上的白色圆角矩形?所以你自己画吧。这通过使用RoundRect函数简单地完成。

如果您需要使用静态控制来确定RoundRect的坐标(这可以使处理更容易,例如,处理不同的DPI设置),您可以将其保留但不可见。

示例代码:

void OnPaint(HWND hWnd) 
{ 
    PAINTSTRUCT ps; 
    BeginPaint(hWnd, &ps); 

    // Create and select a white brush. 
    HBRUSH hbrWhite = CreateSolidBrush(RGB(255, 255, 255)); 
    HBRUSH hbrOrig = SelectObject(ps.hdc, hbrWhite); 

    // Create and select a white pen (or a null pen). 
    HPEN hpenWhite = CreatePen(PS_SOLID, 1, RGB(255, 255, 255)); 
    HPEN hpenOrig = SelectObject(ps.hdc, hpenWhite); 

    // Optionally, determine the coordinates of the invisible static control 
    // relative to its parent (this window) so we know where to draw. 
    // This is accomplished by calling GetClientRect to retrieve the coordinates 
    // and then using MapWindowPoints to transform those coordinates. 

    // Draw the RoundRect. 
    RoundRect(ps.hdc, Rect.left, Rect.top, Rect.right, Rect.bottom, 15, 15); 

    // If you want to draw some text on the RoundRect, this is where you do it. 
    SetBkMode(ps.hdc, TRANSPARENT); 
    SetTextColor(ps.hdc, RGB(255, 0, 0)); // red text 
    DrawText(ps.hdc, TEXT("Sample Text"), -1, &Rect, DT_CENTER); 

    // Clean up after ourselves. 
    SelectObject(ps.hdc, hbrOrig); 
    SelectObject(ps.hdc, hpenOrig); 
    DeleteObject(hbrWhite); 
    DeleteObject(hpenWhite); 
    EndPaint(hWnd, &ps); 
} 
+0

这听起来不错。非常感谢,会尝试一下,让你知道。:)有一个API的海洋知道..lol ...:P – user1624807

+0

尚未运行此代码,但只是试图了解。你制作了一支白色笔和一支白色笔,然后用它来绘制RoundRect()。这是如何设置圆角矩形的背景? api是否负责照顾?正如你在图像中看到的,我想要一个在蓝色窗口背景上有白色圆角矩形的背景。 – user1624807

+0

好的,我通过RoundRect()的文档并得到了答案。 – user1624807