Compilation Tutorial: Bitmap Preliminary

Source: Internet
Author: User
Tags define definition integer resource

In this lesson, we'll learn how to use bitmaps in a program. More precisely, we're going to learn how to display bitmaps in a client area of a window.

Theory

A bitmap is a picture that is stored in a computer. Bitmap files have quite a number of formats (translator: e.g.. Bmp. Jpg. Gif. PIC, and so on) but Windows only supports Windows Bitmap Graphics format, that is, BMP files. The bitmap referred to in this lesson is also a BMP file. The easiest way to use a bitmap is to define it in a resource file (. rc). There are two ways to define a method. The first method is to define it as an integer macro, as follows:

#define IDB_MYBITMAP 100

Idb_mybitmap BITMAP "C:\project\example.bmp"

In the first line we define an integer macro with a value of 100. The second line is to point the integer macro to the bitmap that you want to define, so that the compiler knows the path where the bitmap is located.

Another way is to give it a name, which is to define it as a string, as follows:

Mybitmap BITMAP "C:\project\example.bmp"

The effects of the two methods are the same. Which method you choose depends on whether you prefer to use an integer macro or a string to point to a bitmap in your program.

Now that we have defined the bitmap in the resource file, the next step is to display it in the client area of the window.

In the program, we use the API function LoadBitmap to get the bitmap handle. The following is the end of the LoadBitmap function:

LoadBitmap Proto Hinstance:hinstance, lpbitmapname:lpstr

The function returns a bitmap handle. The function has two parameters, where hinstance is the program handle. Lpbitmapname is a pointer to a bitmap name (for the second definition method). If you use the first definition method, you can fill in the value or integer macro that points to the bitmap (the corresponding value is 100, and the integer macro is Idb_mybitmap). Here's a simple example:

The first method:


.386
. Model Flat, StdCall
................
. const
Idb_mybitmap EQU 100
...............
. Data?
HINSTANCE DD?
..............
. Code
.............
Invoke Getmodulehandle,null
MOV hinstance,eax
............
Invoke Loadbitmap,hinstance,idb_mybitmap
...........

The second method:


.386
. Model Flat, StdCall
................
. Data
Bitmapname db "Mybitmap", 0
...............
. Data?
HINSTANCE DD?
..............
. Code
.............
Invoke Getmodulehandle,null
MOV hinstance,eax
............
Invoke Loadbitmap,hinstance,addr Bitmapname
...........

Gets a device text (DC) handle. You can get it by BeginPaint the API function when responding to WM_PAINT messages. If in other messages, it can be obtained using API function GetDC.

Create a memory image for this DC. The purpose of this is to create a "hidden paper", the bitmap "painting" On top, for buffering. When we finish this work, we copy the bitmap on the "paper" to the real DC through a function. This is the double buffering technique for displaying images quickly on the screen. (Translator: Can reduce the image jitter) This "paper" with the API function CreateCompatibleDC established, the following is its end type:

CreateCompatibleDC Proto HDC:HDC

If the function succeeds, it returns a handle to the DC memory image, which is also "paper."

Now we have the "paper", you can put a bit of the picture on the top. This can be done via API function SelectObject, where the first argument is a handle to "paper", the second argument is a handle to a bitmap, and the following is the end of the function:

SelectObject Proto Hdc:hdc, Hgdiobject:dword

Now the bitmap has been painted on the "paper". Next we're going to copy the bitmap to the real DC. There are many API functions that can do this, such as BitBlt and StretchBlt. function BitBlt simply copies the contents of one DC to another, and function StretchBlt automatically adjusts the size of the source DC replicated content to fit the output area of the destination DC, so the former is faster than the latter. Here we only use function BitBlt, and here's the end of it:
BitBlt Proto Hdcdest:dword, Nxdest:dword, Nydest:dword, Nwidth:dword, Nheight:dword, Hdcsrc:dword, Nxsrc:dword, NySrc:D WORD, Dwrop:dword

The handle of the Hdcdest destination DC.

Nxdest, nydest the upper-left corner coordinate of the output area of the destination DC.

nwidth, nheight the length and width of the DC output area of the destination.

HDCSRC the source DC handle.

NXSRC, nysrc the upper-left corner coordinate of the area to be replicated in the source DC.

Dwrop screen Operation code (ROP). This parameter is used to determine which method of operation the color of the copied content and the original color of the output area are processed. Typically, you simply overwrite the output area with the copied content.

After everything is done, use API function DeleteObject to release the bitmap object, that is, "erase" the bitmap.

Done! Now let's review the whole process: first, you need to define the bitmap in the resource file. Then you need to load the bitmap resource in the program and get the bitmap handle. Then you need to get a DC to the bitmap output area, and create the memory image of the DC, and put the bitmap into this DC memory image. Finally, the bitmap is copied from the DC memory image to the real DC.

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