Sign in to follow this  
GuitarPlayer0912

Scrollbar warps

Recommended Posts

Did anybody ever have this problem... (by the way, I'm using c++) You have a vertical or horizontal scrollbar. Let's use a horizontal one for this example. If the SCROLLINFO.nPos member is at 0 (thumb is all the way at the left), and you click on the left arrow, the thumb shoots to the right. That happened to me with this code...

LRESULT CALLBACK cEditor::prTileHandler(HWND hTileWnd, UINT msg, WPARAM wParam,
  LPARAM lParam){

  // Scrolling information
  SCROLLINFO si;
  BITMAP bm;
  RECT rcTileWnd;
  static short sXTilePos;

  switch(msg){

    // Called when Tile Selector window is resized
    case WM_SIZE:

      GetObject(tlSelectedSet.hTileset, sizeof(bm), &bm); // Just retrieves dimensions of current tileset for scrolling
      GetClientRect(hTileWnd, &rcTileWnd);

      si.cbSize = sizeof(SCROLLINFO);
      si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
      si.nMin = 0;

      si.nPage = rcTileWnd.right;
      si.nMax = bm.bmWidth - 1;
      si.nPos = sXTilePos;

      SetScrollInfo(hTileWnd, SB_HORZ, &si, TRUE);

      // WM_SIZE message handled, return 0;
      return 0;

    // Called when the Tile Selector window is scrolled horizontally
    case WM_HSCROLL:

      GetScrollInfo(hTileWnd, SB_HORZ, &si);

      {
        switch(LOWORD(wParam)){
          case SB_LINELEFT:
            si.nPos -= 1;
            break;

          case SB_LINERIGHT:
            si.nPos += 1;
            break;

          case SB_THUMBTRACK:
            si.nPos = HIWORD(wParam); // Oh and this doesn't work either
            break;
        }

        si.fMask = SIF_POS;

        SetScrollInfo(hTileWnd, SB_HORZ, &si, TRUE);
      }

      // WM_HSCROLL message handled, return 0
      return 0;

      // ... rest of messages
   }
}


Anyone have an idea? It wasn't messing up before but I had to delete all the code here to integrate it into my Map Editor Engine.

Share this post


Link to post
Share on other sites
You seem to be experiencing what looks to me like an automatic wraparound. This occurs where you have a number, say, 0, for an unsigned char, or an unsigned int. Now, you ask the program to take 0, and then to detract one (because you press the left arrow) which is basically saying - what's the number before 0 in an unsigned number. Well, there is none. If you were to look at your variables in binary - it's much more complicated than that, but point being - if you were to go less than 0 in an unsigned number, your variable would 'wrap around' to the next number - in this case, the greatest number that data type can hold.
What you should do inside your switch statement:
Quote:
switch(LOWORD(wParam))
is this:
case SB_LINELEFT:
if (si.nPos > 0)
si.nPos -= 1;
break;

case SB_LINERIGHT:
if (si.nPos < 255)
si.nPos += 1;
break;
Now, I used 255 there for the highest value that si.nPos can be, that is assuming you used an unsigned char. If you used an unsigned int, change it to 65535.
Hope this helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this