views:

23

answers:

1

hey, here im creating a window: _hWnd = CreateWindowEx( WS_EX_CLIENTEDGE, //dwExStyle (LPCWSTR) _wndClass, //lpClassName L"", //lpWindowName WS_CHILD | WS_HSCROLL | WS_VSCROLL , //dwStyle CW_USEDEFAULT, //X CW_USEDEFAULT, //Y 200, //nWidth 150, //nHeight hWndParent, //hWndParent NULL, //hMenu hInstance, //hInstance NULL //lpParam );

i added the scrollbars (WS_HSCROLL | WS_VSCROLL), but how can i control them?

A: 

I think you should refer to MSDN for more information, but here is some short part of code I wrote one day (just for you to start from something).

Generally, the idea is about handling specific messages in your WindowProcedure routine (WM_HSCROLL, WM_VSCROLL). The easiest way to evaluate new scroller position (I mean, the WinAPI way) is to use a specific SCROLLINFO structure. In the following chunk of code, SCROLLINFO si is used.

  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;
    }
Kotti