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

📄 tut25.html

📁 WINDOWS程序员使用指南--汇编基础
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <meta name="Author" content="Iczelion">
   <meta name="GENERATOR" content="Mozilla/4.7 [en] (Win98; I) [Netscape]">
   <title>Iczelion's Win32asm Tutorial 25: Simple Bitmap</title>
</head>
<body text="#FFFFFF" bgcolor="#000000" link="#FFFF00" vlink="#C0C0C0" alink="#C0FFC0">

<center>
<h1>
<font face="Arial,Helvetica"><font color="#FFFF99">Tutorial 25: Simple
Bitmap</font></font></h1></center>
<font face="Arial,Helvetica"><font size=-1>In this tutorial, we will learn
how to use bitmap in our program. To be exact, we will learn how to display
a bitmap in the client area of our window. Download <a href="files/tut25.zip">the
example</a>.</font></font>
<h3>
<font face="Arial,Helvetica"><font color="#FFCC33">Theory</font></font></h3>
<font face="Arial,Helvetica"><font size=-1>Bitmaps 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 as a resource. There
are two ways to do that. You can include the bitmap in the resource definition
file (.rc) as follows:</font></font>
<br>&nbsp;
<blockquote><font face="Arial,Helvetica">#define IDB_MYBITMAP&nbsp;&nbsp;
100</font>
<br><font face="Arial,Helvetica">IDB_MYBITMAP&nbsp; <b><font color="#FFFF99">BITMAP</font></b>&nbsp;
"c:\project\example.bmp"</font></blockquote>
<font face="Arial,Helvetica"><font size=-1>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.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>The other method uses a
name to represent the bitmap as follows:</font></font>
<blockquote><font face="Arial,Helvetica">MyBitMap&nbsp; <b><font color="#FFFF99">BITMAP</font></b>
"c:\project\example.bmp"</font></blockquote>
<font face="Arial,Helvetica"><font size=-1>This method requires that you
refer to the bitmap in your program by the string "MyBitMap" instead of
a value.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>Either method works fine
as long as you know which method you're using.</font></font>
<br><font face="Arial,Helvetica"><font size=-1>Now that we put the bitmap
in the resource file, we can go on with the steps in displaying it in the
client area of our window.</font></font>
<ol>
<li>
<font face="Arial,Helvetica"><font size=-1>call LoadBitmap to get the bitmap
handle. LoadBitmap has the following definition:</font></font></li>
</ol>

<ol>
<ol><b><font face="Arial,Helvetica">LoadBitmap proto hInstance:HINSTANCE,
lpBitmapName:LPSTR</font></b></ol>

