鱼C论坛

 找回密码
 立即注册
查看: 1907|回复: 2

[学习笔记] 滚动条最终代码

[复制链接]
发表于 2019-2-12 16:21:57 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 pwnmelife 于 2019-2-12 16:32 编辑
  1. #include <Windows.h>
  2. #include <strsafe.h>
  3. #include "SysMets.h"

  4. #define LINEHEIGHT 15
  5. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


  6. int WINAPI WinMain(HINSTANCE hInstance,
  7.         HINSTANCE hPrevInstance,
  8.         LPSTR lpCmdLine,
  9.         int nShowCmd) {
  10.         static TCHAR szAppName[] = TEXT("Bill");
  11.         HWND hwnd;
  12.         WNDCLASS wndclass;
  13.         MSG msg;
  14.         
  15.         wndclass.cbClsExtra = 0;
  16.         wndclass.cbWndExtra = 0;

  17.         wndclass.hInstance = hInstance;
  18.         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  19.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  20.         wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  21.         wndclass.lpszClassName = szAppName;
  22.         wndclass.lpszMenuName = NULL;
  23.         wndclass.lpfnWndProc = WndProc;
  24.         wndclass.style = CS_HREDRAW | CS_VREDRAW;

  25.         RegisterClass(&wndclass);
  26.         hwnd = CreateWindow(TEXT("Bill"), TEXT("Tip"), WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
  27.                 CW_USEDEFAULT, 40, 400, 100,
  28.                 NULL, NULL, hInstance, NULL);

  29.         ShowWindow(hwnd, nShowCmd);
  30.         UpdateWindow(hwnd);
  31.         while (GetMessage(&msg, NULL, 0, 0)) {
  32.                 TranslateMessage(&msg);
  33.                 DispatchMessage(&msg);
  34.         }
  35.         return 0;

  36. }

  37. LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
  38.         WPARAM wParam, LPARAM lParam) {
  39.         HDC hdc;
  40.         SCROLLINFO si;
  41.         PAINTSTRUCT ps;
  42.         RECT rect;
  43.         TCHAR szBuffer[128];
  44.         TEXTMETRIC tm;

  45.         static int cxChar,cxCaps,cyChar;
  46.         static int cxClient, cyClient, iMaxWidth;
  47.         int i, x, y, iVscrollPos, iVertPos, iHorzPos, iPaintBeg, iPaintEnd;
  48.         size_t len;
  49.         switch (message) {
  50.         case WM_CREATE:
  51.                 hdc = GetDC(hwnd);
  52.                 GetTextMetrics(hdc, &tm);
  53.                 cxChar = tm.tmAveCharWidth;
  54.                 cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
  55.                 cyChar = tm.tmHeight + tm.tmExternalLeading;
  56.                 ReleaseDC(hwnd, hdc);
  57.                 iMaxWidth = 40 * cxChar + 22 * cxCaps;
  58.                 return 0;
  59.         case WM_VSCROLL:
  60.                 si.cbSize = sizeof(si);
  61.                 si.fMask = SIF_ALL;
  62.                 GetScrollInfo(hwnd, SB_VERT, &si);
  63.                 iVertPos = si.nPos;
  64.                 switch (LOWORD(wParam)) {
  65.                 case SB_TOP:
  66.                         si.nPos = si.nMin;
  67.                         break;
  68.                 case SB_BOTTOM :
  69.                         si.nPos = si.nMax;
  70.                         break;
  71.                 case SB_LINEDOWN:
  72.                         si.nPos += 1;
  73.                         break;
  74.                 case SB_LINEUP:
  75.                         si.nPos -= 1;
  76.                         break;
  77.                 case SB_PAGEDOWN:
  78.                         si.nPos += si.nPage;
  79.                         break;
  80.                 case SB_PAGEUP:
  81.                         si.nPos -= si.nPage;
  82.                         break;
  83.                 case SB_THUMBPOSITION:
  84.                         si.nPos = si.nTrackPos;
  85.                         break;
  86.                 default:
  87.                         break;
  88.                 }
  89.                 if (iVertPos != si.nPos) {
  90.                         si.fMask = SIF_POS;
  91.                         SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
  92.                         ScrollWindow(hwnd, 0, cyChar * (iVertPos - si.nPos),
  93.                                 NULL, NULL);
  94.                         InvalidateRect(hwnd, NULL, TRUE);
  95.                         UpdateWindow(hwnd);
  96.                 }
  97.                 return 0;
  98.         case WM_HSCROLL:
  99.                 si.cbSize = sizeof(si);
  100.                 si.fMask = SIF_ALL;
  101.                 GetScrollInfo(hwnd, SB_HORZ, &si);
  102.                 iHorzPos = si.nPos;
  103.                 switch (LOWORD(wParam)) {
  104.                 case SB_LINELEFT:
  105.                         si.nPos -= 1;
  106.                         break;
  107.                 case SB_LINERIGHT:
  108.                         si.nPos += 1;
  109.                         break;
  110.                 case SB_PAGELEFT:
  111.                         si.nPos -= si.nPage;
  112.                         break;
  113.                 case SB_PAGERIGHT:
  114.                         si.nPos += si.nPage;
  115.                         break;
  116.                 case SB_THUMBPOSITION:
  117.                         si.nPos = si.nTrackPos;
  118.                 default:
  119.                         break;
  120.                 }
  121.                 if (iHorzPos != si.nPos) {
  122.                         si.fMask = SIF_POS;
  123.                         SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
  124.                         ScrollWindow(hwnd, cxChar * (iHorzPos - si.nPos), 0, NULL, NULL);
  125.                         InvalidateRect(hwnd, NULL, TRUE);
  126.                         UpdateWindow(hwnd);
  127.                 }
  128.                 return 0;
  129.         case WM_PAINT:
  130.                 hdc = BeginPaint(hwnd, &ps);
  131.                 si.cbSize = sizeof(si);
  132.                 si.fMask = SIF_POS;
  133.                 GetScrollInfo(hwnd, SB_VERT, &si);
  134.                 iVertPos = si.nPos;
  135.                 GetScrollInfo(hwnd, SB_HORZ, &si);
  136.                 iHorzPos = si.nPos;

  137.                 iPaintBeg = max(0, iVertPos + ps.rcPaint.top / cyChar);
  138.                 iPaintEnd = min(NUMLINES - 1,
  139.                         iVertPos + ps.rcPaint.bottom / cyChar);
  140.                 for (i  = iPaintBeg        ; i <= iPaintEnd; i++) {
  141.                         StringCchLength(sysmetrics[i].szLabel, 1024, &len);
  142.                         TextOut(hdc, 0, (i - iPaintBeg) * cyChar, sysmetrics[i].szLabel, len);
  143.                         
  144.                         StringCchLength(sysmetrics[i].szDesc, 1024, &len);
  145.                         TextOut(hdc, 22 * cxCaps, (i - iPaintBeg) * cyChar, sysmetrics[i].szDesc, len);

  146.                         SetTextAlign(hdc, TA_RIGHT | TA_TOP);
  147.                         StringCchPrintf(szBuffer, 10, TEXT("%5d"), GetSystemMetrics(sysmetrics[i].iIndex));
  148.                         StringCchLength(szBuffer, 10, &len);
  149.                         TextOut(hdc, 22 * cxCaps + 40 * cxChar, (i - iPaintBeg) * cyChar, szBuffer, len);
  150.                         SetTextAlign(hdc, TA_LEFT | TA_TOP);
  151.                 }
  152.                 EndPaint(hwnd, &ps);
  153.                 return 0;
  154.         case WM_SIZE:
  155.                 cxClient = LOWORD(lParam);
  156.                 cyClient = HIWORD(lParam);

  157.                 si.cbSize = sizeof(si);
  158.                 si.fMask = SIF_RANGE | SIF_PAGE;

  159.                 si.nMin = 0;
  160.                 si.nMax = NUMLINES - 1;
  161.                 si.nPage = cyClient / cyChar;
  162.                 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);

  163.                 si.cbSize = sizeof(si);
  164.                 si.fMask = SIF_RANGE | SIF_PAGE;
  165.                 si.nMin = 0;
  166.                 si.nMax = 2 + iMaxWidth / cxChar;
  167.                 si.nPage = cxClient / cxChar;
  168.                 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);

  169.                 return 0;
  170.         case WM_DESTROY:
  171.                 PostQuitMessage(0);
  172.                 return 0;
  173.         default:
  174.                 return DefWindowProc(hwnd, message, wParam, lParam);
  175.         }
  176. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-2-28 09:58:26 | 显示全部楼层
多谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-2-28 21:58:02 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 19:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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