|

楼主 |
发表于 2024-7-26 16:09:57
|
显示全部楼层
- #include <windows.h>
- #include"sysmets.h"
- #include<strsafe.h>
- #define LINECOUNT 128
- 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("hello"),
- WS_OVERLAPPEDWINDOW | WS_VSCROLL,
- 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;
- TEXTMETRIC tm;
- TCHAR szBuffer[128];
- static int cxChar, cxCaps, cyChar;
- static int cxClient, cyClient;//客户区宽度和高度
- size_t iTarget;
- switch (message)
- {
- case WM_CREATE:
- hdc = GetDC(hwnd);
- GetTextMetrics(hdc, &tm);
- cxChar = tm.tmAveCharWidth;//字体字符平均宽度
- //如果是等宽字体不改变,如果是变宽字体(大写字母是小写字母宽度的1.5倍)
- cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
- //获取字符高度
- cyChar = tm.tmHeight + tm.tmExternalLeading;
- //初始化滚动条
- SetScrollRange(hwnd, SB_VERT, 0, LINECOUNT - 1, FALSE);
- SetScrollPos(hwnd, SB_VERT, 20, FALSE);//我发现这里好像刷不刷新都一样是怎么回事?
- ReleaseDC(hwnd, hdc);
- return 0;
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- for (size_t i = 0; i < NUMLINES; i++)
- {
- StringCchLength(sysmetrics[i].szLabel, 1024, &iTarget);
- TextOut(hdc, 0, cyChar * i, sysmetrics[i].szLabel, iTarget);
- StringCchLength(sysmetrics[i].szDesc, 1024, &iTarget);
- TextOut(hdc, 22 * cxCaps, cyChar * i, sysmetrics[i].szDesc, iTarget);
- auto TextModeTemp = GetTextAlign(hdc);
- SetTextAlign(hdc, TA_RIGHT | TA_TOP);
- StringCchPrintf(szBuffer, 10, TEXT("%d"), GetSystemMetrics(sysmetrics[i].iIndex));
- StringCchLength(szBuffer, 10, &iTarget);
- TextOut(hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer, iTarget);
- SetTextAlign(hdc, TextModeTemp);
- }
- EndPaint(hwnd, &ps);
- return 0;
- case WM_VSCROLL:
- hdc = GetDC(hwnd);
- SetTextAlign(hdc, TA_TOP | TA_RIGHT);
- switch (LOWORD(wparam))
- {
- case SB_LINEUP:
- TextOut(hdc, cxClient - 10, 10, TEXT("向上滚动一行"), 6);
- break;
- case SB_LINEDOWN:
- TextOut(hdc, cxClient - 10, 10, TEXT("向下滚动一行"), 6);
- break;
- case SB_PAGEUP:
- TextOut(hdc, cxClient - 10, 10, TEXT("向上滚动一页"), 6);
- break;
- case SB_PAGEDOWN:
- TextOut(hdc, cxClient - 10, 10, TEXT("向下滚动一页"), 6);
- break;
- case SB_THUMBTRACK:
- TextOut(hdc, cxClient - 10, 10, TEXT("别抓着我不放"), 6);
- break;
- }
- ReleaseDC(hwnd, hdc);
- return 0;
- case WM_SIZE:
- hdc = GetDC(hwnd);
- StringCchPrintf(szBuffer, 128, TEXT("客户区分辨率是:%d * %d px"), LOWORD(lparam), HIWORD(lparam));
- StringCchLength(szBuffer, 128, &iTarget);
- TextOut(hdc, 10, 10, szBuffer, iTarget);
- cxClient = LOWORD(lparam);
- cyClient = HIWORD(lparam);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, message, wparam, lparam);
- }
复制代码 请问在上面的代码中我好像刷不刷新都好像一样是咋回事? |
|