Windows program design: Zero-Basic self-learning _ 7 _ timer usage

Source: Internet
Author: User

In Visual Basic, you can use the timer control to implement the timing function.ProgramThe design can also implement the timing function,

By enabling the timer object, you can implement the timer function.

Use a simple exp to view the timer usage:

/* This instance Code Display timer usage-Beeper Program */# include <windows. h> # define id_timer 1 lresult callback (hwnd, uint message, wparam, lparam); int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {static tchar szappclassname [] = text ("Beeper"); static tchar szappwndcaption [] = text ("Beeper"); hwnd; MSG; wndclass. cbclsextra = 0; wndclass. cbwndextra = 0; Wndclass. hbrbackground = (hbrush) getstockobject (white_brush); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. hicon = loadicon (null, idi_application); wndclass. hinstance = hinstance; wndclass. lpfnwndproc = wndproc; wndclass. lpszclassname = szappclassname; wndclass. lpszmenuname = NULL; wndclass. style = cs_hredraw | cs_vredraw; If (! Registerclass (& wndclass) {MessageBox (null, "You need winnt to run this program! "," Warning ", mb_ OK); Return 0;} hwnd = createwindow (szappclassname, szappwndcaption, ws_overlappedwindow, cw_usedefault, cw_usedefault, 200,200, null, null, hinstance, null ); showwindow (hwnd, nshowcmd); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); dispatchmessage (& MSG );} return MSG. wparam;} // ****************** lresult callback wndproc (hwnd, uint message, wparam, lpara M lparam) {static bool fflipflop = false; hbrush; HDC; paintstruct pS; rect RC; Switch (Message) {Case wm_create: settimer (hwnd, id_timer, 1000, null);/* winuserapi uint winapi settimer (hwnd, use the uint nidevent handle of the timer window and the ID of the timer. This value can be customized and is an unsigned integer uint uelapse, time of the timer. The time base is Ms timerproc lptimerfunc); */return 0; Case wm_timer: messagebeep (-1); fflipflop =! Fflipflop; invalidaterect (hwnd, null, false); Return 0; Case wm_paint: HDC = beginpaint (hwnd, & PS); getclientrect (hwnd, & rc ); hbrush = createsolidbrush (fflipflop? RGB (255, 0): RGB (,); fillrect (HDC, & rc, hbrush); deleteobject (hbrush); endpaint (hwnd, & PS ); return 0; Case wm_destroy: killtimer (hwnd, id_timer); postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam );}

It must be noted that the settimer function:

/*
Winuserapi uint winapi
Settimer (hwnd, the handle of the window using the timer, that is, the window after the scheduled time can receive the wm_timer message
Uint nidevent, ID of the timer. This value can be customized and is an unsigned integer.
Uint uelapse, Timer time, time base is Ms
Timerproc lptimerfunc); the callback function of the timer message, which is unique for processing the timer message.

*/

When processing a timer message, you can complete the message processing function in the window and define the callback function of the timer,

Exp:

/* This instance code shows the usage of the timer-Beeper Program */# include <windows. h> # define id_timer 1 lresult callback wndproc (hwnd, uint message, wparam, lparam); // define a timer-related callback function void callback timerproc (hwnd, uint message, uint itimeid, DWORD dwtime); int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {static tchar szappclassname [] = text ("Beeper "); static tchar szappwndcapt Ion [] = text ("Beeper"); hwnd; MSG; wndclass. cbclsextra = 0; wndclass. cbwndextra = 0; wndclass. hbrbackground = (hbrush) getstockobject (white_brush); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. hicon = loadicon (null, idi_application); wndclass. hinstance = hinstance; wndclass. lpfnwndproc = wndproc; wndclass. lpszclassname = szappclassname; wndclass. lpszmenuname = NULL; wndclass. style = cs_hredraw | Cs_vredraw; If (! Registerclass (& wndclass) {MessageBox (null, "You need winnt to run this program! "," Warning ", mb_ OK); Return 0;} hwnd = createwindow (szappclassname, szappwndcaption, ws_overlappedwindow, cw_usedefault, cw_usedefault, 200,200, null, null, hinstance, null ); showwindow (hwnd, nshowcmd); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); dispatchmessage (& MSG );} return MSG. wparam;} // ****************** lresult callback wndproc (hwnd, uint message, wparam, lpara M lparam) {Switch (Message) {Case wm_create: settimer (hwnd, id_timer, 1000, timerproc);/* winuserapi uint winapi settimer (hwnd, use the uint nidevent of the window handle of the timer and the ID of the timer. This value can be customized. It is an unsigned integer uint uelapse. The time base of the timer is Ms timerproc lptimerfunc ); when the callback function of the timer message is defined, this value transmits the address */return 0 of the callback function of the timer message; Case wm_destroy: killtimer (hwnd, id_timer ); postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam );} // Implement a timer-related callback function void callback timerproc (hwnd, // It is the window handle uint message specified when settimer is called, // windows only sends the wm_timer message to the timer Message Processing callback function, so this value is always wm_timeruint itimeid, // This value is the iddword dwtime of the timer) // This value is compatible with the value returned from the gettickcount function, and is the number of seconds since Windows Startup {static bool fflipflop = false; hbrush; HDC; rect RC; messagebeep (-1); fflipflop =! Fflipflop; getclientrect (hwnd, & rc); // obtain the size information of the user region. HDC = getdc (hwnd); hbrush = createsolidbrush (fflipflop? RGB (255, 0, 0): RGB (255,); // creates the fillrect (HDC, & rc, hbrush) function for solid color painting ); // use the specified image brush to fill the releasedc (hwnd, HDC); deleteobject (hbrush); // Delete the object created by the user} in the rectangle}