<p><br><font face="Arial,Helvetica"><font size=-1>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:</font></font>
<ol>&nbsp;
<br><font face="Arial,Helvetica"><font size=-1>First Method:</font></font>
<p><font face="Arial,Helvetica"><font size=-1>.386</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.model flat, stdcall</font></font>
<br><font face="Arial,Helvetica"><font size=-1>................</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.const</font></font>
<br><font face="Arial,Helvetica"><font size=-1>IDB_MYBITMAP&nbsp;&nbsp;&nbsp;
equ 100</font></font>
<br><font face="Arial,Helvetica"><font size=-1>...............</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.data?</font></font>
<br><font face="Arial,Helvetica"><font size=-1>hInstance&nbsp; dd ?</font></font>
<br><font face="Arial,Helvetica"><font size=-1>..............</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.code</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.............</font></font>
<br><font face="Arial,Helvetica"><font size=-1>&nbsp;&nbsp;&nbsp; invoke
GetModuleHandle,NULL</font></font>
<br><font face="Arial,Helvetica"><font size=-1>&nbsp;&nbsp;&nbsp; mov hInstance,eax</font></font>
<br><font face="Arial,Helvetica"><font size=-1>............</font></font>
<br><font face="Arial,Helvetica"><font size=-1>&nbsp;&nbsp;&nbsp; invoke
LoadBitmap,hInstance,IDB_MYBITMAP</font></font>
<br><font face="Arial,Helvetica"><font size=-1>...........</font></font>
<p><font face="Arial,Helvetica"><font size=-1>Second Method:</font></font>
<p><font face="Arial,Helvetica"><font size=-1>.386</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.model flat, stdcall</font></font>
<br><font face="Arial,Helvetica"><font size=-1>................</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.data</font></font>
<br><font face="Arial,Helvetica"><font size=-1>BitmapName&nbsp; db "MyBitMap",0</font></font>
<br><font face="Arial,Helvetica"><font size=-1>...............</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.data?</font></font>
<br><font face="Arial,Helvetica"><font size=-1>hInstance&nbsp; dd ?</font></font>
<br><font face="Arial,Helvetica"><font size=-1>..............</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.code</font></font>
<br><font face="Arial,Helvetica"><font size=-1>.............</font></font>
<br><font face="Arial,Helvetica"><font size=-1>&nbsp;&nbsp;&nbsp; invoke
GetModuleHandle,NULL</font></font>
<br><font face="Arial,Helvetica"><font size=-1>&nbsp;&nbsp;&nbsp; mov hInstance,eax</font></font>
<br><font face="Arial,Helvetica"><font size=-1>............</font></font>
<br><font face="Arial,Helvetica"><font size=-1>&nbsp;&nbsp;&nbsp; invoke
LoadBitmap,hInstance,addr BitmapName</font></font>
<br><font face="Arial,Helvetica"><font size=-1>...........</font></font></ol>

<li>
<font face="Arial,Helvetica"><font size=-1>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.</font></font></li>

<li>
<font face="Arial,Helvetica"><font size=-1>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 CreateCompatibleDC.</font></font></li>
</ol>

<ol>
<ol><font face="Arial,Helvetica">CreateCompatibleDC&nbsp; proto&nbsp; hdc:HDC</font></ol>

<p><br><font face="Arial,Helvetica"><font size=-1>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.</font></font>
<li>
<font face="Arial,Helvetica"><font size=-1>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:</font></font></li>
</ol>

<ol>
<ol><font face="Arial,Helvetica"><font size=-1>SelectObject&nbsp;&nbsp;
proto&nbsp; hdc:HDC, hGdiObject:DWORD</font></font></ol>

<li>
<font face="Arial,Helvetica"><font size=-1>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:</font></font></li>
</ol>

<ol>
<ol><font face="Arial,Helvetica"><font size=-1>BitBlt&nbsp; proto&nbsp;
hdcDest:DWORD, nxDest:DWORD, nyDest:DWORD, nWidth:DWORD, nHeight:DWORD,
hdcSrc:DWORD, nxSrc:DWORD, nySrc:DWORD, dwROP:DWORD</font></font>
<br><font face="Arial,Helvetica"><font size=-1></font></font>&nbsp;</ol>
<font face="Arial,Helvetica"><font size=-1><b><font color="#FFFF99">hdcDest</font></b>
is the handle of the device context that serves as the destination of bitmap
transfer operation</font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#FFFF99">nxDest,
nyDest </font></b>are the coordinate of the upper left corner of the output
area</font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#FFFF99">nWidth,
nHeight </font></b>are the width and height of the output area</font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#FFFF99">hdcSrc
</font></b>is
the handle of the device context that serves as the source of bitmap transfer
operation</font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#FFFF99">nxSrc,
nySrc </font></b>are the coordinate of the upper left corner of the source
rectangle.</font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#FFFF99">dwROP
</font></b>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.</font></font>
<li>
<font face="Arial,Helvetica"><font size=-1>When you're done with the bitmap,
delete it with DeleteObject API call.</font></font></li>
</ol>
<font face="Arial,Helvetica"><font size=-1>That's it! To recapitulate,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -