Timer for learning notes in Windows Programming

Source: Internet
Author: User

Three ways to use a timer:

Method 1:

This is the most convenient method. It allows Windows to send wm_timer messages to the normal window message processing program of the application. The settimer call is as follows:

SetTimer (hwnd, 1, uiMsecInterval, NULL) ;        

The first parameter is the window handle that the window message processing program will receive the wm_timer message. The second parameter is the timer ID, which is a non-zero value. In this example, it is assumed to be 1. The third parameter is a 32-bit integer without plus or minus signs. A time interval is specified in milliseconds. A value of 60,000 will enable Windows to send a wm_timer message every minute.

You can call

KillTimer (hwnd, 1) ;        

Stop the wm_timer message at any time (even if the wm_timer message is being processed ). The second parameter of this function is the same timer ID used in the settimer call. Before terminating the program, you should respond to the wm_destroy message to stop any active timer.

When your window message processing program receives a wm_timer message, the wparam parameter is equal to the timer id value (1 in the above case), and The lparam parameter is 0. If multiple timers need to be set, different timer IDs are used for each timer. The value of wparam varies with the wm_timer message passed to the window message processing program. To make the program more readable, you can use # define to describe different timer IDs:

# Define timer_sec 1 # define timer_min 2 is added to the window processing function:
Casewm_timer: Switch (wparam) {Case timer_sec: // processes break once per second; Case timer_min: // processes break once per minute;} return 0.
Method 2:
Let windows directly send the timer message to another function of your program.
Settimer (hwnd, itimerid, imsecinterval, timerproc );

The following callback function is called timerproc (you can select any name that does not conflict with other terms). It only processes wm_timer messages:

Void callback timerproc (hwnd, uint message, uint itimerid, DWORD dwtime) {process wm_timer message}

The hwnd parameter of timerproc is the window handle specified when settimer is called. Windows only sends the wm_timer message to timerproc, so the message parameter is always equal to wm_timer.

The value of itimerid is the timer ID, and the value of dwtimer is compatible with the value returned from the gettickcount function. This is the number of milliseconds that have elapsed since Windows was started.

Method 3:

The third method to set the timer is similar to the second method, except that the hwnd parameter passed to settimer is set to null, and the second parameter (usually the timer ID) is ignored. Finally, the ID of the timer returned by this function:

iTimerID = SetTimer (NULL, 0, wMsecInterval, TimerProc) ;        

If no timer is available, the itimerid value returned from settimer is null.

The first parameter (usually the window handle) of killtimer must be null, And the timer ID must be the return value of settimer:

KillTimer (NULL, iTimerID) ;        

The hwnd parameter passed to the timerproc timer function must also be null. The timer setting method is rarely used. If you have a series of settimer calls at different times in your program

If the timer ID is used, it is very convenient to use this method.

 

 

 

 

Some functions in this chapter:

1. messagebeep (-1 );

Play Sound. the following values can be used to play different sounds. The return value is of the bool type. If the return value is successful, the return value is non-zero. If the return value is failed, the return value is 0.

If the call fails

DWORD getlasterror (void); function to get the error type.

Value Sound
-1 Standard beep using the computer speaker
Mb_iconasterisk Systemasterisk
Mb_iconexclamation Systemexclamation
Mb_iconhand Systemhand
Mb_iconquestion Systemquestion
Mb_ OK Systemdefault

2.Bool invalidaterect (
Hwnd
Hwnd,// Handle to window
Const rect *Lprect,// Rectangle coordinates
BoolBerase// Erase state
);

Invalidaterect (hwnd, null, false );
Parameter 1 points to the handle of the window to be updated; parameter 2 points to the region to be updated. If it is null, the entire region must be updated;

Parameter 3 indicates whether the background is erased during update. If the value is true, the background is erased. If the value is false, the background is not erased.

 

3.Bool getclientrect (
Hwnd
Hwnd,// Handle to window
LprectLprect// Client coordinates
);

GetHwndThe customer area of the window to be pointed to and placed in lprect.

4. getclientrect (hwnd, & rc)

5. fillrect (HDC, & rc, hbrush );

6. polygon (HDC, ptsegment [iseg], 6 );

7. offsetworgex (HDC,-42, 0, null );
8. getlocaltime (& St); the function returns the current local time (set by the computer)

Getsystemtime (& St); function returns the current world time
Typedef struct _ systemtime
{
Word wyear;
Word wmonth;
Word wdayofweek;
Word wday;
Word whour;
Word wminute;
Word wsecond;
Word wmilliseconds;
}
Systemtime, * psystemtime;

9. getlocaleinfo (locale_user_default, locale_itime, szbuffer, 2 );

Int getlocaleinfo (
Lcid
Locale,// Locale identifier
LctypeLctype,// Information type
LptstrLplcdata,// Information Buffer
IntCchdata// Size of Buffer
);

Obtain the specified information of the system or user based on Parameter 2.In the szbuffer space, if the last length is 0, the return value is the length of the obtained information.
10. setmapmode (HDC, mm_isotropic );
11. setwindowextex (HDC, 276, 72, null );
12. setviewportextex (HDC, cxclient, cyclient, null );

13. setw.worgex (HDC, 138, 36, null );
14. setviewportorgex (HDC, cxclient/2, cyclient/2, null );

 

 

Errors in program debugging in this section:

Linking...
Libcd. Lib (wincrt0.obj): Error lnk2001: unresolved external symbol _ winmain @ 16
Debug/test.exe: Fatal error lnk1120: 1 unresolved externals

 

 

Solution: This file uses winmian instead of main as the entry function. The default setting of VC is for console programs.
Solution:
1. Go to project-> setting-> C/C ++, select Preprocessor in category (the first line), add _ WINDOWS to processor definitions, and delete _ Console

2. Go to project-> setting-> link and change/subsystem: console. To/subsystem: Windows in project options.

3. Save the settings and rebuild all.

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.