Aracılığıyla paylaş


Zamanlayıcıları Kullanma

Bu konu, zamanlayıcıları oluşturma ve yok etme ve fare girişini belirli aralıklarla yakalamak için zamanlayıcının nasıl kullanılacağını gösterir.

Bu konu aşağıdaki bölümleri içerir.

Zamanlayıcı Oluşturma

Aşağıdaki örnek, iki zamanlayıcı oluşturmak için SetTimer işlevini kullanır. İlk zamanlayıcı 10 saniyede bir, ikinci zamanlayıcı ise beş dakikada bir ayarlanır.

// Set two timers. 
 
SetTimer(hwnd,             // handle to main window 
    IDT_TIMER1,            // timer identifier 
    10000,                 // 10-second interval 
    (TIMERPROC) NULL);     // no timer callback 
 
SetTimer(hwnd,             // handle to main window 
    IDT_TIMER2,            // timer identifier 
    300000,                // five-minute interval 
    (TIMERPROC) NULL);     // no timer callback 

Bu zamanlayıcılar tarafından oluşturulan WM_TIMER iletilerini işlemek için, hwnd parametresi için pencere yordamına bir WM_TIMER durum deyimi ekleyin.

case WM_TIMER: 
 
    switch (wParam) 
    { 
        case IDT_TIMER1: 
            // process the 10-second timer 
 
             return 0; 
 
        case IDT_TIMER2: 
            // process the five-minute timer 

            return 0; 
    } 

Bir uygulama, WM_TIMER iletileri ana pencere yordamı tarafından değil uygulama tanımlı geri çağırma işlevi tarafından işlenen bir zamanlayıcı da oluşturabilir. Bu, aşağıdaki kod örneğinde olduğu gibi bir zamanlayıcı oluşturur ve zamanlayıcının WM_TIMER iletilerini işlemek için MyTimerProc geri çağırma işlevini kullanır.

// Set the timer. 
 
SetTimer(hwnd,                // handle to main window 
    IDT_TIMER3,               // timer identifier 
    5000,                     // 5-second interval 
    (TIMERPROC) MyTimerProc); // timer callback

Bu MyTimerProc çağırma kuralı, TimerProc geri çağırma işlevini temel almalıdır.

Uygulamanız bir pencere tutamacı belirtmeden bir süreölçer oluşturursa, uygulamanızın WM_TIMER iletileri için ileti sırasını izlemesi ve uygun pencereye göndermesi gerekir.

HWND hwndTimer;   // handle to window for timer messages 
MSG msg;          // message structure 
 
    while (GetMessage(&msg, // message structure 
            NULL,           // handle to window to receive the message 
               0,           // lowest message to examine 
               0))          // highest message to examine 
    { 
 
        // Post WM_TIMER messages to the hwndTimer procedure. 
 
        if (msg.message == WM_TIMER) 
        { 
            msg.hwnd = hwndTimer; 
        } 
 
        TranslateMessage(&msg); // translates virtual-key codes 
        DispatchMessage(&msg);  // dispatches message to window 
    } 

Zamanlayıcıyı Yok Etme

Uygulamalar artık gerekli olmayan zamanlayıcıları yok etmek için KillTimer işlevini kullanmalıdır. Aşağıdaki örnek IDT_TIMER1, IDT_TIMER2 ve IDT_TIMER3 sabitleri tarafından tanımlanan zamanlayıcıları yok eder.

// Destroy the timers. 
 
KillTimer(hwnd, IDT_TIMER1); 
KillTimer(hwnd, IDT_TIMER2); 
KillTimer(hwnd, IDT_TIMER3); 

Fare Girişini Yakalamak için Zamanlayıcı İşlevlerini Kullanma

Bazen ekranda fare işaretçisi varken daha fazla girişi önlemek gerekir. Bunu gerçekleştirmenin bir yolu, belirli bir olay gerçekleşene kadar fare girişini yakalayan özel bir yordam oluşturmaktır. Birçok geliştirici bu yordamı "fare kapanı oluşturma" olarak adlandırıyor.

Aşağıdaki örnek, fare girişini yakalamak için SetTimerve KillTimerişlevlerinikullanır. SetTimer, her 10 saniyede bir WM_TIMER iletisi gönderen bir zamanlayıcı oluşturur. Uygulama her WM_TIMER iletisi aldığında fare işaretçisi konumunu kaydeder. Geçerli konum önceki konumla aynıysa ve uygulamanın ana penceresi simge durumuna küçültülmüşse, uygulama fare işaretçisini simgeye taşır. Uygulama kapatıldığında KillTimer zamanlayıcıyı durdurur.

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

Fare girişinin nasıl yakalanacağını gösteren aşağıdaki örnek, WM_TIMER iletisini uygulamanın ileti kuyruğu yerine, MyTimerProc olarak adlandırılmış uygulama tanımlı geri çağırma işlevi aracılığıyla işler.

UINT uResult;               // SetTimer's return value 
HICON hIcon1;               // icon handle 
POINT ptOld;                // previous cursor location 
HINSTANCE hinstance;        // handle to current instance 
 
//
// Perform application initialization here. 
//
 
wc.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(400)); 
wc.hCursor = LoadCursor(hinstance, MAKEINTRESOURCE(200)); 
 
// Record the current 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) MyTimerProc); // 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 
 
    switch (message) 
    { 
    // 
    // Process other messages. 
    // 
 
        case WM_DESTROY: 
        // Destroy the timer. 
 
            KillTimer(hwnd, IDT_MOUSETRAP); 
            PostQuitMessage(0); 
            break; 
 
        //
        // Process other messages. 
        // 
 
} 
 
// MyTimerProc is an application-defined callback function that 
// processes WM_TIMER messages. 
 
VOID CALLBACK MyTimerProc( 
    HWND hwnd,        // handle to window for timer messages 
    UINT message,     // WM_TIMER message 
    UINT idTimer,     // timer identifier 
    DWORD dwTime)     // current system time 
{ 
 
    RECT rc; 
    POINT pt; 
 
    // 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; 
        } 
    } 
} 

Zamanlayıcılar Hakkında