📄 lion-tutorial12.htm
字号:
and CS_HREDRAW styles. We use this opportunity to resize our edit control to
the same size as the client area of the parent window. First we have to know
the current width and height of the client area of the parent window. We get
this info from lParam. The high word of lParam contains the height and the low
word of lParam the width of the client area. We then use the information to
resize the edit control by calling MoveWindow function which, in addition to
changing the position of the window, can alter the size too.
<p><b> .if ax==IDM_OPEN</b>
<br>
<b>
mov ofn.Flags, OFN_FILEMUSTEXIST or \</b> <br>
<b>
OFN_PATHMUSTEXIST or OFN_LONGNAMES or\</b> <br>
<b>
OFN_EXPLORER or OFN_HIDEREADONLY</b> <br>
<b>
invoke GetOpenFileName, ADDR ofn</b>
<p>When the user selects File/Open menu item, we fill in the Flags member of ofn
structure and call GetOpenFileName function to display the open file dialog
box.
<p><b>
.if eax==TRUE</b> <br>
<b>
invoke CreateFile,ADDR buffer,\</b> <br>
<b>
GENERIC_READ or GENERIC_WRITE ,\</b> <br>
<b>
FILE_SHARE_READ or FILE_SHARE_WRITE,\</b> <br>
<b>
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,\</b> <br>
<b>
NULL</b> <br>
<b>
mov hFile,eax</b>
<p>After the user selects a file to open, we call CreateFile to open the file.
We specifies that the function should try to open the file for read and write.
After the file is opened, the function returns the handle to the opened file
which we store in a global variable for future use. This function has the following
syntax:
<p><b>CreateFile proto lpFileName:DWORD,\</b> <br>
<b>
dwDesiredAccess:DWORD,\</b> <br>
<b>
dwShareMode:DWORD,\</b> <br>
<b>
lpSecurityAttributes:DWORD,\</b> <br>
<b>
dwCreationDistribution:DWORD\,</b> <br>
<b>
dwFlagsAndAttributes:DWORD\,</b> <br>
<b>
hTemplateFile:DWORD</b>
<p><b>dwDesiredAccess</b> specifies which operation you want to perform on the
file.
<ul>
<li> <b>0 </b> Open the file to query its attributes. You have to rights
to read or write the data.</li>
<li> <b>GENERIC_READ</b> Open the file for reading.</li>
<li> <b>GENERIC_WRITE</b> Open the file for writing.</li>
</ul>
<b>dwShareMode</b> specifies which operation you want to allow other processes
to perform on the file that 's being opened.
<ul>
<li> <b>0</b> Don't share the file with other processes.</li>
<li> <b>FILE_SHARE_READ</b> allow other processes to read the data from
the file being opened</li>
<li> <b>FILE_SHARE_WRITE</b> allow other processes to write data to the
file being opened.</li>
</ul>
<b>lpSecurityAttributes</b> has no significance under Windows 95. <br>
<b>dwCreationDistribution</b> specifies the action CreateFile will perform when
the file specified in lpFileName exists or when it doesn't exist.
<ul>
<li> <b>CREATE_NEW</b> Creates a new file. The function fails if the specified
file already exists.</li>
<li> <b>CREATE_ALWAYS</b> Creates a new file. The function overwrites the file
if it exists.</li>
<li> <b>OPEN_EXISTING</b> Opens the file. The function fails if the file does
not exist.</li>
<li> <b>OPEN_ALWAYS</b> Opens the file, if it exists. If the file does not exist,
the function creates the file as if dwCreationDistribution were CREATE_NEW.</li>
<li> <b>TRUNCATE_EXISTING</b> Opens the file. Once opened, the file is truncated
so that its size is zero bytes. The calling process must open the file with
at least GENERIC_WRITE access. The function fails if the file does not exist.</li>
</ul>
dwFlagsAndAttributes specifies the file attributes
<ul>
<li> <b>FILE_ATTRIBUTE_ARCHIVE</b> The file is an archive file. Applications
use this attribute to mark files for backup or removal.</li>
<li> <b>FILE_ATTRIBUTE_COMPRESSED</b> The file or directory is compressed. For
a file, this means that all of the data in the file is compressed. For a directory,
this means that compression is the default for newly created files and subdirectories.</li>
<li> <b>FILE_ATTRIBUTE_NORMAL</b> The file has no other attributes set. This
attribute is valid only if used alone.</li>
<li> <b>FILE_ATTRIBUTE_HIDDEN</b> The file is hidden. It is not to be included
in an ordinary directory listing.</li>
<li> <b>FILE_ATTRIBUTE_READONLY</b> The file is read only. Applications can
read the file but cannot write to it or delete it.</li>
<li> <b>FILE_ATTRIBUTE_SYSTEM </b>The file is part of or is used exclusively
by the operating system.</li>
</ul>
<b>
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE</b> <br>
<b>
mov hMemory,eax</b> <br>
<b>
invoke GlobalLock,hMemory</b> <br>
<b>
mov pMemory,eax</b>
<p>When the file is opened, we allocate a block of memory for use by ReadFile
and WriteFile functions. We specify GMEM_MOVEABLE flag to let Windows
move the memory block around to consolidate memory. GMEM_ZEROINIT flag tells
GlobalAlloc to fill the newly allocated memory block with zeroes. <br>
When GlobalAlloc returns successfully, eax contains the handle to the allocated
memory block. We pass this handle to GlobalLock function which returns a pointer
to the memory block.
<p><b>
invoke ReadFile,hFile,pMemory,MEMSIZE-1,ADDR SizeReadWrite,NULL</b> <br>
<b>
invoke SendMessage,hwndEdit,WM_SETTEXT,NULL,pMemory</b>
<p>When the memory block is ready for use, we call ReadFile function to read data
from the file. When a file is first opened or created, the file pointer is at
offset 0. So in this case, we start reading from the first byte in the file
onwards. The first parameter of ReadFile is the handle of the file to read,
the second is the pointer to the memory block to hold the data, next is the
number of bytes to read from the file, the fourth param is the address of the
variable of DWORD size that will be filled with the number of bytes actually
read from the file. <br>
After we fill the memory block with the data, we put the data into the edit
control by sending WM_SETTEXT message to the edit control with lParam containing
the pointer to the memory block. After this call, the edit control shows the
data in its client area.
<p><b>
invoke CloseHandle,hFile</b> <br>
<b>
invoke GlobalUnlock,pMemory</b> <br>
<b>
invoke GlobalFree,hMemory</b> <br>
<b>
.endif</b>
<p>At this point, we have no need to keep the file open any longer since our purpose
is to write the modified data from the edit control to another file, not the
original file. So we close the file by calling CloseHandle with the file handle
as its parameter. Next we unlock the memory block and free it. Actually you
don't have to free the memory at this point, you can reuse the memory block
during the save operation later. But for demonstration purpose , I choose to
free it here.
<p><b>
invoke SetFocus,hwndEdit</b>
<p>When the open file dialog box is displayed on the screen, the input focus shifts
to it. So after the open file dialog is closed, we must move the input focus
back to the edit control. <br>
This end the read operation on the file. At this point, the user can edit the
content of the edit control.And when he wants to save the data to another file,
he must select File/Save as menuitem which displays a save as dialog box. The
creation of the save as dialog box is not much different from the open file
dialog box. In fact, they differ in only the name of the functions, GetOpenFileName
and GetSaveFileName. You can reuse most members of the ofn structure too except
the Flags member.
<p><b>
mov ofn.Flags,OFN_LONGNAMES or\</b> <br>
<b>
OFN_EXPLORER or OFN_HIDEREADONLY</b>
<p>In our case, we want to create a new file, so OFN_FILEMUSTEXIST and OFN_PATHMUSTEXIST
must be left out else the dialog box will not let us create a file that doesn't
already exist. <br>
The dwCreationDistribution parameter of the CreateFile function must be changed
to <b>CREATE_NEW</b> since we want to create a new file. <br>
The remaining code is identical to those in the open file section except the
following:
<p><b>
invoke SendMessage,hwndEdit,WM_GETTEXT,MEMSIZE-1,pMemory</b> <br>
<b>
invoke WriteFile,hFile,pMemory,eax,ADDR SizeReadWrite,NULL</b>
<p>We send WM_GETTEXT message to the edit control to copy the data from it to
the memory block we provide, the return value in eax is the length of the data
inside the buffer. After the data is in the memory block, we write them to the
new file.<strong> </strong>
<hr size="1">
<div align="center"> This article come from Iczelion's asm page, Welcom to <a href="http://asm.yeah.net">http://asm.yeah.net</a></div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -