鱼C论坛

 找回密码
 立即注册
查看: 2795|回复: 3

关于滚动条的问题

[复制链接]
发表于 2017-5-10 18:44:55 | 显示全部楼层 |阅读模式
5鱼币

滚动条滚动后程序界面内容错乱了,不知道为何会这样
大家帮忙看看代码




#include<Windows.h>
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("this program requires Windows NT!"), szAppName, MB_ICONERROR);
                return 0;
        }
        hwnd = CreateWindow(szAppName,
                TEXT("by:"),
                WS_OVERLAPPEDWINDOW |WS_VSCROLL,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                500,
                400,
                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;
        SCROLLINFO si;
        TEXTMETRICW tm;
        TCHAR szBuffer[255];
        static int cxClient, cyClient,cxchar,cychar,iVertPos;
        switch (message)
        {
        case WM_SIZE:
                cxClient = LOWORD(lParam);
                cyClient = HIWORD(lParam);

                si.cbSize = sizeof(si);
                si.fMask = SIF_RANGE | SIF_PAGE;
                si.nMin = 0;
                si.nMax = 30 - 1;
                si.nPage = cyClient / cychar;
                SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
                return 0;
        case WM_VSCROLL:
                si.cbSize = sizeof(si);
                si.fMask = SIF_ALL;
                GetScrollInfo(hwnd, SB_VERT, &si);
                iVertPos = si.nPos;
                switch (LOWORD(wParam))
                {
                case SB_LINEDOWN:
                        si.nPos = si.nPos + 1;
                        break;
                case SB_LINEUP:
                        si.nPos = si.nPos - 1;
                        break;
                default:
                        break;

                }
                si.fMask = SIF_POS;
                SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
                GetScrollInfo(hwnd, SB_VERT, &si);
                if (si.nPos != iVertPos)
                {
                        ScrollWindow(hwnd, 0, cychar*(iVertPos - si.nPos), NULL, NULL);
                        UpdateWindow(hwnd);
                }
                return 0;

        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);
                si.cbSize = sizeof(si);
                si.fMask = SIF_POS;
                GetScrollInfo(hwnd, SB_VERT, &si);
                iVertPos = si.nPos;
               
                for (int i = 0; i < 30; i++)
                {
                        wsprintf(szBuffer, TEXT("%d"), i);
                        TextOut(hdc, 0, i*cychar, szBuffer, lstrlen(szBuffer));
                }
                EndPaint(hwnd, &ps);
                return 0;
        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
}

