2013-04-23 117 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);