鱼C论坛

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

win32滚动条刷新的到底是什么?

[复制链接]
发表于 2024-7-26 16:17:02 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <windows.h>
  2. #include"sysmets.h"
  3. #include<strsafe.h>
  4. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

  5. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  6. {
  7.     static TCHAR szAppName[] = TEXT("MyWindows");
  8.     HWND hwnd;
  9.     MSG msg;
  10.     WNDCLASS wndclass;

  11.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  12.     wndclass.lpfnWndProc = WndProc;
  13.     wndclass.cbClsExtra = 0;
  14.     wndclass.cbWndExtra = 0;
  15.     wndclass.hInstance = hInstance;
  16.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  17.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  18.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  19.     wndclass.lpszMenuName = NULL;
  20.     wndclass.lpszClassName = szAppName;

  21.     if (!RegisterClass(&wndclass))
  22.     {
  23.         MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
  24.         return 0;
  25.     }

  26.     hwnd = CreateWindow(szAppName,
  27.         TEXT("hello"),
  28.         WS_OVERLAPPEDWINDOW | WS_VSCROLL,
  29.         CW_USEDEFAULT,
  30.         CW_USEDEFAULT,
  31.         CW_USEDEFAULT,
  32.         CW_USEDEFAULT,
  33.         NULL,
  34.         NULL,
  35.         hInstance,
  36.         NULL);

  37.     ShowWindow(hwnd, iCmdShow);
  38.     UpdateWindow(hwnd);

  39.     while (GetMessage(&msg, NULL, 0, 0))
  40.     {
  41.         TranslateMessage(&msg);
  42.         DispatchMessage(&msg);
  43.     }

  44.     return msg.wParam;
  45. }

  46. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
  47. {
  48.     HDC hdc;
  49.     PAINTSTRUCT ps;
  50.     TEXTMETRIC tm;
  51.     TCHAR szBuffer[128];
  52.     static int cxChar, cxCaps, cyChar;
  53.     static int cxClient, cyClient;//客户区宽度和高度
  54.     size_t iTarget;

  55.     switch (message)
  56.     {
  57.     case WM_CREATE:
  58.         hdc = GetDC(hwnd);

  59.         GetTextMetrics(hdc, &tm);
  60.         cxChar = tm.tmAveCharWidth;//字体字符平均宽度

  61.         //如果是等宽字体不改变,如果是变宽字体(大写字母是小写字母宽度的1.5倍)
  62.         cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
  63.         //获取字符高度
  64.         cyChar = tm.tmHeight + tm.tmExternalLeading;

  65.         //初始化滚动条
  66.         SetScrollRange(hwnd, SB_VERT, 0, NUMLINES - 1, FALSE);
  67.         SetScrollPos(hwnd, SB_VERT, 20, TRUE);//刷新的到底是什么?


  68.         ReleaseDC(hwnd, hdc);
  69.         return 0;
  70.     case WM_PAINT:
  71.         hdc = BeginPaint(hwnd, &ps);
  72.         for (size_t i = 0; i < NUMLINES; i++)
  73.         {
  74.             StringCchLength(sysmetrics[i].szLabel, 1024, &iTarget);
  75.             TextOut(hdc, 0, cyChar * i, sysmetrics[i].szLabel, iTarget);

  76.             StringCchLength(sysmetrics[i].szDesc, 1024, &iTarget);
  77.             TextOut(hdc, 22 * cxCaps, cyChar * i, sysmetrics[i].szDesc, iTarget);

  78.             auto TextModeTemp = GetTextAlign(hdc);

  79.             SetTextAlign(hdc, TA_RIGHT | TA_TOP);

  80.             StringCchPrintf(szBuffer, 10, TEXT("%d"), GetSystemMetrics(sysmetrics[i].iIndex));
  81.             StringCchLength(szBuffer, 10, &iTarget);
  82.             TextOut(hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer, iTarget);
  83.             SetTextAlign(hdc, TextModeTemp);


  84.         }
  85.         EndPaint(hwnd, &ps);
  86.         return 0;
  87.     case WM_VSCROLL:

  88.         hdc = GetDC(hwnd);

  89.         SetTextAlign(hdc, TA_TOP | TA_RIGHT);
  90.         switch (LOWORD(wparam))
  91.         {
  92.         case SB_LINEUP:
  93.             TextOut(hdc, cxClient - 10, 10, TEXT("向上滚动一行"), 6);
  94.             break;
  95.         case SB_LINEDOWN:
  96.             TextOut(hdc, cxClient - 10, 10, TEXT("向下滚动一行"), 6);
  97.             break;
  98.         case SB_PAGEUP:
  99.             TextOut(hdc, cxClient - 10, 10, TEXT("向上滚动一页"), 6);
  100.             break;
  101.         case SB_PAGEDOWN:
  102.             TextOut(hdc, cxClient - 10, 10, TEXT("向下滚动一页"), 6);
  103.             break;
  104.         case SB_THUMBTRACK:
  105.             TextOut(hdc, cxClient - 10, 10, TEXT("别抓着我不放"), 6);
  106.             break;
  107.         }
  108.         ReleaseDC(hwnd, hdc);
  109.         return 0;
  110.     case WM_SIZE:
  111.         hdc = GetDC(hwnd);
  112.         StringCchPrintf(szBuffer, 128, TEXT("客户区分辨率是:%d * %d px"), LOWORD(lparam), HIWORD(lparam));
  113.         StringCchLength(szBuffer, 128, &iTarget);
  114.         TextOut(hdc, 10, 10, szBuffer, iTarget);

  115.         cxClient = LOWORD(lparam);
  116.         cyClient = HIWORD(lparam);

  117.         return 0;
  118.     case WM_DESTROY:
  119.         PostQuitMessage(0);
  120.         return 0;

  121.     }

  122.     return DefWindowProc(hwnd, message, wparam, lparam);
  123. }
复制代码
上述代码中win32滚动条刷新的到底是什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-26 04:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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