QQ截图20170510184222.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-11 01:56:25 | 显示全部楼层
朋友不妨可以参考下咱课堂上的演示代码:

  1. /* -------------------------------------------------------------------
  2.                   MyWindows.c -- 基本窗口模型
  3.               《Windows 程序设计(SDK)》视频教程
  4. --------------------------------------------------------------------*/

  5. #include <windows.h>
  6. #include "strsafe.h"
  7. #include "sysmets.h"

  8. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

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

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

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

  30.         hwnd = CreateWindow(szAppName,
  31.                 TEXT("鱼C工作室"),
  32.                 WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  33.                 CW_USEDEFAULT,
  34.                 CW_USEDEFAULT,
  35.                 CW_USEDEFAULT,
  36.                 CW_USEDEFAULT,
  37.                 NULL,
  38.                 NULL,
  39.                 hInstance,
  40.                 NULL);

  41.         ShowWindow(hwnd, iCmdShow);
  42.         UpdateWindow(hwnd);

  43.         while (GetMessage(&msg, NULL, 0, 0))
  44.         {
  45.                 TranslateMessage(&msg);
  46.                 DispatchMessage(&msg);
  47.         }

  48.         return msg.wParam;
  49. }

  50. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  51. {
  52.         HDC hdc;
  53.         PAINTSTRUCT ps;
  54.         TEXTMETRIC tm;
  55.         SCROLLINFO si;
  56.         TCHAR szBuffer[10];
  57.         static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth;
  58.         size_t iTarget;
  59.         int i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd;

  60.         switch (message)
  61.         {
  62.         case WM_CREATE:
  63.                 hdc = GetDC(hwnd);

  64.                 GetTextMetrics(hdc, &tm);
  65.                 cxChar = tm.tmAveCharWidth;
  66.                 cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
  67.                 cyChar = tm.tmHeight + tm.tmExternalLeading;

  68.                 ReleaseDC(hwnd, hdc);

  69.                 // 设置客户区的最大宽度
  70.                 // (我们这里设置为 22 个大写字符的宽度 + 40 个小写字符的宽度)
  71.                 iMaxWidth = 22 * cxCaps + 40 * cxChar;
  72.                 return 0;

  73.         case WM_SIZE:
  74.                 cxClient = LOWORD(lParam);
  75.                 cyClient = HIWORD(lParam);

  76.                 // 设置垂直滚动条
  77.                 si.cbSize = sizeof(si);
  78.                 si.fMask = SIF_RANGE | SIF_PAGE;
  79.                 si.nMin = 0;
  80.                 si.nMax = NUMLINES - 1;
  81.                 si.nPage = cyClient / cyChar;
  82.                 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);

  83.                 // 设置水平滚动条
  84.                 si.cbSize = sizeof(si);
  85.                 si.fMask = SIF_RANGE | SIF_PAGE;
  86.                 si.nMin = 0;
  87.                 si.nMax = 2 + iMaxWidth / cxChar;
  88.                 si.nPage = cxClient / cxChar;
  89.                 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);

  90.                 return 0;

  91.         case WM_VSCROLL:
  92.                 // 获得滚动条信息
  93.                 si.cbSize = sizeof(si);
  94.                 si.fMask = SIF_ALL;
  95.                 GetScrollInfo(hwnd, SB_VERT, &si);

  96.                 // 获得滑块的位置
  97.                 iVertPos = si.nPos;

  98.                 // 根据消息的通知码改变滚动条滑块的位置
  99.                 switch (LOWORD(wParam))
  100.                 {
  101.                         // 用户点击键盘 Home 按键
  102.                 case SB_TOP:
  103.                         si.nPos = si.nMin;
  104.                         break;

  105.                         // 用户点击键盘 End 按键
  106.                 case SB_BOTTOM:
  107.                         si.nPos = si.nMax;
  108.                         break;

  109.                         // 用户点击滚动条上边的三角形
  110.                 case SB_LINEUP:
  111.                         si.nPos -= 1;
  112.                         break;

  113.                         // 用户点击滚动条下边的三角形
  114.                 case SB_LINEDOWN:
  115.                         si.nPos += 1;
  116.                         break;

  117.                         // 用户点击滑块上边的滚动条轴
  118.                 case SB_PAGEUP:
  119.                         si.nPos -= si.nPage;
  120.                         break;

  121.                         // 用户点击滑块下边的滚动条轴
  122.                 case SB_PAGEDOWN:
  123.                         si.nPos += si.nPage;
  124.                         break;

  125.                         // 用户拖动滚动条
  126.                 case SB_THUMBTRACK:
  127.                         si.nPos = si.nTrackPos;
  128.                         break;

  129.                 default:
  130.                         break;
  131.                 }

  132.                 // 设置改变后的滚动条滑块位置
  133.                 si.fMask = SIF_POS;
  134.                 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);

  135.                 // 获得滚动条滑块的位置,由于窗口调整,它可能不是同一个值
  136.                 GetScrollInfo(hwnd, SB_VERT, &si);

  137.                 // 与此前的保存的值进行比较,如果不同则滚动窗口
  138.                 if (si.nPos != iVertPos)
  139.                 {
  140.                         ScrollWindow(hwnd, 0, cyChar * (iVertPos - si.nPos), NULL, NULL);
  141.                         UpdateWindow(hwnd);
  142.                 }

  143.                 return 0;

  144.         case WM_HSCROLL:
  145.                 // 获得滚动条信息
  146.                 si.cbSize = sizeof(si);
  147.                 si.fMask = SIF_ALL;
  148.                 GetScrollInfo(hwnd, SB_HORZ, &si);

  149.                 // 获得滑块的位置
  150.                 iHorzPos = si.nPos;

  151.                 // 根据消息的通知码改变滚动条滑块的位置
  152.                 switch (LOWORD(wParam))
  153.                 {
  154.                         // 用户点击滚动条左边的三角形
  155.                 case SB_LINELEFT:
  156.                         si.nPos -= 1;
  157.                         break;

  158.                         // 用户点击滚动条右边的三角形
  159.                 case SB_LINERIGHT:
  160.                         si.nPos += 1;
  161.                         break;

  162.                         // 用户点击滑块左边的滚动条轴
  163.                 case SB_PAGELEFT:
  164.                         si.nPos -= si.nPage;
  165.                         break;

  166.                         // 用户点击滑块右边的滚动条轴
  167.                 case SB_PAGERIGHT:
  168.                         si.nPos += si.nPage;
  169.                         break;

  170.                         // 滚动条最终停留的位置
  171.                 case SB_THUMBPOSITION:
  172.                         si.nPos = si.nTrackPos;
  173.                         break;

  174.                 default:
  175.                         break;
  176.                 }

  177.                 // 设置改变后的滚动条滑块位置
  178.                 si.fMask = SIF_POS;
  179.                 SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);

  180.                 // 获得滚动条滑块的位置,由于窗口调整,它可能不是同一个值
  181.                 GetScrollInfo(hwnd, SB_HORZ, &si);

  182.                 // 与此前的保存的值进行比较,如果不同则滚动窗口
  183.                 if (si.nPos != iHorzPos)
  184.                 {
  185.                         ScrollWindow(hwnd, cxChar * (iHorzPos - si.nPos), 0, NULL, NULL);
  186.                 }

  187.                 return 0;

  188.         case WM_PAINT:
  189.                 hdc = BeginPaint(hwnd, &ps);

  190.                 // 获得垂直滚动条的位置
  191.                 si.cbSize = sizeof (si);
  192.                 si.fMask = SIF_POS;
  193.                 GetScrollInfo(hwnd, SB_VERT, &si);
  194.                 iVertPos = si.nPos;

  195.                 // 获得水平滚动条的位置
  196.                 GetScrollInfo(hwnd, SB_HORZ, &si);
  197.                 iHorzPos = si.nPos;

  198.                 // 计算需要重绘的区域并确保范围在[0, NUMLINES-1]之中
  199.                 iPaintBeg = max(0, iVertPos + ps.rcPaint.top / cyChar);
  200.                 iPaintEnd = min(NUMLINES - 1, iVertPos + ps.rcPaint.bottom / cyChar);

  201.                 for (i = iPaintBeg; i <= iPaintEnd; i++)
  202.                 {
  203.                         // 计算此时重绘无效区域的位置
  204.                         x = cxChar * (1 - iHorzPos);
  205.                         y = cyChar * (i - iVertPos);

  206.                         StringCchLength(sysmetrics[i].szLabel, 1024, &iTarget);
  207.                         TextOut(hdc, x, y, sysmetrics[i].szLabel, iTarget);

  208.                         StringCchLength(sysmetrics[i].szDesc, 1024, &iTarget);
  209.                         TextOut(hdc, x + 22 * cxCaps, y, sysmetrics[i].szDesc, iTarget);

  210.                         SetTextAlign(hdc, TA_RIGHT | TA_TOP);
  211.                         StringCchPrintf(szBuffer, 10, TEXT("%5d"), GetSystemMetrics(sysmetrics[i].iIndex));
  212.                         StringCchLength(szBuffer, 10, &iTarget);
  213.                         TextOut(hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer, iTarget);

  214.                         SetTextAlign(hdc, TA_LEFT | TA_TOP);
  215.                 }

  216.                 EndPaint(hwnd, &ps);
  217.                 return 0;

  218.         case WM_DESTROY:
  219.                 PostQuitMessage(0);
  220.                 return 0;
  221.         }

  222.         return DefWindowProc(hwnd, message, wParam, lParam);
  223. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-11 21:51:54 | 显示全部楼层
