2013-04-23 43 views
4

我是新来的Win32并试图获得在C基于GDI ++编写代码(由于技术原因不希望使用GDI +)win32 - 如何围绕文本字符串绘制矩形?

编辑:简单求的问题:

我需要周围绘制一个矩形在窗口中间绘制的文本。 - 如何填充矩形坐标? - 任何人可以帮助行 - 矩形(x1,y1,x2,y2)? - 如何计算这些(x1,y1)&(x2,y2)的值?

谢谢。

 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); 
+0

这气味像功课。你的努力或你的程序结构在哪里? – 2013-04-23 00:59:33

+1

GY - 同意这是一个家庭作业 - 但是对于win32编程来说相对较新 - 并且基于我读过的内容 - 需要创建一个基于临时内存的DC来计算文本的大小/宽度 - 找到大部分从这个线程的答案,但不知道如何创建一个临时DC来执行此任务。即使它是一个矩形包围的单个文本 - 我可以进一步扩展它 - http://stackoverflow.com/questions/1835749/win32-text-drawing-puzzle – ejuser 2013-04-23 01:02:34

+1

这是我有这样的代码的最低版本远(用于单个文本):\t 情况WM_PAINT: \t \t HDC =调用BeginPaint(HWND,&ps); \t \t GetClientRect(HWND,&RC客户机); \t \t选择对象(HDC,GetStockObject(DEFAULT_GUI_FONT)); \t \t SetTextColor (hdc,RGB(255,0,0)); \t \t DrawText(hdc,wstring(s.begin(),s.end())。c_str(),-1 ,&rcClient,DT_SINGLELINE | DT_CENTER | DT_VCENTER); \t \t // TODO:在此添加任何图纸代码... \t \t调用EndPaint(HWND,&ps); \t \t突破; – ejuser 2013-04-23 01:08:08

回答

8

你实际上并不需要自己居中文本。如果您传递适当的标志,GDI文本输出功能可以为您做到这一点。

例如,如果您调用DrawText并通过DT_CENTER标志,它将自动在指定的矩形(水平居中)中间绘制文本。

假设你只有文本(这听起来像你这样做)的单行线,你可以让它自动垂直中心通过传递DT_SINGLELINEDT_VCENTER标志的文本。

所以你所要做的就是编写代码将your window's client area分成4等份,然后将这些矩形传递给DrawText函数。这并不困难。如果你不能在头脑中想象它,请将铅笔和纸放在它上面。

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); 
} 

 

+0

谢谢科迪。你的假设对于单行是正确的,这当然有帮助,想知道是否(X,Y)co-o rdinates是已知的 - 文本可以围绕它而不是矩形吗?想象一下 - 为此,需要计算文本的高度和宽度 - 以(X,Y)为中心? (道歉的问题,因为我实际上试图找到解决方案围绕(x,y)(垂直和水平)文本居中。 谢谢 – ejuser 2013-04-23 04:18:08

+0

@ejuser \t当然,如果你知道坐标,你可以创建一个RECT如果你想要的话,你可以自己计算出一串文本所需的宽度和高度,为此,你将使用['GetTextExtentPoint32'](http: //msdn.microsoft.com/en-us/library/windows/desktop/dd144938.aspx),就像我记得你在某个问题中提到的那样,只是让Windows为你做更多的工作,更多的信息是[这里](http://stackoverflow.com/a/11600004/366904)。 – 2013-04-23 04:21:33

+0

感谢科迪,会进一步探索并回复 - 希望能够得到你想要提供的提示 – ejuser 2013-04-23 04:39:42

1
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

得到它终于:)非常感谢科迪灰色指着我在正确的方向这个:)

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);