Reputation: 301
To Start with , this is the desired look that I want to achieve but with rounded corners of the white region, and am not fully successful in doing so
To achieve this , I have identified the screen co-ordinates of the rectangle which is to be made white and created a static text window and set a rounded region using this:
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);
Then to color the region sett above I used the following:
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;
This makes the region colored white but the white region is not rounded as I expected. Here are my questions:
1- How to make SetWindowRgn() work for child controls ?Is my approach correct or I need to take some other way to achieve my goal(rounding off the corners of the child)?
2- The parent window has WS_CLIPCHILDREN style enabled , which means that whatever I do in WM_PAINT of the main windows is not going to paint the child window area. I need some text to be put in the white region of the child window as well. Where do I do that ? TextOut() doesnt seem to work inside WM_CTLCOLORSTATIC handler.
Should I change the window class of the child from "STATIC" to some custom class and write the WindowProc() for the child, wherein I handle the WM_PAINT to draw text on it?
Kindly provide your suggestions.
Upvotes: 0
Views: 2043
Reputation: 245001
Since you say that you're handling WM_PAINT
for your main window anyway to draw text, I recommend skipping the complexity of child controls and regions altogether.
I mean, all you want is a white rounded rectangle on your window's background? So draw it yourself. This is trivially accomplished using the RoundRect
function.
If you need the static control to determine the coordinates of the RoundRect (this can make things a lot easier when dealing with, e.g., different DPI settings), you can leave it there but make it invisible.
Sample code:
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);
}
Upvotes: 2