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.
- 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:
- selectobject proto hdc:hdc, hgdiobject:dword
- when you're done with the bitmap, delete it with deleteobject api call.
- bitblt proto hdcdest:dword, nxdest:dword, nydest:dword, nwidth:dword, nheight:dword, hdcsrc:dword, nxsrc:dword, nysrc:dword, dwrop:dword
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.
example code:
.386.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.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