We can use the timer to implement simple animation screen protection programs:

Exp:

/* This instance code shows how to use the timer-clock Program */# include <windows. h> # include <winuser. h> # define id_timer 1 lresult callback wndproc (hwnd, uint message, wparam, lparam); // define a timer-related callback function void callback timerproc (hwnd, uint message, uint itimeid, DWORD dwtime); int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {static tchar szappclassname [] = text ("Beeper "); stati C tchar szappwndcaption [] = text ("Beeper"); hwnd; MSG; wndclass. cbclsextra = 0; wndclass. cbwndextra = 0; wndclass. hbrbackground = (hbrush) getstockobject (white_brush); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. hicon = loadicon (null, idi_application); wndclass. hinstance = hinstance; wndclass. lpfnwndproc = wndproc; wndclass. lpszclassname = szappclassname; wndclass. lpszmenuname = NULL; wndclas S. Style = cs_hredraw | cs_vredraw; If (! Registerclass (& wndclass) {MessageBox (null, "You need winnt to run this program! "," Warning ", mb_ OK); Return 0;} hwnd = createwindow (szappclassname, szappwndcaption, ws_visible | ws_popup, 0, 0, 1366,768, null, null, hinstance, null ); showwindow (hwnd, sw_showmaximized); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); dispatchmessage (& MSG );} return MSG. wparam;} // ****************** lresult callback wndproc (hwnd, uint message, wparam, lparam) {SW Itch (Message) {Case wm_create: settimer (hwnd, id_timer, 1000, timerproc);/* winuserapi uint winapi settimer (hwnd, use the uint nidevent handle of the timer window, the ID of the timer. The value can be customized. It is an unsigned integer uint uelapse and the timer time. The time base is Ms timerproc lptimerfunc). When the callback function of the timer message is defined, this value transmits the address of the timer message callback function */return 0; Case wm_keydown: Switch (wparam) {Case vk_escape: postquitmessage (0); break; default: break ;} case wm_destroy: killtimer (hwnd, id_timer); postq Uitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam);} // implements a timer-related callback function void callback timerproc (hwnd, // The Window handle uint message specified when settimer is called. // windows only sends the wm_timer message to the timer Message Processing callback function. Therefore, this value is always wm_timer uint itimeid, // This value is the id dword dwtime of the timer) // This value is compatible with the value returned from the gettickcount function, and is the number of seconds since Windows started {static bool fflipflop = false; hbrush; HDC; rect RC; // messagebeep (-1); fflipflop =! Fflipflop; getclientrect (hwnd, & rc); // obtain the size information of the user region. HDC = getdc (hwnd); hbrush = createsolidbrush (fflipflop? RGB (255, 0, 0): RGB (255,); // creates the fillrect (HDC, & rc, hbrush) function for solid color painting ); // use the specified image brush to fill the releasedc (hwnd, HDC); deleteobject (hbrush); // Delete the object created by the user} in the rectangle}

Timer usage is relatively easy to teach, mainly to understand the connection between the address of the callback function in the hwnd and settiemr functions of the timer callback function, and

Window handle correspondence.

The timer MUST be used according to the actual situation. You cannot use too many timers with too short timer time. Otherwise, the system performance will be significantly reduced. At the same time, you must

Note that the killtimer function must be called to cancel the set timer when the application exits.

If a timer does not need to be used, it is better to use the killtimer function to cancel the timer.

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.