Need help with mouse bug

This is a discussion on Need help with mouse bug within the Windows Programming forums, part of the Platform Specific Boards category; I'm using WM_LBUTTONDOWN and WM_LBUTTONUP to set/reset a boolean variable keeping track on whether the user holds down the left ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Need help with mouse bug

    I'm using WM_LBUTTONDOWN and WM_LBUTTONUP to set/reset a boolean variable keeping track on whether the user holds down the left mouse button or not. Normally this works fine except when the user presses the button, moves the pointer outside the window (still keeping the button down) then releasing it. This prevents the WM_LBUTTONUP message to be posted, thus making the program think that the button is still pressed though it isn't.
    Is there any message that is sent if the mouse leaves the window area? Or something else that can prevent this bug?
    Thankful for any help!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Cast your eyes over SetCapture()

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    SetCapture didn't work. It made the program freeze with an hourglass until you select another program then go back to it again. The bug still remains though...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Hmm...should work

    Try this code......Click inside the window to change the message....then click inside the window.....drag outside the client area and let go...you should find that your WM_LBUTTONUP message is still being recieved

    Code:
    #include <windows.h>
    #include <string.h>
    
    //**************************************************************************
    
    
    LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,
    	WPARAM wParam,LPARAM lParam)
    {
    	HDC dc;
    	PAINTSTRUCT ps;
    	RECT rect;
    	static enum STATE{MOUSEDOWN = 0,MOUSEUP} eState;
    	char* szString;
    	static HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
    
    	switch(uMsg){
    
    	case WM_CREATE:
    		eState = MOUSEUP;
    		break;
    	case WM_CLOSE:
    		PostQuitMessage(0);
    		break;
    	case WM_LBUTTONUP:
    		ReleaseCapture();
    		eState = MOUSEUP;
    		InvalidateRect(hWnd,0,TRUE);
    		UpdateWindow(hWnd);
    		break;
    	case WM_LBUTTONDOWN:
    		SetCapture(hWnd);
    		eState = MOUSEDOWN;
    		InvalidateRect(hWnd,0,TRUE);
    		UpdateWindow(hWnd);
    		break;
    	case WM_PAINT:
    		dc = BeginPaint(hWnd,&ps);
    		GetClientRect(hWnd,&rect);
    		szString = (eState == MOUSEDOWN ? "Mouse Button Down" :
    			"Mouse Button Up");
    		FillRect(dc,&rect,hBrush);
    		DrawText(dc,szString,strlen(szString),&rect,
    			DT_VCENTER | DT_CENTER | DT_SINGLELINE);
    		EndPaint(hWnd,&ps);
    		break;
    	default:
            return DefWindowProc(hWnd,uMsg,wParam,lParam);  
        }
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hInstPrev,
    	LPSTR szCommand,int nCmdShow)
    {
    
    	const char* szClass = "Magos";
    	HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
    
    	WNDCLASSEX wcx = {0};   
    	wcx.cbSize = sizeof(wcx); 
    	wcx.style = CS_HREDRAW|CS_VREDRAW; 
    	wcx.lpfnWndProc = WndProc;
    	wcx.hInstance = hInst;
    	wcx.hIcon = LoadIcon(0,IDI_APPLICATION);  
    	wcx.hCursor = LoadCursor(0,IDC_ARROW); 
    	wcx.hbrBackground = hBrush;    
    	wcx.lpszClassName = szClass;  
    	
    	if (!RegisterClassEx(&wcx))
        {
    		MessageBox(0,"Failed to register wnd class",0,MB_OK);
    		return 1;
        }
     
    	HWND hWnd = CreateWindowEx(0,szClass,"Magos",
    		WS_OVERLAPPEDWINDOW,GetSystemMetrics(SM_CXSCREEN)/4,
    		GetSystemMetrics(SM_CYSCREEN)/4,GetSystemMetrics(SM_CXSCREEN)/2,
    		GetSystemMetrics(SM_CYSCREEN)/2,0,0,hInst,0);
    	
    	if (!hWnd)
        {
    		MessageBox(0,"Window creation failed",0,MB_OK);
    		return 1;
        }
    	
    	ShowWindow(hWnd,nCmdShow);
    	UpdateWindow(hWnd);
    
    	MSG msg;
    	
    
    	while(GetMessage(&msg,0,0,0) > 0)
        {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
        }
    
    
    	return msg.wParam;
    }

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yes, it works now. Silly me called SetCapture() once when the program started instead of every time the message was sent .
    Thanks!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need Help in Mouse Pointer
    By obaid in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2006, 02:33 AM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  3. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 01:10 AM
  4. Making a mouse hover button, API style
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2004, 06:17 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM

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