By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

ScrollBar issue

P: 48
Hi windows API programmers,

I'm so close to making my vertical scrollbar to work properly, except for:

After pushing or dragging the scrollbar the window scrolls properly until I let go of the mouse. When I let go the window scrolls another unit to which I don't want to happen. I want to have the scrollbar place the window where it was set rather than 1 unit up or down.

Another odd thing is the WM_VSCROLL message is called once when the mouse is down and once when the mouse is up. I think if I can make it only call it for mousedown then it would fix this problem.


Suggestions? Thanks.

Expand|Select|Wrap|Line Numbers
  1. case WM_VSCROLL:
  2. {
  3.     int nScrollCode = (int)LOWORD(wParam);
  4.     int nPos = (short int)HIWORD(wParam);
  5.     controlVbar(nScrollCode, nPos);
  6.  
  7.     if(curScrollPos!=oldScrollPos && curScrollPos > 0 && curScrollPos < 100)
  8.     {
  9.         ScrollWindowEx(hwndMain, 0, oldScrollPos-curScrollPos, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN);
  10.         RedrawWindow(hwndMain, NULL, NULL, RDW_ERASE | RDW_INVALIDATE );
  11.  
  12.     }
  13. }    
  14. break;
  15.  
The following is the controlVbar function called from the above line 5.
Expand|Select|Wrap|Line Numbers
  1. void mainWindow::controlVbar(int nScrollCode, int nPos)
  2. {
  3.  
  4.     switch (nScrollCode)
  5.     {
  6.         // Include code that checks for other values of nScrollCode.
  7.         // ...
  8.         case SB_THUMBTRACK:
  9.         {
  10.             oldScrollPos=curScrollPos;
  11.             curScrollPos=nPos;
  12.             SetScrollPos(hwndMain, SB_VERT, nPos, TRUE);
  13.         }
  14.         break;
  15.  
  16.         case SB_LINEUP:
  17.         {
  18.             if(curScrollPos > 0)
  19.             {
  20.                 oldScrollPos=curScrollPos;
  21.                 curScrollPos=oldScrollPos-1;
  22.             }
  23.             SetScrollPos(hwndMain, SB_VERT, curScrollPos, TRUE);
  24.         }
  25.         break;
  26.  
  27.         case SB_LINEDOWN:
  28.         {
  29.             oldScrollPos=curScrollPos;
  30.             if(curScrollPos < 100)
  31.             {
  32.                 oldScrollPos=curScrollPos;
  33.                 curScrollPos=oldScrollPos+1;
  34.             }
  35.             SetScrollPos(hwndMain, SB_VERT, curScrollPos, TRUE);
  36.         }
  37.         break;
  38.  
  39.     }
  40.  
  41.  
  42. }
  43.  
Jun 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: 48
to complete this with an answer I figured out:

first make 3 global int
Expand|Select|Wrap|Line Numbers
  1. int oldScrollPos=0;
  2. int curScrollPos=1;
  3. int lastScroll;
  4.  

Then add these functions:
Expand|Select|Wrap|Line Numbers
  1. void msWheel(HWND hwnd, int zDelta)
  2. {
  3.     if(zDelta > 0)
  4.         zDelta=120;
  5.     else
  6.         zDelta=-120;
  7.  
  8.     oldScrollPos=curScrollPos;
  9.     if(curScrollPos <= 680 && zDelta < 0)
  10.         curScrollPos+=120;
  11.     else if(curScrollPos > 680 && zDelta < 0)
  12.         curScrollPos=799;
  13.  
  14.     if(curScrollPos > 120 && zDelta > 0)
  15.         curScrollPos+=-120;
  16.     else if(curScrollPos < 120 && zDelta > 0)
  17.         curScrollPos=1;
  18.  
  19.     SetScrollPos(hwnd, SB_VERT, curScrollPos, TRUE);
  20.  
  21.     if(curScrollPos!=lastScroll && curScrollPos > 0 && curScrollPos < 800)
  22.     {
  23.         ScrollWindowEx(hwnd, 0, oldScrollPos-curScrollPos,
  24.                         NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN);
  25.         RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE );//| RDW_ALLCHILDREN
  26.  
  27.         lastScroll=curScrollPos;
  28.  
  29.     }
  30.  
  31. }
  32.  
  33. void controlVbar(int nScrollCode, int nPos, HWND hwnd)
  34. {
  35.  
  36.     switch (nScrollCode)
  37.     {
  38.         // Include code that checks for other values of nScrollCode.
  39.         // ...
  40.         case SB_THUMBTRACK:
  41.         {
  42.             oldScrollPos=curScrollPos;
  43.             curScrollPos=nPos;
  44.             SetScrollPos(hwnd, SB_VERT, nPos, TRUE);
  45.         }
  46.         break;
  47.  
  48.         case SB_LINEUP:
  49.         {
  50.             if(curScrollPos > 0)
  51.             {
  52.                 oldScrollPos=curScrollPos;
  53.                 if(curScrollPos > 10)
  54.                     curScrollPos=oldScrollPos-10;
  55.                 else
  56.                     curScrollPos=1;
  57.             }
  58.             SetScrollPos(hwnd, SB_VERT, curScrollPos, TRUE);
  59.         }
  60.         break;
  61.  
  62.         case SB_LINEDOWN:
  63.         {
  64.             oldScrollPos=curScrollPos;
  65.             if(curScrollPos < 800)
  66.             {
  67.                 oldScrollPos=curScrollPos;
  68.                 curScrollPos=oldScrollPos+10;
  69.             }
  70.             SetScrollPos(hwnd, SB_VERT, curScrollPos, TRUE);
  71.         }
  72.         break;
  73.  
  74.     }
  75.  
  76.             if(curScrollPos!=lastScroll && curScrollPos > 0 && curScrollPos < 800)
  77.             {
  78.                 ScrollWindowEx(hwnd, 0, oldScrollPos-curScrollPos,
  79.                                 NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN);
  80.                 RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE );//| RDW_ALLCHILDREN
  81.  
  82.                 lastScroll=curScrollPos;
  83.             }
  84.  
  85.  
  86. }
  87.  
Then to call those functions add these cases in your CALLBACK switch:

Expand|Select|Wrap|Line Numbers
  1. case WM_VSCROLL:
  2.     {
  3.         int nScrollCode = (int)LOWORD(wParam);
  4.         int nPos = (short int)HIWORD(wParam);//short int?
  5.         controlVbar(nScrollCode, nPos, hwnd);
  6.     }
  7.         break;
  8.     case WM_MOUSEWHEEL:
  9.         msWheel(hwnd, GET_WHEEL_DELTA_WPARAM(wParam));
  10.  
  11.  
  12.     break;
  13.  
Jun 18 '07 #2

P: 48
To create the scrollbar I added the vertical scrollbar style on the parent dialog.

then did this code in the main:

Expand|Select|Wrap|Line Numbers
  1. SCROLLINFO si = {sizeof(SCROLLINFO), 
  2.             SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS, 1, 799, 0, 0, 
  3.             0};
  4.     SetScrollInfo (hwnd, SB_VERT, &si, TRUE);
  5.  
Jun 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.