|
发表于 2015-10-29 15:10:48
|
显示全部楼层
本帖最后由 ~风介~ 于 2015-11-5 19:59 编辑
刚学到十几章,说一下自己的看法。
首先,我们要知道这几个函数的作用:
①StringCchPrintf //流定向
②StringCchCat //字符串拼接
③StringCchLength //检测长度
问题一:
在 StringCchPrintf(szBuffer, 128, TEXT(""))之前, szBuffer只是声明而未定义 —— 此处把它赋值为空,方便StringCchCat(szBuffer, 128, TEXT("x"))拼接操作 —— 否则szBuffer的值未确定如何拼接?
这一句还有一个功能就是每一轮迭代后把szBuffer置空。具体可以参考下修改的代码。
- /* -------------------------------------------------------------------
- MyWindows.c -- 基本窗口模型
- 《Windows 程序设计(SDK)》视频教程
- --------------------------------------------------------------------*/
- #include <windows.h>
- #include "strsafe.h"
- #define N 10
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
- {
- static TCHAR szAppName[] = TEXT("MyWindows");
- HWND hwnd;
- MSG msg;
- WNDCLASS wndclass;
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = WndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName = szAppName;
- if (!RegisterClass(&wndclass))
- {
- MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
- return 0;
- }
- hwnd = CreateWindow(szAppName,
- TEXT("鱼C工作室"),
- WS_OVERLAPPEDWINDOW,
- //WS_DISABLED,
- //WS_HSCROLL|WS_VSCROLL|WS_OVERLAPPEDWINDOW,
- /*
- WS_POPUP|WS_SIZEBOX,
- 200,
- 200,
- 400,
- 120,
- */
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- NULL,
- NULL,
- hInstance,
- NULL);
- ShowWindow(hwnd, iCmdShow);
- UpdateWindow(hwnd);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- TCHAR szBuffer[128];
- int i, j;
- size_t iTarget;
- TEXTMETRIC tm;
- RECT rect;
- static int cxChar, cyChar;
- switch (message)
- {
- case WM_CREATE:
- hdc = GetDC(hwnd);
- GetTextMetrics(hdc, &tm);
- cxChar = tm.tmAveCharWidth;
- cyChar = tm.tmHeight + tm.tmExternalLeading;
- ReleaseDC(hwnd, hdc);
- return 0;
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- StringCchPrintf(szBuffer, 128, TEXT("a"));
- for (i = 1; i <= N; i++)
- {
-
- for (j = 0; j < 2 * i - 1; j++)
- {
- StringCchCat(szBuffer, 128, TEXT("x"));
- }
- StringCchLength(szBuffer, 128, &iTarget);
- GetClientRect(hwnd, &rect);
- SetTextAlign(hdc, GetTextAlign(hdc) | TA_CENTER);
- TextOut(hdc, (rect.right - rect.left) / 2, (rect.bottom - rect.top) / 2 - (N / 2 - i + 1) * cyChar, szBuffer, iTarget);
- }
- EndPaint(hwnd, &ps);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, message, wParam, lParam);
- }
复制代码 结果截图:
问题二、三暂时不太明白~@小甲鱼
|
|