PDA

Click to See Complete Forum and Search --> : [RESOLVED]Using RoundRect AND DrawText


ISquishWorms
Oct 20th, 2002, 09:17 AM
Hello,

I am using the following code to draw two red round rectangles outlined with a solid white pen in the canter of my black client window. The problem I have is that I would like to also centre my text with in the round rectangles horizontally and vertically. From what I can tell the API DrawText function would allow me to do just this using ‘DT_CENTER | DT_VCENTER’ however, this API call requires that I have a structure of type RECT how do I acquire that from my round rectangles?

Thanks in advance for any help in this matter,


case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rect;
HDC hdc;


hdc = BeginPaint(hwnd, &ps);

SelectObject(hdc, GetStockObject(WHITE_PEN));
SelectObject (hdc, hBrush1);
RoundRect(hdc,10, 10, cxClient - 10, (cyClient - 10) / 2, 10, 10);
RoundRect(hdc,10, (cyClient + 10) / 2, cxClient - 10, cyClient - 10 , 10, 10);
SetBkColor(hdc, RGB(255,0,0));
SetTextAlign(hdc,TA_CENTER);
TextOut(hdc, cxClient / 2, 15, "TEXT", 4);
TextOut(hdc, cxClient / 2, 55, "MORE TEXT", 9);
//DrawText(hdc, "TEXT", 4, &rect, DT_CENTER | DT_VCENTER);

EndPaint(hwnd, &ps);
return 0;
}

parksie
Oct 20th, 2002, 09:32 AM
It's just the width and height of your round rectangle, so put in the same values you did, just ignore the radius of the corners.

ISquishWorms
Oct 20th, 2002, 09:40 AM
Thanks, very much for your quick reply and advice parksie it is much appreciated. I know that I need these values in the structure RECT, but what is the best way to do this and how?

CornedBee
Oct 20th, 2002, 12:55 PM
RECT rect;
rect.left = 10;
rect.top = 10;
rect.right = cxClient - 10;
rect.bottom = cyClient - 10;


You could also use the SetRect API call, but why call into a dll to do a thing you can do yourself in only 3 lines more?

ISquishWorms
Oct 20th, 2002, 01:23 PM
Thanks, CornedBee some how I knew I could count on you for a solution. :-) Having spent a little more time investigating a solution myself since posting this thread I came up with the following code. In which I was using the SetRect API, but as your solution saves me calling this API I will be implementing your improved solution.



case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rect;
HDC hdc;


hdc = BeginPaint(hwnd, &ps);

SelectObject(hdc, GetStockObject(WHITE_PEN));
SelectObject (hdc, hBrush1);
RoundRect(hdc,10, 10, cxClient - 10, (cyClient - 10) / 2, 10, 10);
RoundRect(hdc,10, (cyClient + 10) / 2, cxClient - 10, cyClient - 10, 10, 10);
SetBkMode(hdc,TRANSPARENT);
SetRect(&rect, 10, 10, cxClient - 10, (cyClient - 10) / 2);
DrawText (hdc, TEXT ("SOME TEXT"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
SetRect(&rect, 10, (cyClient + 10) / 2, cxClient - 10, cyClient - 10);
DrawText (hdc, TEXT ("MORE TEXT"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

EndPaint(hwnd, &ps);

return 0;
}



Thanks, as always for helping make my code even better CornedBee.

CornedBee
Oct 20th, 2002, 01:42 PM
Always glad.