2010-05-20 149 views
1

哎,這裏即時通訊創建窗口: _hWnd = CreateWindowEx( WS_EX_CLIENTEDGE,// dwExStyle參數 (LPCWSTR)_wndClass,// lpClassName L 「」,// lpWindowName WS_CHILD | WS_HSCROLL | WS_VSCROLL, // dwStyle CW_USEDEFAULT,// X CW_USEDEFAULT,//ÿ 200,// nWidth 150,// nHeight參數 hWndParent,// hWndParent NULL,// HMENU 的hInstance,//的hInstance NULL // lpParam );C++滾動條窗口

我添加了滾動條(WS_HSCROLL | WS_VSCROLL),但我該如何控制它們?

回答

1

我認爲你應該參考MSDN獲取更多信息,但這裏是我寫一天的一小段代碼(僅僅是爲了你從某件事開始)。

通常,這個想法是關於處理WindowProcedure例程中的特定消息(WM_HSCROLL,WM_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; 
    }