#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"
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.
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:
.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
...........
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.
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
invoke 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"
#define IDB_MAIN 1Define 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.
IDB_MAIN BITMAP "tweety78.bmp"
.if uMsg==WM_CREATE
invoke
LoadBitmap,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_DESTROYWhen we don't need the bitmap anymore, we delete it with DeleteObject
invoke DeleteObject,hBitmap
invoke PostQuitMessage,NULL