⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tut25.html

📁 WINDOWS程序员使用指南--汇编基础
💻 HTML
📖 第 1 页 / 共 2 页
字号:
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&nbsp;&nbsp; equ 1</font></tt></b>
<p><tt>.data</tt>
<br><tt>ClassName db "SimpleWin32ASMBitmapClass",0</tt>
<br><tt>AppName&nbsp; 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>&nbsp;invoke GetModuleHandle, NULL</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp;&nbsp; hInstance,eax</tt>
<br><tt>&nbsp;invoke GetCommandLine</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp;&nbsp; CommandLine,eax</tt>
<br><tt>&nbsp;invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT</tt>
<br><tt>&nbsp;invoke ExitProcess,eax</tt>
<p><tt>WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD</tt>
<br><tt>&nbsp;LOCAL wc:WNDCLASSEX</tt>
<br><tt>&nbsp;LOCAL msg:MSG</tt>
<br><tt>&nbsp;LOCAL hwnd:HWND</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.cbSize,SIZEOF WNDCLASSEX</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.style, CS_HREDRAW or CS_VREDRAW</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.lpfnWndProc, OFFSET WndProc</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.cbClsExtra,NULL</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.cbWndExtra,NULL</tt>
<br><tt>&nbsp;push&nbsp; hInstance</tt>
<br><tt>&nbsp;pop&nbsp;&nbsp; wc.hInstance</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.hbrBackground,COLOR_WINDOW+1</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.lpszMenuName,NULL</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.lpszClassName,OFFSET ClassName</tt>
<br><tt>&nbsp;invoke LoadIcon,NULL,IDI_APPLICATION</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.hIcon,eax</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.hIconSm,eax</tt>
<br><tt>&nbsp;invoke LoadCursor,NULL,IDC_ARROW</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; wc.hCursor,eax</tt>
<br><tt>&nbsp;invoke RegisterClassEx, addr wc</tt>
<br><tt>&nbsp;INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hInst,NULL</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp; hwnd,eax</tt>
<br><tt>&nbsp;invoke ShowWindow, hwnd,SW_SHOWNORMAL</tt>
<br><tt>&nbsp;invoke UpdateWindow, hwnd</tt>
<br><tt>&nbsp;.while TRUE</tt>
<br><tt>&nbsp; invoke GetMessage, ADDR msg,NULL,0,0</tt>
<br><tt>&nbsp; .break .if (!eax)</tt>
<br><tt>&nbsp; invoke TranslateMessage, ADDR msg</tt>
<br><tt>&nbsp; invoke DispatchMessage, ADDR msg</tt>
<br><tt>&nbsp;.endw</tt>
<br><tt>&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp; eax,msg.wParam</tt>
<br><tt>&nbsp;ret</tt>
<br><tt>WinMain endp</tt>
<p><tt>WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</tt>
<br><tt>&nbsp;&nbsp; LOCAL ps:PAINTSTRUCT</tt>
<br><tt>&nbsp;&nbsp; LOCAL hdc:HDC</tt>
<br><tt>&nbsp;&nbsp; LOCAL hMemDC:HDC</tt>
<br><tt>&nbsp;&nbsp; LOCAL rect:RECT</tt>
<br><tt>&nbsp;&nbsp; .if uMsg==WM_CREATE</tt>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
LoadBitmap,hInstance,IDB_MAIN</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov hBitmap,eax</font></tt></b>
<br><tt>&nbsp;&nbsp; .elseif uMsg==WM_PAINT</tt>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
BeginPaint,hWnd,addr ps</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;
hdc,eax</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
CreateCompatibleDC,hdc</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;
hMemDC,eax</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
SelectObject,hMemDC,hBitmap</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
GetClientRect,hWnd,addr rect</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
BitBlt,hdc,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
DeleteDC,hMemDC</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
EndPaint,hWnd,addr ps</font></tt></b>
<br><tt>&nbsp;.elseif uMsg==WM_DESTROY</tt>
<br><b><tt><font color="#66FF99">&nbsp; invoke DeleteObject,hBitmap</font></tt></b>
<br><tt>&nbsp; invoke PostQuitMessage,NULL</tt>
<br><tt>&nbsp;.ELSE</tt>
<br><tt>&nbsp; invoke DefWindowProc,hWnd,uMsg,wParam,lParam</tt>
<br><tt>&nbsp; ret</tt>
<br><tt>&nbsp;.ENDIF</tt>
<br><tt>&nbsp;xor eax,eax</tt>
<br><tt>&nbsp;ret</tt>
<br><tt>WndProc endp</tt>
<br><tt>end start</tt>
<p><tt>;---------------------------------------------------------------------</tt>
<br><tt>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
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>&nbsp;
<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>&nbsp;&nbsp; .if uMsg==WM_CREATE</tt>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
LoadBitmap,hInstance,IDB_MAIN</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp; .elseif uMsg==WM_PAINT</tt>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
BeginPaint,hWnd,addr ps</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;
hdc,eax</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
CreateCompatibleDC,hdc</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;
hMemDC,eax</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
SelectObject,hMemDC,hBitmap</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
GetClientRect,hWnd,addr rect</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
BitBlt,hdc,0,0,rect.right,rect.bottom,hMemDC,0,0,SRCCOPY</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke
DeleteDC,hMemDC</font></tt></b>
<br><b><tt><font color="#66FF99">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;.elseif uMsg==WM_DESTROY</tt>
<br><b><tt><font color="#66FF99">&nbsp; invoke DeleteObject,hBitmap</font></tt></b>
<br><tt>&nbsp; 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 + -