课后作业第011讲:打印一首小诗
本帖最后由 智商是硬伤 于 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;
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, 128, &iTarget);
iLenLine = 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, iLenLine);
// 打印诗的内容,先居中最长的那行,再左对齐
SetTextAlign(hdc, TA_LEFT);
for (i = 1; i < LINES; i++)
{
TextOut(hdc, (cxClient - iMaxLine * cxChar) / 2, (cyClient - LINES * cyChar) / 2 + i * cyChar, poetry, iLenLine);
}
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;
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, 128, &iTarget);
iLenLine = 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, iLenLine);
// 打印诗的内容,先居中最长的那行,再左对齐
SetTextAlign(hdc, TA_LEFT);
for (i = 1; i < LINES; i++)
{
TextOut(hdc, (cxClient - iMaxLine * cxChar) / 2, (cyClient - LINES * cyChar) / 2 + i * cyChar, poetry, iLenLine);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
得不到应有的结果,这是为什么?
因为红色部分你改变了cxCient和cyClient的值啦,怎么可以除以2呢,这样你得到窗口的大小只有实际值的一半,cxClient和cyClient是不能改变的。
然而现在回答你是不是晚了。
页:
[1]