📄 tut25.html
字号:
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 the memory DC
then copy the content of the memory DC to the real DC.</font></font>
<h3>
<font face="Arial,Helvetica">Example Code:</font></h3>
<tt>.386</tt>
<br><tt>.model flat,stdcall</tt>
<br><tt>option casemap:none</tt>
<br><tt>include \masm32\include\windows.inc</tt>
<br><tt>include \masm32\include\user32.inc</tt>
<br><tt>include \masm32\include\kernel32.inc</tt>
<br><tt>include \masm32\include\gdi32.inc</tt>
<br><tt>includelib \masm32\lib\user32.lib</tt>
<br><tt>includelib \masm32\lib\kernel32.lib</tt>
<br><tt>includelib \masm32\lib\gdi32.lib</tt>
<p><tt>WinMain proto :DWORD,:DWORD,:DWORD,:DWORD</tt>
<br><b><tt><font color="#66FF99">IDB_MAIN equ 1</font></tt></b>
<p><tt>.data</tt>
<br><tt>ClassName db "SimpleWin32ASMBitmapClass",0</tt>
<br><tt>AppName db "Win32ASM Simple Bitmap Example",0</tt>
<p><tt>.data?</tt>
<br><tt>hInstance HINSTANCE ?</tt>
<br><tt>CommandLine LPSTR ?</tt>
<br><tt>hBitmap dd ?</tt>
<p><tt>.code</tt>
<br><tt>start:</tt>
<br><tt> invoke GetModuleHandle, NULL</tt>
<br><tt> mov hInstance,eax</tt>
<br><tt> invoke GetCommandLine</tt>
<br><tt> mov CommandLine,eax</tt>
<br><tt> invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT</tt>
<br><tt> invoke ExitProcess,eax</tt>
<p><tt>WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD</tt>
<br><tt> LOCAL wc:WNDCLASSEX</tt>
<br><tt> LOCAL msg:MSG</tt>
<br><tt> LOCAL hwnd:HWND</tt>
<br><tt> mov wc.cbSize,SIZEOF WNDCLASSEX</tt>
<br><tt> mov wc.style, CS_HREDRAW or CS_VREDRAW</tt>
<br><tt> mov wc.lpfnWndProc, OFFSET WndProc</tt>
<br><tt> mov wc.cbClsExtra,NULL</tt>
<br><tt> mov wc.cbWndExtra,NULL</tt>
<br><tt> push hInstance</tt>
<br><tt> pop wc.hInstance</tt>
<br><tt> mov wc.hbrBackground,COLOR_WINDOW+1</tt>
<br><tt> mov wc.lpszMenuName,NULL</tt>
<br><tt> mov wc.lpszClassName,OFFSET ClassName</tt>
<br><tt> invoke LoadIcon,NULL,IDI_APPLICATION</tt>
<br><tt> mov wc.hIcon,eax</tt>
<br><tt> mov wc.hIconSm,eax</tt>
<br><tt> invoke LoadCursor,NULL,IDC_ARROW</tt>
<br><tt> mov wc.hCursor,eax</tt>
<br><tt> invoke RegisterClassEx, addr wc</tt>
<br><tt> INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\</tt>
<br><tt> WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\</tt>
<br><tt> CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\</tt>
<br><tt> hInst,NULL</tt>
<br><tt> mov hwnd,eax</tt>
<br><tt> invoke ShowWindow, hwnd,SW_SHOWNORMAL</tt>
<br><tt> invoke UpdateWindow, hwnd</tt>
<br><tt> .while TRUE</tt>
<br><tt> invoke GetMessage, ADDR msg,NULL,0,0</tt>
<br><tt> .break .if (!eax)</tt>
<br><tt> invoke TranslateMessage, ADDR msg</tt>
<br><tt> invoke DispatchMessage, ADDR msg</tt>
<br><tt> .endw</tt>
<br><tt> mov eax,msg.wParam</tt>
<br><tt> ret</tt>
<br><tt>WinMain endp</tt>
<p><tt>WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</tt>
<br><tt> LOCAL ps:PAINTSTRUCT</tt>
<br><tt> LOCAL hdc:HDC</tt>
<br><tt> LOCAL hMemDC:HDC</tt>
<br><tt> LOCAL rect:RECT</tt>
<br><tt> .if uMsg==WM_CREATE</tt>
<br><b><tt><font color="#66FF99"> invoke
LoadBitmap,hInstance,IDB_MAIN</font></tt></b>
<br><b><tt><font color="#66FF99"> mov hBitmap,eax</font></tt></b>
<br><tt> .elseif uMsg==WM_PAINT</tt>
<br><b><tt><font color="#66FF99"> invoke
BeginPaint,hWnd,addr ps</font></tt></b>
<br><b><tt><font color="#66FF99"> mov
hdc,eax</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
CreateCompatibleDC,hdc</font></tt></b>
<br><b><tt><font color="#66FF99"> mov
hMemDC,eax</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
SelectObject,hMemDC,hBitmap</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
GetClientRect,hWnd,addr rect</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
BitBlt,hdc,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
DeleteDC,hMemDC</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
EndPaint,hWnd,addr ps</font></tt></b>
<br><tt> .elseif uMsg==WM_DESTROY</tt>
<br><b><tt><font color="#66FF99"> invoke DeleteObject,hBitmap</font></tt></b>
<br><tt> invoke PostQuitMessage,NULL</tt>
<br><tt> .ELSE</tt>
<br><tt> invoke DefWindowProc,hWnd,uMsg,wParam,lParam</tt>
<br><tt> ret</tt>
<br><tt> .ENDIF</tt>
<br><tt> xor eax,eax</tt>
<br><tt> ret</tt>
<br><tt>WndProc endp</tt>
<br><tt>end start</tt>
<p><tt>;---------------------------------------------------------------------</tt>
<br><tt>;
The resource script</tt>
<br><tt>;---------------------------------------------------------------------</tt>
<br><b><tt><font color="#FFFF99">#define IDB_MAIN 1</font></tt></b>
<br><b><tt><font color="#FFFF99">IDB_MAIN BITMAP "tweety78.bmp"</font></tt></b>
<h3>
<font face="Arial,Helvetica"><font size=+1>Analysis:</font></font></h3>
<font face="Arial,Helvetica"><font size=-1>There is not much to analyze
in this tutorial ;)</font></font>
<br>
<blockquote><b><tt><font color="#FFFF99">#define IDB_MAIN 1</font></tt></b>
<br><b><tt><font color="#FFFF99">IDB_MAIN BITMAP "tweety78.bmp"</font></tt></b></blockquote>
<b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>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.</font></font></font></b>
<p><tt> .if uMsg==WM_CREATE</tt>
<br><b><tt><font color="#66FF99"> invoke
LoadBitmap,hInstance,IDB_MAIN</font></tt></b>
<br><b><tt><font color="#66FF99"> mov hBitmap,eax</font></tt></b>
<p><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>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.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>Now
that the bitmap is loaded, we can paint it in the client area of our main
window.</font></font></font></b>
<p><tt> .elseif uMsg==WM_PAINT</tt>
<br><b><tt><font color="#66FF99"> invoke
BeginPaint,hWnd,addr ps</font></tt></b>
<br><b><tt><font color="#66FF99"> mov
hdc,eax</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
CreateCompatibleDC,hdc</font></tt></b>
<br><b><tt><font color="#66FF99"> mov
hMemDC,eax</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
SelectObject,hMemDC,hBitmap</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
GetClientRect,hWnd,addr rect</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
BitBlt,hdc,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
DeleteDC,hMemDC</font></tt></b>
<br><b><tt><font color="#66FF99"> invoke
EndPaint,hWnd,addr ps</font></tt></b>
<p><font face="Arial,Helvetica"><font color="#FFFFFF"><font size=-1>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.</font></font></font>
<blockquote><tt> .elseif uMsg==WM_DESTROY</tt>
<br><b><tt><font color="#66FF99"> invoke DeleteObject,hBitmap</font></tt></b>
<br><tt> invoke PostQuitMessage,NULL</tt></blockquote>
<font face="Arial,Helvetica"><font size=-1>When we don't need the bitmap
anymore, we delete it with DeleteObject</font></font>
<br>
<hr WIDTH="100%">
<center><b>[<a href="http://win32asm.cjb.net">Iczelion's Win32 Assembly
HomePage</a>]</b></center>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -