你實際上並不需要自己居中文本。如果您傳遞適當的標誌,GDI文本輸出功能可以爲您做到這一點。
例如,如果您調用DrawText
並通過DT_CENTER
標誌,它將自動在指定的矩形(水平居中)中間繪製文本。
假設你只有文本(這聽起來像你這樣做)的單行線,你可以讓它自動垂直中心通過傳遞DT_SINGLELINE
和DT_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);
}
這氣味像功課。你的努力或你的程序結構在哪裏? – 2013-04-23 00:59:33
GY - 同意這是一個家庭作業 - 但是對於win32編程來說相對較新 - 並且基於我讀過的內容 - 需要創建一個基於臨時內存的DC來計算文本的大小/寬度 - 找到大部分從這個線程的答案,但不知道如何創建一個臨時DC來執行此任務。即使它是一個矩形包圍的單個文本 - 我可以進一步擴展它 - http://stackoverflow.com/questions/1835749/win32-text-drawing-puzzle – ejuser 2013-04-23 01:02:34
這是我有這樣的代碼的最低版本遠(用於單個文本):\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