How to make to scrollbars work?

This is a discussion on How to make to scrollbars work? within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm having some trouble trying to make my vertical scrollbar work. My horizontal scrollbar works, but the vertical doesn't ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    How to make to scrollbars work?

    Hi,

    I'm having some trouble trying to make my vertical scrollbar work. My horizontal scrollbar works, but the vertical doesn't work properly. It seems that the horizontal scrollbar works in parallel with the vertical scrollbar.

    I can't figure out whats my problem, can someone help me out?

    Here's my code:
    Code:
    LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC		hdc;
    	static RECT rect;
    	static int iHscrollPos, iVscrollPos, cxChar, cyChar,cxCaps, cxClient, cyClient;
    	HCURSOR hCursor ;
    	PAINTSTRUCT ps ;
    	SCROLLINFO  si;
    	TEXTMETRIC  tm ;
    	
    	switch (message)
    	{
    		case WM_CREATE:
    			hdc = GetDC (hwnd) ;
    			
    			GetTextMetrics (hdc, &tm) ;
    			cxChar = tm.tmAveCharWidth ;
    			cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
    			cyChar = tm.tmHeight + tm.tmExternalLeading ;
    
    			SetScrollRange (hwnd, SB_HORZ, 0, 100, FALSE);
    			SetScrollPos (hwnd, SB_HORZ, iHscrollPos, TRUE);
    
    			ReleaseDC (hwnd, hdc) ;
    			return 0;
    
    		case WM_SIZE:
    			cxClient = LOWORD (lParam) ;
    			cyClient = HIWORD (lParam) ;
    			return 0;
    
    		case WM_HSCROLL:
    			switch(LOWORD(wParam))
    			{
    				case SB_LINERIGHT:
    					iHscrollPos += 1;
    					break;
    				case SB_LINELEFT:
    					iHscrollPos -= 1;
    					break;
    				case SB_THUMBPOSITION:
    					iHscrollPos = HIWORD (wParam);
    					break;
    				default:
    					break;
    			}
    			iHscrollPos = max(0, min(iHscrollPos,100));
    
    			if(iHscrollPos != GetScrollPos(hwnd, SB_HORZ))
    			{
    				SetScrollPos(hwnd, SB_HORZ, iHscrollPos, TRUE);
    				InvalidateRect(hwnd,NULL,TRUE);
    			}
    			return 0;
    		
    		case WM_VSCROLL: //to get all the vertical scroll bar information
    			switch (LOWORD (wParam))
    			{
    				case SB_TOP:
    					si.nPos = si.nMin ; 
    					break;
    				case SB_BOTTOM:
    					si.nPos = si.nMax ; 
    					break;
    				case SB_LINEUP:
    					//iHscrollPos -= 1;
    					si.nPos -= 1 ;
    					break;
    				case SB_LINEDOWN:
    					//iHscrollPos += 1;
    					si.nPos += 1 ;
    					break;
    				case SB_PAGEUP:
    					si.nPos -= si.nPage ;
    					break ;
    				case SB_PAGEDOWN:
    					si.nPos += si.nPage ;
    					break ;
    				case SB_THUMBTRACK:
    					si.nPos = si.nTrackPos ;
    					break ;
    				default:
    					break ;
    			}
    			si.fMask = SIF_POS ;
    			iHscrollPos = max(0, min(iHscrollPos,100));
    			
    			if(iHscrollPos != GetScrollPos(hwnd, SB_VERT))
    			{
    				SetScrollPos(hwnd, SB_VERT, iHscrollPos, TRUE);
    				InvalidateRect(hwnd,NULL,TRUE);
    			}
    			return 0 ;
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    			EndPaint(hwnd, &ps);
    			return 0;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    
    	}
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    The vertical and horizontal scrollbars are using the same variable for the scroll position (iHscrollpos). Check out my comments in blue.

    Code:
    LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC		hdc;
    	static RECT rect;
    	static int iHscrollPos, iVscrollPos, cxChar, cyChar,cxCaps, cxClient, cyClient;
    	HCURSOR hCursor ;
    	PAINTSTRUCT ps ;
    	SCROLLINFO  si;
    	TEXTMETRIC  tm ;
    	
    	switch (message)
    	{
    		case WM_CREATE:
    			hdc = GetDC (hwnd) ;
    			
    			GetTextMetrics (hdc, &tm) ;
    			cxChar = tm.tmAveCharWidth ;
    			cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
    			cyChar = tm.tmHeight + tm.tmExternalLeading ;
    
    			SetScrollRange (hwnd, SB_HORZ, 0, 100, FALSE);
    			SetScrollPos (hwnd, SB_HORZ, iHscrollPos, TRUE);
    //horiz position is set, but what about vertical?
    			ReleaseDC (hwnd, hdc) ;
    			return 0;
    
    		case WM_SIZE:
    			cxClient = LOWORD (lParam) ;
    			cyClient = HIWORD (lParam) ;
    			return 0;
    
    		case WM_HSCROLL:
    			switch(LOWORD(wParam))
    			{
    				case SB_LINERIGHT:
    					iHscrollPos += 1;
    					break;
    				case SB_LINELEFT:
    					iHscrollPos -= 1;
    					break;
    				case SB_THUMBPOSITION:
    					iHscrollPos = HIWORD (wParam);
    					break;
    				default:
    					break;
    			}
    			iHscrollPos = max(0, min(iHscrollPos,100));
    
    			if(iHscrollPos != GetScrollPos(hwnd, SB_HORZ))
    			{
    				SetScrollPos(hwnd, SB_HORZ, iHscrollPos, TRUE);
    				InvalidateRect(hwnd,NULL,TRUE);
    			}
    			return 0;
    		
    		case WM_VSCROLL: //to get all the vertical scroll bar information
    			switch (LOWORD (wParam))
    			{
    				case SB_TOP:
    					si.nPos = si.nMin ; 
    					break;
    				case SB_BOTTOM:
    					si.nPos = si.nMax ; 
    					break;
    				case SB_LINEUP:
    					//iHscrollPos -= 1;
    					si.nPos -= 1 ;
    					break;
    				case SB_LINEDOWN:
    					//iHscrollPos += 1;
    					si.nPos += 1 ;
    					break;
    				case SB_PAGEUP:
    					si.nPos -= si.nPage ;
    					break ;
    				case SB_PAGEDOWN:
    					si.nPos += si.nPage ;
    					break ;
    				case SB_THUMBTRACK:
    					si.nPos = si.nTrackPos ;
    					break ;
    				default:
    					break ;
    			}
    			si.fMask = SIF_POS ;
    			iHscrollPos = max(0, min(iHscrollPos,100));
    			
    			if(iHscrollPos != GetScrollPos(hwnd, SB_VERT))
    			{
    				SetScrollPos(hwnd, SB_VERT, iHscrollPos, TRUE); 
    //why is the vert using the horiz variable?
    //This is probably why they run in parallel to each other
    				InvalidateRect(hwnd,NULL,TRUE);
    			}
    			return 0 ;
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    			EndPaint(hwnd, &ps);
    			return 0;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    
    	}
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    thanks, I think got it running now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 09:45 AM
  2. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 10:53 AM
  3. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  4. BSD make and GNU make problem
    By Skarr in forum Linux Programming
    Replies: 4
    Last Post: 09-10-2002, 12:31 PM
  5. cant make it work
    By papous27 in forum Linux Programming
    Replies: 0
    Last Post: 03-05-2002, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21