Problem with Scroll Bar

This is a discussion on Problem with Scroll Bar within the Windows Programming forums, part of the Platform Specific Boards category; I wrote this program (very simple and basic) that incorporates the Vertical Scroll Bar. No errors were flagged and the ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Problem with Scroll Bar

    I wrote this program (very simple and basic) that incorporates the Vertical Scroll Bar. No errors were flagged and the scroll bar works great while scrolling up. Not down.

    I did include (just for a test) the SB_LINEUP and SB_LINEDOWN messages and that's all. I just wanted to test them out. But, I can only scroll one way and I can't find the problem. Here's the code and I have the .c file attached so you can see what I mean:
    Code:
    /*---------------------------------------------------
    	Scroll Bar example
    ---------------------------------------------------*/
    
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   PSTR szCmdLine, int nCmdShow)
    {
    	static TCHAR szAppName[] = TEXT("New Application");
    	HWND		hwnd;
    	MSG			msg;
    	WNDCLASS	wndclass;
    
    	wndclass.style = CS_VREDRAW | CS_HREDRAW;
    	wndclass.lpfnWndProc = WndProc;
    	wndclass.cbClsExtra = 0;
    	wndclass.cbWndExtra = 0;
    	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    	wndclass.lpszMenuName = NULL;
    	wndclass.hInstance = hInstance;
    	wndclass.lpszClassName = szAppName;
    
    	if (!RegisterClass(&wndclass))
    	{
    		MessageBox(NULL, TEXT("Error registering window class!"),
    			szAppName, 0);
    
    		return 0;
    	}
    
    	hwnd = CreateWindow(szAppName, TEXT("This is the First Window"),
    		WS_VSCROLL | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 
    		CW_USEDEFAULT, CW_USEDEFAULT,
    		CW_USEDEFAULT, NULL,
    		NULL, hInstance, NULL);
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	PAINTSTRUCT		ps;
    	RECT			rect;
    	HDC				hdc;
    	TEXTMETRIC		tm;
    	static int		iVscrollPos, cyChar;
    	int				y;
    
    	switch (message)
    	{
    	case WM_CREATE:
    		hdc = GetDC(hwnd);
    		InvalidateRect(hwnd, &rect, FALSE);
    
    		GetTextMetrics(hdc, &tm);
    		ReleaseDC(hwnd, hdc);
    
    		cyChar = tm.tmHeight + tm.tmExternalLeading;
    
    		SetScrollRange(hwnd, SB_VERT, 0, 5, TRUE);
    		SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);
    		return 0;
    		
    	case WM_PAINT:
    		y = cyChar * iVscrollPos;
    
    		hdc = BeginPaint(hwnd, &ps);
    
    		GetClientRect(hwnd, &rect);
    		TextOut(hdc, 0, y, TEXT("TESTING1"), 8);
    		TextOut(hdc, 500, y + 500, TEXT("TESTING2"), 8);
    
    		EndPaint(hwnd, &ps);
    
    		return 0;
    
    	case WM_VSCROLL:
    		switch (LOWORD (lParam))
    		{
    		case SB_LINEUP:
    			iVscrollPos -= 1;
    			break;
    
    		case SB_LINEDOWN:
    			iVscrollPos += 1;
    			break;
    		default:
    			break;
    		}
    
    		if (iVscrollPos != GetScrollPos(hwnd, SB_VERT))
    		{
    			SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);
    			InvalidateRect(hwnd, NULL, TRUE);
    		}
    
    		return 0;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    
    		return 0;
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    Thanks.

    --Garfield
    Attached Files Attached Files
    1978 Silver Anniversary Corvette

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Never mind, never mind. I found my error:
    Code:
    	case WM_VSCROLL:
    		switch (LOWORD (lParam))
    Here it is supposed to be:
    Code:
                    case WM_VSCROLL:
                                    switch (LOWORD (wParam))
    I keep getting the types WPARAM and LPARAM mixed up. What are they for, anyway? My book just said that they are "extra" information for the WndProc along with the message. How are they different?

    Thanks.

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I think the WPARAM variable (yours is wParam ) corresponds to the message being sent to your applications message queue.

    Stored in the low-order(left-most?) bytes of this 32 bit variable is the code number of the message. Hence, somewhere in your system, there is a #define WM_CREATE 1234567 (this of course is not the actual number!) that will be stored in the LOWORD bytes of the WPARAM variable if this message is ever sent.

    The hi-order bytes store the handle of the window destined to recieve the message(I think).

    Anyway, As far as LPARAM variables, all I know is that they correspond to more message - related information!

    And I must add that the above info was provided to you by a NON-EXPERT!!!

    So don't place bets on it or anything crazy like that!

    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks for the explanation.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make a scroll bar in c++?
    By androide in forum C++ Programming
    Replies: 7
    Last Post: 11-18-2003, 12:26 AM
  2. Scroll Bars and Focus
    By Thantos in forum Windows Programming
    Replies: 1
    Last Post: 08-21-2003, 12:57 PM
  3. Horizontal Scroll Bars with CListBox
    By Malek in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2003, 10:58 PM
  4. Scroll bar question
    By converge in forum Windows Programming
    Replies: 4
    Last Post: 01-22-2003, 09:15 AM
  5. Problem with Scroll Bar
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 11-05-2001, 05:34 AM

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