首先,你使用ScrollWindow函数时就要告诉自己,不要在滚动时重绘整个客户区。
这个函数只会产生一小块无效区域(存放在ps.rcPaint中),你要做的是在这块区域里面绘制。

所以你要用到ps.rcPaint这个矩形。代码修改的部分我标记出来了,和小甲鱼老师给的代码的核心是一样的,自己多想想,实在是百度都找不到的就回帖问问。
#include<Windows.h>
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("this program requires Windows NT!"), szAppName, MB_ICONERROR);
                return 0;
        }
        hwnd = CreateWindow(szAppName,
                TEXT("by:"),
                WS_OVERLAPPEDWINDOW | WS_VSCROLL,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                500,
                400,
                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;
        SCROLLINFO si;
        TEXTMETRICW tm;
        TCHAR szBuffer[255];
        static int cxClient, cyClient, cxchar, cychar, iVertPos;
        static RECT ScrollRect;
        int FirstLine, LastLine;

        switch (message)
        {
        case WM_SIZE:
                cxClient = LOWORD(lParam);
                cyClient = HIWORD(lParam);
                GetClientRect(hwnd, &ScrollRect);

                si.cbSize = sizeof(si);
                si.fMask = SIF_RANGE | SIF_PAGE;
                si.nMin = 0;
                si.nMax = 30 - 1;
                si.nPage = cyClient / cychar;
                SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
                return 0;

        case WM_VSCROLL:
                si.cbSize = sizeof(si);
                si.fMask = SIF_ALL;
                GetScrollInfo(hwnd, SB_VERT, &si);
                iVertPos = si.nPos;
                switch (LOWORD(wParam))
                {
                case SB_LINEDOWN:
                        si.nPos = si.nPos + 1;
                        break;
                case SB_LINEUP:
                        si.nPos = si.nPos - 1;
                        break;
                default:
                        break;

                }
                si.fMask = SIF_POS;
                SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
                GetScrollInfo(hwnd, SB_VERT, &si);
                if (si.nPos != iVertPos)
                {
                        ScrollWindow(hwnd, 0, cychar*(iVertPos - si.nPos), NULL, NULL);
                        UpdateWindow(hwnd);
                }
                return 0;

        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);
                si.cbSize = sizeof(si);
                si.fMask = SIF_ALL;
                GetScrollInfo(hwnd, SB_VERT, &si);
                iVertPos = si.nPos;

                FirstLine = min(0, ps.rcPaint.top / cychar + iVertPos);
                LastLine = max(29, ps.rcPaint.bottom / cychar + iVertPos); //控制从第几行开始绘制,在第几行结束绘制



                for (int i = FirstLine; i <= LastLine; i++)
                {
                        wsprintf(szBuffer, TEXT("%d"), i);
                        TextOut(hdc, 0, (i-iVertPos)*cychar, szBuffer, lstrlen(szBuffer));
                }
                EndPaint(hwnd, &ps);
                return 0;

        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-2 18:41:18 | 显示全部楼层
响应WM_PAINT时:
              
  1. FirstLine = min(0, ps.rcPaint.top / cychar + iVertPos);
  2.                 LastLine = max(29, ps.rcPaint.bottom / cychar + iVertPos); //控制从第几行开始绘制,在第几行结束绘制


  3.                 for (int i = FirstLine; i <= LastLine; i++)
  4.                 {
  5.                         wsprintf(szBuffer, TEXT("%d"), i);
  6.                         TextOut(hdc, 0, (i-iVertPos)*cychar, szBuffer, lstrlen(szBuffer));
  7.                 }
复制代码


虽然我都不懂为什么要(i-vertPos)而不是直接i*cychar
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 18:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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