请问在Win32程序中怎样用定时器

解决方案 »

  1.   

    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. 
            // 
     

    摘自MSDN先调用SetTimer 然后响应WM_TIMER消息