Win32 - How to draw a rectangle around a text line?

I am new to Win32 and trying to get GDI-based code in C ++ (for technical reasons I don't want to use GDI +)

Edit: Simplified question:

I need to draw a rectangle around the text that is drawn in the middle of the window. - How can I fill in the coordinates of the rectangle? - Can someone help with the line - Rectangle (x1, y1, x2, y2)? - How to calculate these values ​​(x1, y1) and (x2, y2)?

Thanks..

hdc = BeginPaint(hWnd, &ps); GetClientRect(hWnd, &rcClient); SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT)); SetTextColor(hdc, RGB(255, 0, 0)); DrawText(hdc, wstring(s.begin(),s.end()).c_str(), -1, &rectResult, DT_SINGLELINE | DT_CALCRECT); DrawText(hdc, wstring(s.begin(),s.end()).c_str(), -1, &rcClient, DT_SINGLELINE | DT_CENTER | DT_VCENTER); // Here I need help - How to I place the rectangle around the Text - which is drawn in the middle of the window? // It looks like need to use - rectResult.bottom/top/left/right - but don't know how.. Rectangle(hdc, 0,0,100,100); 
+4
source share
3 answers

In fact, you do not need to focus the text yourself. GDI text output functions can do this for you if you pass the appropriate flags.

For example, if you call DrawText and pass the DT_CENTER flag, it automatically draws the text in the middle of the specified rectangle (with a horizontal orientation).

Assuming you have only one line of text (which sounds like it is), you can force it to automatically center the text vertically by passing the DT_SINGLELINE and DT_VCENTER .

So, all you really need to do is write code to divide the area of ​​your client window into 4 equal parts, and then pass these rectangles to DrawText . This is not terribly difficult. Put pencil and paper if you cannot visualize it in your head.

 void PaintWindow(HWND hWnd) { // Set up the device context for drawing. PAINTSTRUCT ps; HDC hDC = BeginPaint(hWnd, &ps); HPEN hpenOld = static_cast<HPEN>(SelectObject(hDC, GetStockObject(DC_PEN))); HBRUSH hbrushOld = static_cast<HBRUSH>(SelectObject(hDC, GetStockObject(NULL_BRUSH))); // Calculate the dimensions of the 4 equal rectangles. RECT rcWindow; GetClientRect(hWnd, &rcWindow); RECT rc1, rc2, rc3, rc4; rc1 = rc2 = rc3 = rc4 = rcWindow; rc1.right -= (rcWindow.right - rcWindow.left) / 2; rc1.bottom -= (rcWindow.bottom - rcWindow.top) / 2; rc2.left = rc1.right; rc2.bottom = rc1.bottom; rc3.top = rc1.bottom; rc3.right = rc1.right; rc4.top = rc1.bottom; rc4.left = rc1.right; // Optionally, deflate each of the rectangles by an arbitrary amount so that // they don't butt up right next to each other and we can distinguish them. InflateRect(&rc1, -5, -5); InflateRect(&rc2, -5, -5); InflateRect(&rc3, -5, -5); InflateRect(&rc4, -5, -5); // Draw (differently-colored) borders around these rectangles. SetDCPenColor(hDC, RGB(255, 0, 0)); // red Rectangle(hDC, rc1.left, rc1.top, rc1.right, rc1.bottom); SetDCPenColor(hDC, RGB(0, 255, 0)); // green Rectangle(hDC, rc2.left, rc2.top, rc2.right, rc2.bottom); SetDCPenColor(hDC, RGB(0, 0, 255)); // blue Rectangle(hDC, rc3.left, rc3.top, rc3.right, rc3.bottom); SetDCPenColor(hDC, RGB(255, 128, 0)); // orange Rectangle(hDC, rc4.left, rc4.top, rc4.right, rc4.bottom); // Draw the text into the center of each of the rectangles. SetBkMode(hDC, TRANSPARENT); SetBkColor(hDC, RGB(0, 0, 0)); // black // TODO: Optionally, set a nicer font than the default. DrawText(hDC, TEXT("Hello World!"), -1, &rc1, DT_CENTER | DT_SINGLELINE | DT_VCENTER); DrawText(hDC, TEXT("Hello World!"), -1, &rc2, DT_CENTER | DT_SINGLELINE | DT_VCENTER); DrawText(hDC, TEXT("Hello World!"), -1, &rc3, DT_CENTER | DT_SINGLELINE | DT_VCENTER); DrawText(hDC, TEXT("Hello World!"), -1, &rc4, DT_CENTER | DT_SINGLELINE | DT_VCENTER); // Clean up after ourselves. SelectObject(hDC, hpenOld); SelectObject(hDC, hbrushOld); EndPaint(hWnd, &ps); } 

+8
source
 RECT rect={0,0,0,0}; const char *str="Test Text"; DrawText(hDC, str, strlen(str), &rect, DT_CALCRECT | DT_NOCLIP); Rectangle(hDC,rect.left,rect.top,rect.right,rect.bottom); DrawText(hDC, str, strlen(str), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE|DT_NOCLIP); 
+1
source

Got it finally :) Thanks a lot to Cody Gray for pointing me in the right direction :)

 GetTextExtentPoint32(hDC, str, strlen(str), &sz2); rect2.top=rect2.bottom+sz2.cy; rect2.right=rect2.top+sz2.cx; Rectangle(hDC,rect2.left,rect2.top,rect2.right,rect2.bottom); DrawText(hDC, str, -1, &rect2, DT_CENTER | DT_VCENTER | DT_SINGLELINE|DT_NOCLIP); 
+1
source

All Articles