Windows Programming (3)

Source: Internet
Author: User

Where should I start if I have asked you to write a mine clearance program without relevant information?

Let's first think about what the Mine Clearance Program has: menus, stopwatches, record the number of mines, resetting, and minefield. The key to the entire process lies in the minefield. If there is no stopwatch, record the number of mines, and other functions, we can barely play with mine clearance, but if there is no minefield, we won't be able to play. So what is the minefield? There must be a data structure to record the number of mines and the number of surrounding areas. The main part of the interaction between the program and you is to click on the minefield and then display an image, you can also double-click the minefield to open the surrounding area. To put it bluntly, we are drawing a picture. So the whole mine clearance process should begin with drawing.

Lresult callback wndproc (hwnd, uint message, wparam, lparam) {static hbitmap; static int cxclient, cyclient, cxsource, cysource; // bitmap class structure, contains bitmap information Bitmap bitmap; // device content handle HDC, hdcmem; // instance handle of this message, because it is only obtained in the wm_create message, therefore, the hinstance; int X, Y, MX, my; // paintstruct structure of the created window contains information for drawing the customer region, the first parameter is the paintstruct PS of the HDC class; // The variable int dstx = 100; int dsty = 100; Switch (Message) {// create window message: the first message, Case wm_create: // lpcreatestruct, is a pointer to the createstruct structure. // after the forced type conversion of lparam, it points to the struct, And the hinstancehinstance In the struct is (lpcreatestruct) lparam) -> hinstance; // loads a bitmap. The parameter is an instance handle and a bitmap. The returned value is a bitmap handle. // you can use the bitmap name to load the bitmap, however, the serial number hbitmap = loadbitmap (hinstance, makeintresource (idb_bitmap2) is usually used; // obtain information from the specified graphic object // The parameter is: Graphical handle, size of the Object Information buffer, GetObject (hbitmap, sizeof (Bitmap), & Bitmap); // The width of a bitmap cxsource = bitmap. bmwidth; // the height of a bitmap: The entire bitmap consists of 16 different images, each of which should be 1 cysource = bitmap of the entire bitmap. bmheight/16; return 0; // change the window size. Message case wm_size: // The height and height of the 32-bit value indicate the length and width of the customer zone. // different messages must be retained, therefore, staticcxclient = loword (lparam); cyclient = hiword (lparam); Return 0; // draw the window message case wm_paint: // beginpaint function to prepare the drawing, fill in the paintstruct structure with information // The parameter is: The Window handle, the drawing information pointing to the paintstruct structure, the return value is a hdchdc = beginpaint (hwnd, & PS ); // The device environment for memory creation. The returned value is the handle hdcmem = createcompatibledc (HDC) of a memory device environment. // select an object to the device environment. The parameter is: device environment handle, object handle SelectObject (hdcmem, hbitmap); For (y = dsty; y <cyclient-dsty; y ++ = cysource) {for (x = dstx; x <cxclient-dstx; x + = cxsource) {// texture function. The parameter is the target DC handle, the X coordinate in the upper left corner of the texture point, and the Y coordinate in the upper left corner of the texture point, the length of the target and source, the width of the target and the circle, the source DC handle, the X coordinate in the upper left corner of the source, the Y coordinate in the upper left corner of the source, and the grating operation code (copy the source rectangle directly to the target rectangle) bitblt (HDC, X, Y, cxsource, cysource, hdcmem, srccopy) ;}} deletedc (hdcmem); // draw the end Of the window endpaint (hwnd, & PS ); return 0;

Note that the size of the customer zone is obtained using the wm_size message. In the double loop of the wm_paint message, it is used to change the number of bricks to be pasted.

So how can we open it by clicking the mouse?

Case wm_lbuttondown: // The left button will show the opened dashboard effect. HDC = getdc (hwnd); hdcmem = createcompatibledc (HDC); SelectObject (hdcmem, hbitmap ); // select the minefield and draw an opened image. // The mouse points to the minefield, which does not work. // The mouse points to the minefield, use the upper left corner of the square to replace the position of the texture // The Position of the mouse: MX = (loword (lparam); my = (hiword (lparam )); // valid only when it comes to the minefield. The location of the minefield is dsty, left and right dstx if (MX> dstx & MX <cxclient-dstx & my> dsty & my <cyclient-dsty) {// change the cursor position to the upper left corner of the square brick of the corresponding minefield at this point. // the result is always MX = (INT) (Mx-dstx) on the first brick) /cxsource) * cxsource + dstx; my = (INT) (my-dsty)/cysource) * cysource + dsty; // texture function, parameter: Target DC handle, X coordinate in the upper left corner of the texture point, Y coordinate in the upper left corner of the texture point, length of the target rectangle, width of the target rectangle, source DC handle, X coordinate in the upper left corner of the source, and Y coordinate in the upper left corner of the source, grating operation code (copy the source rectangle directly to the target rectangle) bitblt (HDC, MX, my, cxsource, cysource, hdcmem, 0, cysource * 15, srccopy );} deletedc (hdcmem); releasedc (hwnd, HDC); Return 0;
// Close the window message case wm_destroy: postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam); // execute the default message}

You just need to click it, change the place of the mouse point to the place of the square brick it belongs to, and then paste another square brick. The principle of location conversion is actually very simple, that is, the use of the int type data in C language, for example:

If the size of a square brick is 16*16 and the place of your mouse is (30/16), the upper left corner of the brick to which it belongs must be at = 1, 50/16 = 3 is the first column, and the specific pixel of the third row is (1*16, 3*16 ).

 

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.