Introduction to WIN32 Development (24): Timers

Source: Internet
Author: User
Tags win32

I haven't written a blog for a while. Today to play the timer, we often say that the timer, although it is not a complex thing, also not known as the Ox X, but the usefulness of many, for those who need to perform at a certain time of the task, it is quite useful.

First come to know a pair of functions, notice, is a pair, not one.

settimer--setting and enabling timers;

killtimer--cancels the timer.

Now that you understand why you want a pair, it's like a process operation, a function that starts or creates a process, and there's definitely a function to shut down the process, and GETDC must be accompanied by the RELEASEDC function. Yin and Yang are the same.

First of all, SetTimer, the definition of function I do not say, I look at files and MSDN on the line, mainly said the following two parameters:

Nidevent refers to the ID of the timer, a number, you can take the case, as long as not negative numbers are not decimals, such as 10,200,56,115,222; the last argument lptimerfunc is a pointer to a callback function, which is similar to the WindowProc class , but this parameter can be null.

When this parameter is null, in WindowProc you have to capture Wm_timer message, however, this is the message is low priority, the system will be in the process of other messages, idle profession will handle the WM_TIMER message. If the Lptimerfunc parameter is not NULL, you do not have to capture the wm_timer, and you can handle it directly in the callback function.

If the timer ID already exists, a new timer will be substituted for the original timer.

KillTimer say, is to destroy the timer, in which, the timer ID to SetTimer with the previous ID to maintain consistent, this is not specifically explained, you take your library card to the library to borrow books, to return the time, you certainly will not take other people's library card to return the book it?

The theory is to say how much is not good, or use the example to speak it.

Let's just say this example, the main use of timers, every second (1000 milliseconds) to perform once, but each time the case is different, so with a bool type variable to identify, if true in the Wm_paint event in the window to fill the client area red, if False is not filled. In this way, the window can be rendered a flash-flashing effect.

I'll just post the core code, and I'll upload the complete example to "resources" later.

Timer callback function  
VOID CALLBACK timerproc (  
  _in_  hwnd hwnd,  
  _in_ UINT umsg  ,  
  _in_  uint_ptr Idevent,  
  _in_  DWORD dwtime  
)  
{  
    isborderdrawed =!isborderdrawed;  
    RECT RECT;  
    GetClientRect (hwnd,&rect);  
    InvalidateRect (hwnd, &rect, TRUE);  
}
      /* Processing WM_PAINT message * *
ASE WM_PAINT:  
hdc = BeginPaint (hWnd, &ps);  
TODO: Add any drawing code here ...  
   Gets the window border rectangle  
RECT RECT;  
GetClientRect (HWnd, &rect);  
if (isborderdrawed)  
{  
    Hbrush HB = CreateSolidBrush (RGB (255,0,0));  
    FillRect (Hdc,&rect, HB);  
    SelectObject (HDC,HB);  
}  
EndPaint (HWnd, &ps);  
Break

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.