Using SetTimer & WM_TIMER question...?

This is a discussion on Using SetTimer & WM_TIMER question...? within the Windows Programming forums, part of the Platform Specific Boards category; I've got this example "Using Timer Functions to Trap Mouse Input" below from MSDN but not very helpful... Code: HICON ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Using SetTimer & WM_TIMER question...?

    I've got this example "Using Timer Functions to Trap Mouse Input" below from MSDN but not very helpful...

    Code:
    HICON hIcon1;               // icon handle 
    POINT ptOld;                // previous cursor location 
    UINT uResult;               // SetTimer's return value 
    HINSTANCE hinstance;        // handle to current instance 
     
    //
    // Perform application initialization here. 
    //
     
    wc.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(400)); 
    wc.hCursor = LoadCursor(hinstance, MAKEINTRESOURCE(200)); 
     
    // Record the initial cursor position. 
     
    GetCursorPos(&ptOld); 
     
    // Set the timer for the mousetrap. 
     
    uResult = SetTimer(hwnd,             // handle to main window 
        IDT_MOUSETRAP,                   // timer identifier 
        10000,                           // 10-second interval 
        (TIMERPROC) NULL);               // no timer callback 
     
    if (uResult == 0) 
    { 
        ErrorHandler("No timer is available."); 
    } 
     
    LONG APIENTRY MainWndProc( 
        HWND hwnd,          // handle to main window 
        UINT message,       // type of message 
        WPARAM  wParam,     // additional information 
        LPARAM  lParam)     // additional information 
    { 
     
        HDC hdc;        // handle to device context 
        POINT pt;       // current cursor location 
        RECT rc;        // location of minimized window 
     
        switch (message) 
        { 
            //
            // Process other messages. 
            // 
     
            case WM_TIMER: 
            // If the window is minimized, compare the current 
            // cursor position with the one from 10 seconds 
            // earlier. If the cursor position has not changed, 
            // move the cursor to the icon. 
     
                if (IsIconic(hwnd)) 
                { 
                    GetCursorPos(&pt); 
     
                    if ((pt.x == ptOld.x) && (pt.y == ptOld.y)) 
                    { 
                        GetWindowRect(hwnd, &rc); 
                        SetCursorPos(rc.left, rc.top); 
                    } 
                    else 
                    { 
                        ptOld.x = pt.x; 
                        ptOld.y = pt.y; 
                    } 
                } 
     
                return 0; 
     
            case WM_DESTROY: 
     
            // Destroy the timer. 
     
                KillTimer(hwnd, IDT_MOUSETRAP); 
                PostQuitMessage(0); 
                break; 
     
            //
            // Process other messages. 
            // 
     
    }
    From code above, where should the following lines of code be inserted in my program?

    Code:
    HICON hIcon1;               // icon handle 
    POINT ptOld;                // previous cursor location 
    UINT uResult;               // SetTimer's return value 
    HINSTANCE hinstance;        // handle to current instance 
     
    //
    // Perform application initialization here. 
    //
     
    wc.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(400)); 
    wc.hCursor = LoadCursor(hinstance, MAKEINTRESOURCE(200)); 
     
    // Record the initial cursor position. 
     
    GetCursorPos(&ptOld); 
     
    // Set the timer for the mousetrap. 
     
    uResult = SetTimer(hwnd,             // handle to main window 
        IDT_MOUSETRAP,                   // timer identifier 
        10000,                           // 10-second interval 
        (TIMERPROC) NULL);               // no timer callback 
     
    if (uResult == 0) 
    { 
        ErrorHandler("No timer is available."); 
    }
    Is it in WinMain()?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It can go in WndMain if you like - all that is really required is that the hWnd of your main window is available, and that you use a unique timer ID, particularly if you want to cancel the timer again, you need to know the ID.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    It can go in WndMain if you like - all that is really required is that the hWnd of your main window is available, and that you use a unique timer ID, particularly if you want to cancel the timer again, you need to know the ID.

    --
    Mats
    I actually did put all of it in WinMain() but get compiler errors..

    error C2065: 'IDT_MOUSETRAP' : undeclared identifier

    &

    ErrorHandler(); function is not part of API apparently.. How can microsoft give examples of non existing functions? thanx mat

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    I actually did put all of it in WinMain() but get compiler errors..

    error C2065: 'IDT_MOUSETRAP' : undeclared identifier

    &

    ErrorHandler(); function is not part of API apparently.. How can microsoft give examples of non existing functions? thanx mat
    Yes, you need to select your own timer ID (IDT_xxxx).

    The code presented on the MSDN page is incomplete - it shows only parts of the complete code, so some "reading between the lines" is needed to use it is necessary.

    Hence ErrorHandler() is not an API function. It's just a way to say "We got here because of some error, you'd better do something here".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    For ErrorHandler() you could just use a MessageBox.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    I would put the code in the WM_CREATE / WM_INITDIALOG handler. Only issue is timing, will that still catch the location of the icon on the desktop.

    Quote Originally Posted by csonx_p View Post
    IDT_MOUSETRAP
    ID = Identification number
    T = timer
    MOUSETRAP = description / what it does


    You can just use an int instead

    SetTimer(101,......

    or define one

    Code:
    //in header or at top of file
    #define IDT_MOUSETRAP 101 //create timer ID number
    Last edited by novacain; 04-24-2008 at 05:02 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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