Guest User

scroll example

a guest
Aug 19th, 2015
2,567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.48 KB | None | 0 0
  1. // Win32Project4.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Win32Project4.h"
  6. #include <strsafe.h>
  7. #include <Commctrl.h>
  8. #include <atltrace.h>
  9.  
  10. #define MAX_LOADSTRING 100
  11.  
  12. // Global Variables:
  13. HINSTANCE hInst;                                // current instance
  14. TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  15. TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  16.  
  17. // Forward declarations of functions included in this code module:
  18. ATOM                MyRegisterClass(HINSTANCE hInstance);
  19. BOOL                InitInstance(HINSTANCE, int);
  20. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  21. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  22. int g_scrollY = 0;
  23.  
  24. int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
  25.                      _In_opt_ HINSTANCE hPrevInstance,
  26.                      _In_ LPTSTR    lpCmdLine,
  27.                      _In_ int       nCmdShow)
  28. {
  29.     UNREFERENCED_PARAMETER(hPrevInstance);
  30.     UNREFERENCED_PARAMETER(lpCmdLine);
  31.  
  32.     // TODO: Place code here.
  33.     MSG msg;
  34.     HACCEL hAccelTable;
  35.  
  36.     // Initialize global strings
  37.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  38.     LoadString(hInstance, IDC_WIN32PROJECT4, szWindowClass, MAX_LOADSTRING);
  39.     MyRegisterClass(hInstance);
  40.  
  41.     // Perform application initialization:
  42.     if (!InitInstance (hInstance, nCmdShow))
  43.     {
  44.         return FALSE;
  45.     }
  46.  
  47.     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT4));
  48.  
  49.     // Main message loop:
  50.     while (GetMessage(&msg, NULL, 0, 0))
  51.     {
  52.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  53.         {
  54.             TranslateMessage(&msg);
  55.             DispatchMessage(&msg);
  56.         }
  57.     }
  58.  
  59.     return (int) msg.wParam;
  60. }
  61.  
  62.  
  63.  
  64. //
  65. //  FUNCTION: MyRegisterClass()
  66. //
  67. //  PURPOSE: Registers the window class.
  68. //
  69. ATOM MyRegisterClass(HINSTANCE hInstance)
  70. {
  71.     WNDCLASSEX wcex;
  72.  
  73.     wcex.cbSize = sizeof(WNDCLASSEX);
  74.  
  75.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  76.     wcex.lpfnWndProc    = WndProc;
  77.     wcex.cbClsExtra     = 0;
  78.     wcex.cbWndExtra     = 0;
  79.     wcex.hInstance      = hInstance;
  80.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT4));
  81.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  82.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  83.     wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WIN32PROJECT4);
  84.     wcex.lpszClassName  = szWindowClass;
  85.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  86.  
  87.     return RegisterClassEx(&wcex);
  88. }
  89.  
  90. //
  91. //   FUNCTION: InitInstance(HINSTANCE, int)
  92. //
  93. //   PURPOSE: Saves instance handle and creates main window
  94. //
  95. //   COMMENTS:
  96. //
  97. //        In this function, we save the instance handle in a global variable and
  98. //        create and display the main program window.
  99. //
  100. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  101. {
  102.    HWND hWnd;
  103.  
  104.    hInst = hInstance; // Store instance handle in our global variable
  105.  
  106.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_VSCROLL,
  107.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  108.  
  109.    if (!hWnd)
  110.    {
  111.       return FALSE;
  112.    }
  113.  
  114.    ShowWindow(hWnd, nCmdShow);
  115.    UpdateWindow(hWnd);
  116.  
  117.    return TRUE;
  118. }
  119.  
  120. //
  121. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  122. //
  123. //  PURPOSE:  Processes messages for the main window.
  124. //
  125. //  WM_COMMAND  - process the application menu
  126. //  WM_PAINT    - Paint the main window
  127. //  WM_DESTROY  - post a quit message and return
  128. //
  129. //
  130. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  131. {
  132.     int wmId, wmEvent;
  133.     PAINTSTRUCT ps;
  134.     HDC hdc;
  135.  
  136.     switch (message)
  137.     {
  138.     case WM_COMMAND:
  139.         wmId    = LOWORD(wParam);
  140.         wmEvent = HIWORD(wParam);
  141.         // Parse the menu selections:
  142.         switch (wmId)
  143.         {
  144.         case IDM_ABOUT:
  145.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  146.             break;
  147.         case IDM_EXIT:
  148.             DestroyWindow(hWnd);
  149.             break;
  150.         default:
  151.             return DefWindowProc(hWnd, message, wParam, lParam);
  152.         }
  153.         break;
  154.     case WM_LBUTTONDOWN:
  155.     {
  156.         SCROLLINFO si = { 0 };
  157.         si.cbSize = sizeof(SCROLLINFO);
  158.         si.fMask = SIF_POS;
  159.         si.nPos = 0;
  160.         si.nTrackPos = 0;
  161.         GetScrollInfo(hWnd, SB_VERT, &si);
  162.         WCHAR buf[20];
  163.         StringCchPrintf(buf, 20, L"pos = %d\n", si.nPos);
  164.         OutputDebugString(buf);
  165.         break;
  166.     }
  167.     case WM_VSCROLL:
  168.     {
  169.         auto action = LOWORD(wParam);
  170.         HWND hScroll = (HWND)lParam;
  171.         int pos = -1;
  172.         if (action == SB_THUMBPOSITION || action == SB_THUMBTRACK) {
  173.             pos = HIWORD(wParam);
  174.         } else if (action == SB_LINEDOWN) {
  175.             pos = g_scrollY + 30;
  176.         } else if (action == SB_LINEUP) {
  177.             pos = g_scrollY - 30;
  178.         }
  179.         if (pos == -1)
  180.             break;
  181.         WCHAR buf[20];
  182.         SCROLLINFO si = { 0 };
  183.         si.cbSize = sizeof(SCROLLINFO);
  184.         si.fMask = SIF_POS;
  185.         si.nPos = pos;
  186.         si.nTrackPos = 0;
  187.         SetScrollInfo(hWnd, SB_VERT, &si, true);
  188.         GetScrollInfo(hWnd, SB_VERT, &si);
  189.         pos = si.nPos;
  190.         POINT pt;
  191.         pt.x = 0;
  192.         pt.y = pos - g_scrollY;
  193.         auto hdc = GetDC(hWnd);
  194.         LPtoDP(hdc, &pt, 1);
  195.         ReleaseDC(hWnd, hdc);
  196.         ScrollWindow(hWnd, 0, -pt.y, NULL, NULL);
  197.         g_scrollY = pos;
  198.         return 0;
  199.     }
  200.     case WM_CREATE:
  201.     {
  202.         for (int i = 0; i < 100; ++i) {
  203.             auto hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"",
  204.                 WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 10, 30 * i, 250, 21, hWnd, NULL, hInst, NULL);
  205.             wchar_t buf[10];
  206.             StringCchPrintf(buf, 10, L"%d", i);
  207.             SetWindowText(hEdit, buf);
  208.         }
  209.         RECT rc = { 0 };
  210.         GetClientRect(hWnd, &rc);
  211.         SCROLLINFO si = { 0 };
  212.         si.cbSize = sizeof(SCROLLINFO);
  213.         si.fMask = SIF_ALL;
  214.         si.nMin = 0;
  215.         si.nMax = 30 * 99 + 21;
  216.         si.nPage = (rc.bottom - rc.top);
  217.         si.nPos = 0;
  218.         si.nTrackPos = 0;
  219.         SetScrollInfo(hWnd, SB_VERT, &si, true);
  220.         return 0;
  221.     }
  222.     case WM_SIZE:
  223.     {
  224.         RECT rc = { 0 };
  225.         GetClientRect(hWnd, &rc);
  226.         SCROLLINFO si = { 0 };
  227.         si.cbSize = sizeof(SCROLLINFO);
  228.         si.fMask = SIF_ALL;
  229.         si.nMin = 0;
  230.         si.nMax = 30 * 99 + 21;
  231.         si.nPage = (rc.bottom - rc.top);
  232.         si.nPos = 0;
  233.         si.nTrackPos = 0;
  234.         SetScrollInfo(hWnd, SB_VERT, &si, true);
  235.         break;
  236.     }
  237.     case WM_PAINT:
  238.         hdc = BeginPaint(hWnd, &ps);
  239.         // TODO: Add any drawing code here...
  240.         EndPaint(hWnd, &ps);
  241.         break;
  242.     case WM_DESTROY:
  243.         PostQuitMessage(0);
  244.         break;
  245.     default:
  246.         return DefWindowProc(hWnd, message, wParam, lParam);
  247.     }
  248.     return 0;
  249. }
  250.  
  251. // Message handler for about box.
  252. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  253. {
  254.     UNREFERENCED_PARAMETER(lParam);
  255.     switch (message)
  256.     {
  257.     case WM_INITDIALOG:
  258.         return (INT_PTR)TRUE;
  259.  
  260.     case WM_COMMAND:
  261.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  262.         {
  263.             EndDialog(hDlg, LOWORD(wParam));
  264.             return (INT_PTR)TRUE;
  265.         }
  266.         break;
  267.     }
  268.     return (INT_PTR)FALSE;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment