鱼C论坛

 找回密码
 立即注册
查看: 2066|回复: 1

课后作业第011讲:打印一首小诗

[复制链接]
发表于 2015-8-13 17:35:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 智商是硬伤 于 2015-8-13 17:39 编辑
  1. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  2. {
  3.         HDC hdc;
  4.         PAINTSTRUCT ps;
  5.         TEXTMETRIC tm;
  6.         static int cxClient, cyClient, cxChar, cyChar, iLenLine[LINES];
  7.         static size_t iTarget, iMaxLine;
  8.         int i;
  9.         
  10.         static TCHAR *poetry[] = {
  11.                 TEXT("I Think I Can"),
  12.                 TEXT(""),
  13.                 TEXT("If you think you are beaten, you are;"),
  14.                 TEXT("If you think you dare not, you don't;"),
  15.                 TEXT("If you want to win but think you can't;"),
  16.                 TEXT("It's almost a cinch you won't."),
  17.                 TEXT("If you think you'll lose, you're lost;"),
  18.                 TEXT("For out of the world we find;"),
  19.                 TEXT("Success begins with a fellow's will;"),
  20.                 TEXT("It's all in a state of mind."),
  21.                 TEXT("Life's battles don't always go;"),
  22.                 TEXT("To the stronger and faster man;"),
  23.                 TEXT("But sooner or later the man who wins;"),
  24.                 TEXT("Is the man who thinks he can.")
  25.         };

  26.         switch (message)
  27.         {
  28.         case WM_CREATE:
  29.                 hdc = GetDC(hwnd);
  30.                 GetTextMetrics(hdc, &tm);
  31.                 cxChar = tm.tmAveCharWidth;
  32.                 cyChar = tm.tmHeight + tm.tmExternalLeading;
  33.                 ReleaseDC(hwnd, hdc);

  34.                 // 计算每一行的字符数
  35.                 // 并求出长度最大的行,以便后期的居中处理
  36.                 for (i = 0; i < LINES; i++)
  37.                 {
  38.                         StringCchLength(poetry[i], 128, &iTarget);
  39.                         iLenLine[i] = iTarget;
  40.                         iMaxLine = iMaxLine < iTarget ? iTarget : iMaxLine;
  41.                 }
  42.                 return 0;

  43.         case WM_SIZE:
  44.                 cxClient = LOWORD(lParam);
  45.                 cyClient = HIWORD(lParam);
  46.                 return 0;

  47.         case WM_PAINT:
  48.                 hdc = BeginPaint(hwnd, &ps);

  49.                 // 打印标题,客户区居中
  50.                 SetTextAlign(hdc, TA_CENTER);
  51.                 TextOut(hdc, cxClient / 2, (cyClient - LINES * cyChar) / 2, poetry[0], iLenLine[0]);

  52.                 // 打印诗的内容,先居中最长的那行,再左对齐
  53.                 SetTextAlign(hdc, TA_LEFT);
  54.                 for (i = 1; i < LINES; i++)
  55.                 {
  56.                         TextOut(hdc, (cxClient - iMaxLine * cxChar) / 2, (cyClient - LINES * cyChar) / 2 + i * cyChar, poetry[i], iLenLine[i]);
  57.                 }

  58.                 EndPaint(hwnd, &ps);
  59.                 return 0;

  60.         case WM_DESTROY:
  61.                 PostQuitMessage(0);
  62.                 return 0;
  63.         }

  64.         return DefWindowProc(hwnd, message, wParam, lParam);
  65. }
复制代码

这是鱼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, 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[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, iLenLine);
                }

                EndPaint(hwnd, &ps);
                return 0;

        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }

        return DefWindowProc(hwnd, message, wParam, lParam);
}


得不到应有的结果,这是为什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-9-28 20:14:05 | 显示全部楼层
因为红色部分你改变了cxCient和cyClient的值啦,怎么可以除以2呢,这样你得到窗口的大小只有实际值的一半,cxClient和cyClient是不能改变的。
然而现在回答你是不是晚了。

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
小甲鱼 + 5 + 5 热爱鱼C^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 16:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表