【问题标题】:c++ scrollbar to windowc++ 滚动条到窗口
【发布时间】:2011-02-21 19:00:57
【问题描述】:

嘿,我在这里创建一个窗口: _hWnd = CreateWindowEx( WS_EX_CLIENTEDGE, //dwExStyle (LPCWSTR) _wndClass, //lpClassName L"", //lpWindowName WS_儿童 | WS_HSCROLL | WS_VSCROLL , //dwStyle CW_USEDEFAULT, //X CW_USEDEFAULT, //是 200, //n宽度 150, //n高度 hWndParent, //hWndParent NULL, //hMenu hInstance, //hInstance NULL //lp参数 );

我添加了滚动条 (WS_HSCROLL | WS_VSCROLL),但我该如何控制它们?

【问题讨论】:

    标签: c++ window scrollbar


    【解决方案1】:

    我认为你应该参考 MSDN 以获取更多信息,但这里是我一天写的一小段代码(只是为了让你从一些东西开始)。

    一般来说,这个想法是关于在 WindowProcedure 例程中处理特定消息(WM_HSCROLLWM_VSCROLL)。评估新滚动条位置的最简单方法(我的意思是 WinAPI 方法)是使用特定的SCROLLINFO 结构。在下面的代码块中,使用了SCROLLINFO si

      case WM_HSCROLL:
        {
          TEXTHANDLER * handler = ((TEXTHANDLER *)GetProp(hWnd, "TEXTHANDLER"));
    
          // If user is trying to scroll outside
          // of scroll range, we don't have to
          // invalidate window
          BOOL needInvalidation = TRUE;
    
          if (handler->renderer->wordWrap)
          {
            return 0;
          }
    
          si.cbSize = sizeof(si);
          si.fMask  = SIF_ALL;
          GetScrollInfo(hWnd, SB_HORZ, &si);
    
          switch (LOWORD(wParam))
          {
          case SB_LINELEFT: 
            si.nPos -= 1;
            if (si.nPos < 0)
            {
              si.nPos = 0;
              needInvalidation = FALSE;
            }
            break;
    
          case SB_LINERIGHT: 
            si.nPos += 1;
            if (si.nPos > si.nMax)
            {
              si.nPos = si.nMax;
              needInvalidation = FALSE;
            }
            break;
    
          case SB_THUMBTRACK: 
            si.nPos = si.nTrackPos;
            break;
          }
    
          si.fMask = SIF_POS;
          SetScrollInfo(hWnd, SB_HORZ, &si, TRUE);
    
          // Set new text renderer parameters
          handler->renderer->xPos = si.nPos;
    
          if (needInvalidation) InvalidateRect(hWnd, NULL, FALSE);
          return 0;
      }
    
      case WM_VSCROLL:
        {
          TEXTHANDLER * handler = ((TEXTHANDLER *)GetProp(hWnd, "TEXTHANDLER"));
    
          BOOL needInvalidation = TRUE;
    
          si.cbSize = sizeof(si);
          si.fMask  = SIF_ALL;
          GetScrollInfo(hWnd, SB_VERT, &si);
    
          switch (LOWORD(wParam))
          {
          case SB_LINEUP: 
            si.nPos -= 1;
            if (si.nPos < 0)
            {
              si.nPos = 0;
              needInvalidation = FALSE;
            }
            break;
    
          case SB_LINEDOWN: 
            si.nPos += 1;
            if (si.nPos > si.nMax)
            {
              si.nPos = si.nMax;
              needInvalidation = FALSE;
            }
            break;
    
          case SB_PAGEUP:
            si.nPos -= handler->renderer->cyCount;
            if (si.nPos < 0)
            {
              si.nPos = 0;
              needInvalidation = FALSE;
            }
            break;
    
          case SB_PAGEDOWN:
            si.nPos += handler->renderer->cyCount;
            if (si.nPos > si.nMax)
            {
              si.nPos = si.nMax;
              needInvalidation = FALSE;
            }
            break;
    
          case SB_THUMBTRACK: 
            si.nPos = si.nTrackPos;
            break;
          }
    
          si.fMask = SIF_POS;
          SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
    
          // Set new text renderer parameters
          handler->renderer->yPos = si.nPos;
    
          if (needInvalidation) InvalidateRect(hWnd, NULL, FALSE);
          return 0;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 2010-11-12
      • 1970-01-01
      • 2017-07-12
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多