TUTORIAL 25: Simple Bitmap
In this Tutorial, We Well Learn How To Use Bitmap in Our Program. To Be Exact, We Well Learn How To Display A Bitmap in The Client Area of Our Window. Download
The example.
TheoryBitmaps 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 it is to do what. You can include the bitmap in the resource definition file (.rc) as Follows:
#define idb_mybitmap 100
IDB_MYBITMAP
Bitmap "c: /project/example.bmp"
This 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.
The Other Method Uses a name to represent the bitmap as stock:
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 secret area of our window.
Call loadbitmap to get the bitmap handle. loadingBitmap Has The Following Definition:
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 loadingBitmap , hinstance, idb_mybitmap ...........
Second Method:
.386 .Model flat, stdcall .............. .data Bitmapname DB "mybitmap", 0 .............. .data? Hinstance dd? .............. .code .......................................... .. invoke loadingbitmap, hinstance, addr bitmapname ...........
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 CreateCompatiPLEDC.
CreateCompatiPledc Proto HDC: HDC
If this function succeeds, it returns the handle of the memory device context in eax. Hdc is the handle to the device context that you want the memory DC to be compatible with.Now that you got a hidden drawing surface, you can draw on it .. by selecting the bitmap into it This is done by calling SelectObject with the handle to the memory DC as the first parameter and the bitmap handle as the second parameter SelectObject has the following definition:
SelectObject Proto HDC: HDC, HGDiobject: DWORD
The bitmap is drawn on the memory device context now. All we need to do here is to copy it to the actual display device, namely the true device context. There are several functions that can perform this operation such as BitBlt and StretchBlt. BitBlt just Copies The Content of One DC To Another So It's Fast While Stretchblt Can Stretch Or Compress The Bitmap To Fit The Output Area. We Will Use Bitblt Here for Simplicity. Bitblt Has The Following Definition:
Bitblt Proto HDCDEST: DWORD, NXDEST: DWORD, NYDEST: DWORD, NWIDTH: DWORD, NHEIGHT: DWORD, HDCSRC: DWORD, NXSRC: DWORD, NYSRC: DWORD, DWROP: DWORD
HDCDEST IS The Handle of The Device Context That Serves as The Destination of Bitmap Transfer Operation
NXDEST, NYDEST Are The Coordinate of The Upper Left Corner of The Output Area
NWIDTH, NHEIGHT Are The Width and Height of the Output Area
HDCSRC Is The Handle of The Device Context That Serves as The Source of Bitmap Transfer Operation
NXSRC, Nysrc Are The Coordinate of The Upper Left Corner of The Source Rectangle.
dwROP is the raster-operation code (hence the acronym ROP) that governs how to combine the color data of the bitmap to the existing color data on the output area to achieve the final result. Most of the time, you only want to overwrite the existing color data with the new one.When you're done with the bitmap, delete it with DeleteObject API call. That's it! to recapitulate, you need to put the bitmap into the resource scipt. Then load it from the resource with LoadBitmap. you'll get the bitmap handle. Next you obtain the handle to the device context of the area you want to paint the bitmap on. Then you create a memory device context that is compatible with the device context you just obtained. Select the bitmap into ...............
Example Code: .386
.Model flat, stdcall
Option CaseMAP: NONE
INCLUDE /MASM32/INCLUDE/Windows.inc
INCLUDE /MASM32/INCLUDE/USER32.INC
INCLUDE /MASM32/INCLUDE / WANEL32.INC
INCLUDE /MASM32/INCLUDE/gdi32.inc
INCLUDELIB /MASM32/LIB/USER32.LIB
INCLUDELIB /MASM32/LIB/kernel32.lib
INCLUDELIB /MASM32/LIB/GDI32.LIB
Winmain Proto: DWORD,: DWORD,: DWORD,: DWORD IDB_MAIN EQU 1
.DATA CLASSNAME DB "SimpleWin32asmbitmapclass", 0 AppName DB "Win32ASM Simple Bitmap Example", 0
.DATA? HINSTANCE HINSTANCE? COMMANDLINE LPSTR? HBITMAP DD?
. Code Start: Invoke GetModuleHandle, Null Mov Hinstance, Eax Invoke Getcommandline Mov Commandline, Eax Invoke Winmain, Hinstance, Null, CommandLine, SW_SHOWDEFAULT INVOKE EXITPROCESS, EAX
WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD LOCAL wc: WNDCLASSEX LOCAL msg: MSG LOCAL hwnd: HWND mov wc.cbSize, SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra, NULL mov wc.cbWndExtra, NULL push hInstance pop wc.hInstance mov wc.hbrBackground, COLOR_WINDOW 1 mov wc.lpszMenuName, NULL mov wc.lpszClassName, OFFSET ClassName invoke LoadIcon, NULL, IDI_APPLICATION mov wc.hIcon , eax mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_ARROW mov wc.hCursor, eax invoke RegisterClassEx, addr wc INVOKE CreateWindowEx, NULL, aDDR ClassName, aDDR AppName, / WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, / CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, / hInst, NULL mov hwnd, eax invoke ShowWindow, hwnd, SW_SHOWNORMAL invoke UpdateWindow, hwnd .while TRUE invoke GetMessage, ADDR msg, NULL, 0,0 .break .if (! eax) invoke TranslateMessage, ADDR msg invok E DispatchMessage, Addr Msg .Endw Mov Eax, Msg.wParam Ret Winmain Endp
WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM LOCAL ps: PAINTSTRUCT LOCAL hdc: HDC LOCAL hMemDC: HDC LOCAL rect: RECT .if uMsg == WM_CREATE invoke LoadBitmap, hInstance, IDB_MAIN mov hBitmap, eax. elseif uMsg == WM_PAINT invoke BeginPaint, hWnd, addr ps mov hdc, eax invoke CreateCompatibleDC, hdc mov hMemDC, eax invoke SelectObject, hMemDC, hBitmap invoke GetClientRect, hWnd, addr rect invoke BitBlt, hdc, 0,0, rect.right, rect.bottom, hMemDC, 0,0, SRCCOPY invoke DeleteDC, hMemDC invoke EndPaint, hWnd, addr ps .elseif uMsg == WM_DESTROY invoke DeleteObject, hBitmap invoke PostQuitMessage, NULL .ELSE invoke DefWindowProc, hWnd, uMsg, wParam, lParam ret. Endif xor Eax, EAX RET WNDPROC ENDP End Start; --------------------------------------- ------------------------------; The resource script; --------------- -------------------------------------------------- ---- #Define IDB_MAIN 1 IDB _Main Bitmap "Tweety78.BMP"
Analysis: there is not much to analyze in this tutorial;
#define idb_main 1
IDB_MAIN bitmap "Tweety78.BMP"
Define a constant named IDB_MAIN, assign 1 as its value. And then use that constant as the bitmap resource identifier. The bitmap file to be included in the resource is "tweety78.bmp" which resides in the same folder as the resource script.
.IF uMsg == wm_create invoke loadingbitmap, hinstance, idb_main mov hbitmap, EAX
In response to WM_CREATE, we call LoadBitmap to load the bitmap from the resource, passing the bitmap's resource identifier as the second parameter to the API. We get the handle to the bitmap when the function returns. Now that the bitmap is loaded, we can paint it in the client area of our main window..elseif uMsg == WM_PAINT invoke BeginPaint, hWnd, addr ps mov hdc, eax invoke CreateCompatibleDC, hdc mov hMemDC, eax invoke SelectObject, hMemDC, hBitmap invoke GetClientRect, hWnd, addr rect invoke Bitblt, HDC, 0, 0, Rect.right, Rect.bottom, Hmemdc, 0, 0, Srccopy Invoke deletedc, HMEMDC Invoke Endpaint, HWnd, Addr PS
We choose to paint the bitmap in response to WM_PAINT message. We first call BeginPaint to obtain the handle to the device context. Then we create a compatible memory DC with CreateCompatibleDC. Next select the bitmap into the memory DC with SelectObject. Determine the dimension of the client area with GetClientRect. Now we can display the bitmap in the client area by calling BitBlt which copies the bitmap from the memory DC to the real DC. When the painting is done, we have no further need for the memory DC so we delete IT with deletedc. End Painting session with endpaint.
.ELSEIF uMSG == WM_DESTROY
Invoke deleteObject, hbitmap
Invoke PostquitMessage, NULL
WHEN we don't need it Bitmap Anymore, We delete it with deleteObject