tutorial 25: simple bitmap
in this tutorial, we will learn how to use bitmap in our program. to be exact, we will learn how to display a bitmap in the client area of our window. download the example.
theory
bitmaps can be thought of as pictures stored in computer. there are many picture formats used with computers but windows only natively supports windows bitmap graphics files (.bmp). the bitmaps i'll refer to in this tutorial are windows bitmap graphics files. the easiest way to use a bitmap is to use it as a resource. there are two ways to do that. you can include the bitmap in the resource definition file (.rc) as follows:#define idb_mybitmap 100this method uses a constant to represent the bitmap. the first line just creates a constant named idb_mybitmap which has the value of 100. we will use this label to refer to the bitmap in the program. the next line declares a bitmap resource. it tells the resource compiler where to find the actual bmp file.
idb_mybitmap bitmap "c:\project\example.bmp"
the other method uses a name to represent the bitmap as follows:
mybitmap bitmap "c:\project\example.bmp"this method requires that you refer to the bitmap in your program by the string "mybitmap" instead of a value.
either method works fine as long as you know which method you're using.
now that we put the bitmap in the resource file, we can go on with the steps in displaying it in the client area of our window.
- call loadbitmap to get the bitmap handle. loadbitmap has the following definition:
- obtain a handle to device context (dc). you can obtain this handle by calling beginpaint in response to wm_paint message or by calling getdc anywhere.
- create a memory device context which has the same attribute as the device context we just obtained. the idea here is to create a kind of "hidden" drawing surface which we can draw the bitmap on. when we are finished with the operation, we just copy the content of the hidden drawing surface to the actual device context in one function call. it's an example of double-buffer technique used for fast display of pictures on the screen. you can create this "hidden" drawing surface by calling createcompatibledc.
- loadbitmap proto hinstance:hinstance, lpbitmapname:lpstr
this function returns a bitmap handle. hinstance is the instance handle of our program. lpbitmapname is a pointer to the string that is the name of the bitmap (incase you use the second method to refer to the bitmap). if you use a constant to refer to the bitmap (like idb_mybitmap), you can put its value here. (in the example above it would be 100). a short example is in order:
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
...........
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
...........
- createcompatibledc proto hdc:hdc