|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 智商是硬伤 于 2015-8-13 17:39 编辑 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
static int cxClient, cyClient, cxChar, cyChar, iLenLine[LINES];
static size_t iTarget, iMaxLine;
int i;
static TCHAR *poetry[] = {
TEXT("I Think I Can"),
TEXT(""),
TEXT("If you think you are beaten, you are;"),
TEXT("If you think you dare not, you don't;"),
TEXT("If you want to win but think you can't;"),
TEXT("It's almost a cinch you won't."),
TEXT("If you think you'll lose, you're lost;"),
TEXT("For out of the world we find;"),
TEXT("Success begins with a fellow's will;"),
TEXT("It's all in a state of mind."),
TEXT("Life's battles don't always go;"),
TEXT("To the stronger and faster man;"),
TEXT("But sooner or later the man who wins;"),
TEXT("Is the man who thinks he can.")
};
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
// 计算每一行的字符数
// 并求出长度最大的行,以便后期的居中处理
for (i = 0; i < LINES; i++)
{
StringCchLength(poetry[i], 128, &iTarget);
iLenLine[i] = iTarget;
iMaxLine = iMaxLine < iTarget ? iTarget : iMaxLine;
}
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
// 打印标题,客户区居中
SetTextAlign(hdc, TA_CENTER);
TextOut(hdc, cxClient / 2, (cyClient - LINES * cyChar) / 2, poetry[0], iLenLine[0]);
// 打印诗的内容,先居中最长的那行,再左对齐
SetTextAlign(hdc, TA_LEFT);
for (i = 1; i < LINES; i++)
{
TextOut(hdc, (cxClient - iMaxLine * cxChar) / 2, (cyClient - LINES * cyChar) / 2 + i * cyChar, poetry[i], iLenLine[i]);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
这是鱼c的答案,现在我修改一下,特用颜色标记了一下。
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
static int cxClient, cyClient, cxChar, cyChar, iLenLine[LINES];
static size_t iTarget, iMaxLine;
int i;
static TCHAR *poetry[] = {
TEXT("I Think I Can"),
TEXT(""),
TEXT("If you think you are beaten, you are;"),
TEXT("If you think you dare not, you don't;"),
TEXT("If you want to win but think you can't;"),
TEXT("It's almost a cinch you won't."),
TEXT("If you think you'll lose, you're lost;"),
TEXT("For out of the world we find;"),
TEXT("Success begins with a fellow's will;"),
TEXT("It's all in a state of mind."),
TEXT("Life's battles don't always go;"),
TEXT("To the stronger and faster man;"),
TEXT("But sooner or later the man who wins;"),
TEXT("Is the man who thinks he can.")
};
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
// 计算每一行的字符数
// 并求出长度最大的行,以便后期的居中处理
for (i = 0; i < LINES; i++)
{
StringCchLength(poetry[i], 128, &iTarget);
iLenLine[i] = iTarget;
iMaxLine = iMaxLine < iTarget ? iTarget : iMaxLine;
}
return 0;
/*
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
*/
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
cxClient = (rect.right - rect.left)/2;
cyClient = (rect.bottom - rect.top)/2;
// 打印标题,客户区居中
SetTextAlign(hdc, TA_CENTER);
TextOut(hdc, cxClient / 2, (cyClient - LINES * cyChar) / 2, poetry[0], iLenLine[0]);
// 打印诗的内容,先居中最长的那行,再左对齐
SetTextAlign(hdc, TA_LEFT);
for (i = 1; i < LINES; i++)
{
TextOut(hdc, (cxClient - iMaxLine * cxChar) / 2, (cyClient - LINES * cyChar) / 2 + i * cyChar, poetry[i], iLenLine[i]);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
得不到应有的结果,这是为什么?
|
|