WM_MOUSELEAVE message not being received?

Started by
0 comments, last by mattd 14 years, 2 months ago
Hello I have an application that scales the application window to 1/4 of the dimensions of the computers screen size, & shows a red dot on the application window where the mouse actually is on the computer screen. My problem is that the code to capture the screen coordinates(& set an alarm) when the mouse leaves the application window is not being done. Its supposed to be done when my mouse leaves the application window & my windows proceedure receives the WM_MOUSELEAVE message. Can you help me figure out why the code under WM_MOUSELEAVE is not being inacted?

#include <windows.h>
#include <iostream>
#include <string>
#include <Windowsx.h>
#include <stdio.h>

using namespace std;

#define IDT_TIMER 1
#define IDG_TIME  2

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];
COLORREF col = RGB(255,0,0);
bool trackMouse = false;
UINT t; // For debugging

// Functions List //
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void setWindow(HWND hwnd);
void drawStats(HDC hdc);
void getCursor(HWND hwnd);


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:
        {    
             setWindow(hwnd);
             //t = SetTimer(hwnd,IDG_TIME,50,NULL);
        }
        case WM_MOUSEMOVE:
        //case WM_NCMOUSEMOVE: // detect movements of mouse in NonClient area
        {    // Get cursor position
        
             if (trackMouse==true) {
                 KillTimer(hwnd,IDT_TIMER);
                 ReleaseCapture(); // release mouse capture
                 col = RGB(255,0,0);
                 trackMouse = false;
             }
             else getCursor(hwnd);
        }     
        break;
        case WM_MOUSELEAVE:  // if mouse leaves client area/window
        {    // This code is never initiated????
             UINT mouseTracker = SetTimer(hwnd,IDT_TIMER,50,NULL);
             SetCapture(hwnd);  // capture mouse
             col = RGB(0,0,255);
             trackMouse = true;
        }
        break;
        case WM_TIMER:
        {
             switch(LOWORD(wParam)) {
                 
                 case IDT_TIMER:
                 {    // update cursor position
                      getCursor(hwnd);
                 }     
                 break;
                 case IDG_TIME:
                      getCursor(hwnd);
                 break;
                 default:
                 break;                   
             }
        }    
        break;
        case WM_PAINT:
        {
             HDC hdc;
             PAINTSTRUCT ps;
             hdc = BeginPaint(hwnd,&ps);
             
             drawStats(hdc);
             
             EndPaint(hwnd,&ps);
        }    
        break;
        case WM_CLOSE:
            KillTimer(hwnd,IDG_TIME);
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

void setWindow(HWND hwnd) 
{
   // Post: Get screen dimensions & set application window position  
   
   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);
   // set scale       
   RECT clientR;
   GetClientRect(hwnd,&clientR);   
   scale = rScreen.right/clientR.right;
}

void drawStats(HDC hdc) 
{
   // Post: Draw cursors scaled position & sprite
   
   int xScaled = mX/scale;
   int yScaled = mY/scale;
   
   HBRUSH hBrush = CreateSolidBrush(col);   //RGB(200,200,50)
   HRGN hReg = CreateEllipticRgn(xScaled-10,yScaled-10,xScaled+10,yScaled+10);
             
   FillRgn(hdc,hReg,hBrush);
             
   SetBkMode(hdc,TRANSPARENT);
   TextOut(hdc,10,10,mouseX,4);
   TextOut(hdc,10,30,mouseY,4);
   
   DeleteObject(hBrush);
}

void getCursor(HWND hwnd)
{
   // Post: Find cursor position & convert to char array
            
   POINT pt;
   GetCursorPos(&pt);
   mX = pt.x; mY = pt.y;
             
   itoa(mX,mouseX,10);
   itoa(mY,mouseY,10);
   InvalidateRect(hwnd,NULL,true);  
}



Advertisement
You have to call TrackMouseEvent to receive WM_MOUSELEAVE messages.

This topic is closed to new replies.

Advertisement