Windows Programming (4): timer for decomposing the Mine Clearance Program

Source: Internet
Author: User

I forgot to explain it last time. Because I want to do a realistic mine clearance, the bitmap of my mine clearance program is all from the mine clearance under WindowsXP. How does one obtain a bitmap? My job is Windows 7, and the mine clearance is different from that of XP. I first downloaded a mine clearance under XP, and then used a software: PE e to obtain the bitmap, then load these bitmaps in my program.

This is a timer. The mine-clearing timer is not of the hour, minute, or second type, but of the hundred, ten, or second type.

If you have ever looked over a minefield, you should be able to understand that the Timer shows that time changes are also being drawn, constantly changing from 0 ~ 9. Now we only need a clock message to trigger the drawing. In Windows, the message wm_timer exists. How is this message used?

When you want to trigger a timer, you must first set the timer (settimer); when the clock arrives, write the Response Program in wm_timer; when no timer is needed, cancel the timer (killtimer ). This is also the general idea of my program: When the wm_create message arrives, first draw the hour, draw in the upper right corner, the value is all 0; when you click the left mouse button to start timing, when the clock arrives, send a custom message. Draw a picture under the custom message (showing the clock beat). When the wm_destroy message arrives, cancel the clock.

The macro defined in the program is used to indicate the timer, because we can set multiple timers, each timer clock interval is different to complete the timer function in mine clearance. Instead of doing this, I set a timer with a one-second hop and set an integer variable sec to record the number of seconds. SEC % 10 indicates the number of seconds. (SEC % 100)/10 indicates dozens of seconds. (This is mainly because an error occurs when 10 is directly divisible, so remove the hundred bits first); SEC/100 indicates several hundred seconds.

With so many worships, let's look at the program!

# Include <windows. h> # include "resource. H "# define id_timer_1 1 // custom message # define wm_mymsg (wm_user + 100) lresult callback wndproc (hwnd, uint, wparam, lparam); int winapi winmain (hinstance, // The current instance handle hinstance hprevinstance, // The previous instance handle lpstr lpcmdline, // command line int ncmdshow) // display status {static tchar szappname [] = text ("My Timer"); // window handle hwnd; // message MSG; // window class wndclass; // window style: when the window is moved or the size of the window is changed, the wndclass is repainted. s Tyle = cs_hredraw | cs_vredraw; // specifies the callback function wndclass. lpfnwndproc = wndproc; // The extra bit is used to determine the position of the next window class. wndclass is not used for the moment. cbclsextra = 0; // The extra bit is used to confirm the location of the next window instance. wndclass is not used for the moment. cbwndextra = 0; // instance handle wndclass. hinstance = hinstance; // The Mount icon wndclass. hicon = loadicon (null, idi_application); // load the cursor wndclass. hcursor = loadcursor (null, idc_arrow); // wndclass. hbrbackground = (hbrush) getstockobject (white_brush); // menu: No wndclass. lpsz Menuname = NULL; // window class name wndclass. lpszclassname = szappname; // register the window if (! Registerclass (& wndclass) {return-1 ;}// create a window hwnd = createwindow (szappname, // name of the window class, must be a registered text ("timer"), // window title ws_overlappedwindow, // window style cw_usedefault, // X coordinate cw_usedefault, // y coordinate cw_usedefault, // width cw_usedefault, // height null, // parent window handle null, // menu window handle hinstance, // WINDOS of the advanced version ignores NULL ); //// display window showwindow (hwnd, sw_showna); // update window updatewindow (hwnd); // message loop while (getmessage (& MSG, null )) {translatemessage (& MSG); // send the message to the window dispatchmessage (& MSG);} return MSG. wparam;} // callback function: The main function of this program is to design a timer lresult callback wndproc (hwnd, uint message, wparam, lparam) {// bitmap handle static hbitmap hbitmap_clc; // client size static int cxclient, cyclient; // resource size static int cxsource_clc, cysource_clc; // bitmap bitmap_clc; // device content handle HDC, hdcmem; // instance handle hinstance; // coordinate information int X, Y; // drawing structure information paintstruct pS; // The timing variable static int sec = 0; Switch (Message) {// create a message: load the bitmap to obtain the bitmap Information Case wm_create: // obtain the instance handle hinstance = (lpcreatestruct) lparam)-> hinstance; // load the bitmap hbitmap_clc = loadbitmap (hinstance, makeintresource (idb_bitmap1); // obtain the bitmap information, put it in bitmap_clc to GetObject (hbitmap_clc, sizeof (Bitmap), & bitmap_clc); // length and width of each number cxsource_clc = bytes; cysource_clc = bitmap_clc.bmheight/12; // obtain the size of the current customer partition case wm_size: cxclient = loword (lparam); cyclient = hiword (lparam); Return 0; // drawing message: Set the default texture case wm_paint: HDC = beginpaint (hwnd, & PS); // create compatible memory device content hdcmem = createcompatibledc (HDC) for the specified device; // select the object to the device content: the parameter is the device content handle, and the object handle is SelectObject (hdcmem, hbitmap_clc); // start drawing // texture position: the rightmost is 1 second, and the left is 10 seconds, another point on the left is 100 seconds, starting all 0 bitblt (HDC, cxClient-cxSource_clc, 0, cxsource_clc, cysource_clc, hdcmem, 0, cysource_clc * 11, srccopy); bitblt (HDC, cxClient-cxSource_clc *, medium, cysource_clc, hdcmem, 0, cysource_clc * 11, srccopy); bitblt (HDC, cxClient-cxSource_clc *, cxsource_clc, cysource_clc, hdcmem, 0, limit * 11, srccopy); deletedc (hdcmem); endpaint (hwnd, & PS); Return 0; // click with the left mouse button: start timing case wm_lbuttondown: settimer (hwnd, id_timer_1, 1000, null); Return 0; // the arrival of the clock: sends the custom message case wm_timer: messagebeep (mb_iconasterisk); SEC ++; // sends the custom message sendmessage (hwnd, wm_mymsg, wparam, lparam); Return 0; // custom message, start drawing and change the clock value case wm_mymsg: HDC = getdc (hwnd); hdcmem = createcompatibledc (HDC ); selectObject (hdcmem, hbitmap_clc); // start drawing // number position: 0: 12th; position: cysource_clc * 11; 1: 11th; position: cysource_clc * 10, bitblt (HDC, cxClient-cxSource_clc, 0, cxsource_clc, cysource_clc, hdcmem, 0, cysource_clc * (11-sec % 10), srccopy); bitblt (HDC, cxClient-cxSource_clc *, large, small, small, hdcmem, 0, cysource_clc * (11-(SEC % 100)/10), srccopy); bitblt (HDC, cxClient-cxSource_clc *, cxsource_clc, cysource_clc, hdcmem, 0, cysource_clc * (11-sec/100), srccopy); deletedc (hdcmem); releasedc (hwnd, HDC); Return 0; Case wm_destroy: postquitmessage (0); killtimer (hwnd, id_timer_1); Return 0;} // return defwindowproc (hwnd, message, wparam, lparam) by default );}

If you are not using the bitmap that comes with Windows mine clearance, there will certainly be an error!

Related Article

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