-
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!
-
Cast your eyes over SetCapture()
-
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...
-
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;
}
-
Yes, it works now. Silly me called SetCapture() once when the program started instead of every time the message was sent :o.
Thanks!