Prev: DefWinProc() doesn't return during window resizing?
Next: Making TIME_ZONE_INFORMATION find if Daylight savings is active
From: Jimbo on 24 Jan 2010 22:39 Hello I am making an application in Win32 that can display the mouse x,y coordinates whereever the mouse is, whether in the client window or outside it. What windows message is sent the Windows Proceedure function when the mouse is outside the client area of an application. I thought it was WM_NCMOUSEMOVE I have my application below that shows the mouse screen coordinates when the mouse is within the client area but not when its outside the client area. How can I detect & display the mouse coords when outside the client area Here is my full application. It compiles & is simple & if you play it you will easily see what is wrong(how I cannot get the mouse coords when the mouse is in the background. #include <windows.h> #include <iostream> #include <string> #include <Windowsx.h> #include <stdio.h> using namespace std; const char g_szClassName[] = "myWindowClass"; static HINSTANCE gInstance; static int scale = 4; // Scale down screen dimension by 5 static int screenW = GetSystemMetrics(SM_CXSCREEN); // Get screen width static int screenH = GetSystemMetrics(SM_CYSCREEN); int mX, mY; char mouseX[32]; char mouseY[32]; // Functions List // LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // if registration of main class fails if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "Show mouse position on screen", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,200,200, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } // Step 4: the Window Procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { RECT rScreen; HWND screenHwnd = GetDesktopWindow(); GetWindowRect(screenHwnd,&rScreen); SetWindowPos(hwnd,NULL,rScreen.right-((rScreen.right/ scale)+10),rScreen.top+10, rScreen.right/scale,rScreen.bottom/ scale,SWP_NOZORDER); } case WM_MOUSEMOVE: case WM_MOUSELEAVE: // if mouse leaves client area/window case WM_NCHITTEST: case WM_NCMOUSEMOVE: // detect movements of mouse in NonClient area { // 2 ways to get mouse Position // 1st way: POINT pt; GetCursorPos(&pt); mX = pt.x; mY = pt.y; //mX = GET_X_LPARAM(lParam); //mY = GET_Y_LPARAM(lParam); itoa(mX,mouseX,10); itoa(mY,mouseY,10); InvalidateRect(hwnd,NULL,true); } break; case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; int xScaled = mX/scale; int yScaled = mY/scale; hdc = BeginPaint(hwnd,&ps); //HPEN hPen = CreatePen(PS_SOLID,1,RGB(200,50,50)); HBRUSH hBrush = CreateSolidBrush(RGB(200,200,50)); HRGN hReg = CreateEllipticRgn (xScaled-10,yScaled-10,xScaled+10,yScaled+10); //SelectObject(hdc,hPen); //Ellipse(hdc,mX-10,mY-10,mX+10,mY+10); FillRgn(hdc,hReg,hBrush); SetBkMode(hdc,TRANSPARENT); TextOut(hdc,10,10,mouseX,4); TextOut(hdc,10,30,mouseY,4); EndPaint(hwnd,&ps); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }
From: Christian ASTOR on 25 Jan 2010 03:54 On 25 jan, 04:39, Jimbo <nill...(a)yahoo.com> wrote: > I am making an application in Win32 that can display the mouse x,y > coordinates whereever the mouse is, whether in the client window or > outside it. Use a mouse Hook or SetCapture()
From: B.S on 26 Jan 2010 10:07
use SetTimer(); function it wil set windows update time window will worck whan window is not a foreground. SetTimer(hwnd,WM_TIMER,100); 1000 is a 1 second than use case WM_TIMER: { SendMessage(hwnd,WM_MOUSEMOVE,0,0); } it mast wark |