Sign in to follow this  
gretty

Alarm is never Initiated?

Recommended Posts

gretty    273
Hello I have an application that displays a scaled window of the screen & displays the scaled 'position of the mouse on the screen' onto the client window. I have some code to detect when the mouse leaves the window, & at that point a timer should be initiated that repeats every 50 clock steps & draws the updated position of the mouse. But the alarm & mouse hook is never initiated? Can you help me firgure out why:
#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  
   //       We add 20px to the width of window & 40px to the height to compensate
   //       for the non-client area of the window(scaled screen mouse location will be accurate).
   
   RECT rScreen;
   HWND screenHwnd = GetDesktopWindow();
   GetWindowRect(screenHwnd,&rScreen);
   SetWindowPos(hwnd,NULL,rScreen.right-((rScreen.right/scale)+10),rScreen.top+10,
                (rScreen.right/scale)+20,(rScreen.bottom/scale)+40,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);  
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this