?? lion-tutorial11.htm
字號:
<html>
<head>
<link rel="stylesheet" href="../../asm.css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Iczelion's win32 asm tutorial</title>
</head>
<body bgcolor="#FFFFFF" background="../../images/back01.jpg">
<p align="center">Tutorial 11: More about Dialog Box</p>
<hr size="1">
We will learn more about dialog box in this tutorial. Specifically, we will explore
the topic of how to use dialog boxs as our input-output devices. If you read the
previous tutorial, this one will be a breeze since only a minor modification is
all that's needed to be able to use dialog boxes as adjuncts to our main window.
Also in this tutorial, we will learn how to use common dialog boxes.
<p>Download the dialog box examples <a href="files/tut11-1.zip">here</a> and <a href="files/tut11-2.zip">here</a>.
Download Common Dialog Box example <a href="files/tut11-3.zip">here</a>.
<h3> Theory:</h3>
Very little is to be said about how to use dialog boxes as input-output devices
of our program. Your program creates the main window as usual and when you want
to display the dialog box, just call CreateDialogParam or DialogBoxParam. With
DialogBoxParam call, you don't have to do anything more, just process the messages
in the dialog box procedure. With CreateDialogParam, you must insert IsDialogMessage
call in the message loop to let dialog box manager handle the keyboard navigation
in your dialog box for you. Since the two cases are trivial, I'll not put the
source code here. You can download the examples and examine them yourself, <a href="files/tut11-1.zip">here</a>
and <a href="files/tut11-2.zip">here</a>. <br>
Let's go on to the common dialog boxes. Windows has prepared predefined dialog
boxes for use by your applications. These dialog boxes exist to provide standardized
user interface. They consist of file, print, color, font, and search dialog boxes.
You should use them as much as possible. The dialog boxes reside in comdlg32.dll.
In order to use them, you have to link to comdlg32.lib. You create these dialog
boxes by calling appropriate functions in the common dialog library. For open
file dialog, it is GetOpenFileName, for save as dialog it is GetSaveFileName,
for print dialog it is PrintDlg and so on. Each one of these functions takes a
pointer to a structure as its parameter. You should look them up in Win32 API
reference. In this tutorial, I'll demonstrate how to create and use an open file
dialog. <br>
Below is the function prototype of GetOpenFileName function: <br>
<blockquote><b>GetOpenFileName proto lpofn:DWORD</b></blockquote>
You can see that it receives only one parameter, a pointer to an OPENFILENAME
structure. The return value TRUE means the user selected a file to open, it's
FALSE otherwise. We will look at OPENFILENAME structure next. <br>
<blockquote><b>OPENFILENAME STRUCT</b>
<blockquote><b> lStructSize DWORD ?</b> <br>
<b> hwndOwner HWND ?</b> <br>
<b> hInstance HINSTANCE ?</b> <br>
<b> lpstrFilter LPCSTR ?</b> <br>
<b> lpstrCustomFilter LPSTR ?</b> <br>
<b> nMaxCustFilter DWORD ?</b> <br>
<b> nFilterIndex DWORD ?</b> <br>
<b> lpstrFile LPSTR ?</b> <br>
<b> nMaxFile DWORD ?</b> <br>
<b> lpstrFileTitle LPSTR ?</b> <br>
<b> nMaxFileTitle DWORD ?</b> <br>
<b> lpstrInitialDir LPCSTR ?</b> <br>
<b> lpstrTitle LPCSTR ?</b> <br>
<b> Flags DWORD ?</b> <br>
<b> nFileOffset WORD ?</b> <br>
<b> nFileExtension WORD ?</b> <br>
<b> lpstrDefExt LPCSTR ?</b> <br>
<b> lCustData LPARAM ?</b> <br>
<b> lpfnHook DWORD ?</b> <br>
<b> lpTemplateName LPCSTR ?</b></blockquote>
<b>OPENFILENAME ENDS</b></blockquote>
Let's see the meaning of the frequently used members. <br>
<center>
<table BORDER width="100%" >
<tr>
<td>lStructSize</td>
<td>The size of the OPENFILENAME structure , in bytes</td>
</tr>
<tr>
<td>hwndOwner</td>
<td>The window handle of the open file dialog box.</td>
</tr>
<tr>
<td>hInstance</td>
<td>Instance handle of the application that creates the open file dialog
box</td>
</tr>
<tr>
<td>lpstrFilter</td>
<td>The filter strings in the format of pairs of null terminated strings.
The first string in each pair is the description. The second string is
the filter pattern. for example: <br>
FilterString db "All Files (*.*)",0,
"*.*",0 <br>
db "Text Files (*.txt)",0,"*.txt",0,0 <br>
Note that only the pattern in the second string in each pair is actually
used by Windows to filter out the files. Also noted that you have to put
an extra 0 at the end of the filter strings to denote the end of it.</td>
</tr>
<tr>
<td>nFilterIndex</td>
<td>Specify which pair of the filter strings will be initially used when
the open file dialog is first displayed. The index is 1-based, that is
the first pair is 1, the second pair is 2 and so on. So in the above example,
if we specify nFilterIndex as 2, the second pattern, "*.txt" will be used.</td>
</tr>
<tr>
<td>lpstrFile</td>
<td>Pointer to the buffer that contains the filename used to initialize
the filename edit control on the dialog box. The buffer should be at least
260 bytes long. <br>
After the user selects a file to open, the filename with full path is
stored in this buffer. You can extract the information from it later.</td>
</tr>
<tr>
<td>nMaxFile</td>
<td>The size of the lpstrFile buffer.</td>
</tr>
<tr>
<td>lpstrTitle</td>
<td>Pointer to the title of the open file dialog box</td>
</tr>
<tr>
<td>Flags</td>
<td>Determine the styles and characteristics of the dialog box.</td>
</tr>
<tr>
<td>nFileOffset</td>
<td>After the user selects a file to open, this member contains the index
to the first character of the actual filename. For example, if the full
name with path is "c:\windows\system\lz32.dll", the this member will contain
the value 18.</td>
</tr>
<tr>
<td>nFileExtension</td>
<td>After the user selects a file to open, this member contains the index
to the first character of the file extension</td>
</tr>
</table>
</center>
<h3> Example:</h3>
The following program displays an open file dialog box when the user selects File->
Open from the menu. When the user selects a file in the dialog box, the program
displays a message box showing the full name, filename,and extension of the selected
file.
<p><b>.386</b> <br>
<b>.model flat,stdcall</b> <br>
<b>option casemap:none</b> <br>
<b>WinMain proto :DWORD,:DWORD,:DWORD,:DWORD</b> <br>
<b>include \masm32\include\windows.inc</b> <br>
<b>include \masm32\include\user32.inc</b> <br>
<b>include \masm32\include\kernel32.inc</b> <br>
<b>include \masm32\include\comdlg32.inc</b> <br>
<b>includelib \masm32\lib\user32.lib</b> <br>
<b>includelib \masm32\lib\kernel32.lib</b> <br>
<b>includelib \masm32\lib\comdlg32.lib</b>
<p><b>.const</b> <br>
<b>IDM_OPEN equ 1</b> <br>
<b>IDM_EXIT equ 2</b> <br>
<b>MAXSIZE equ 260</b> <br>
<b>OUTPUTSIZE equ 512</b>
<p><b>.data</b> <br>
<b>ClassName db "SimpleWinClass",0</b> <br>
<b>AppName db "Our Main Window",0</b> <br>
<b>MenuName db "FirstMenu",0</b> <br>
<b>ofn OPENFILENAME <></b> <br>
<b>FilterString db "All Files",0,"*.*",0</b> <br>
<b>
db "Text Files",0,"*.txt",0,0</b> <br>
<b>buffer db MAXSIZE dup(0)</b> <br>
<b>OurTitle db "-=Our First Open File Dialog Box=-: Choose the file to open",0</b>
<br>
<b>FullPathName db "The Full Filename with Path is: ",0</b> <br>
<b>FullName db "The Filename is: ",0</b> <br>
<b>ExtensionName db "The Extension is: ",0</b> <br>
<b>OutputString db OUTPUTSIZE dup(0)</b> <br>
<b>CrLf db 0Dh,0Ah,0</b>
<p><b>.data?</b> <br>
<b>hInstance HINSTANCE ?</b> <br>
<b>CommandLine LPSTR ?</b>
<p><b>.code</b> <br>
<b>start:</b> <br>
<b> invoke GetModuleHandle, NULL</b> <br>
<b> mov hInstance,eax</b> <br>
<b> invoke GetCommandLine<br>
mov CommandLine,eax</b> <br>
<b> invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT</b>
<br>
<b> invoke ExitProcess,eax</b>
<p><b>WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD</b>
<br>
<b> LOCAL wc:WNDCLASSEX</b> <br>
<b> LOCAL msg:MSG</b> <br>
<b> LOCAL hwnd:HWND</b> <br>
<b> mov wc.cbSize,SIZEOF WNDCLASSEX</b> <br>
<b> mov wc.style, CS_HREDRAW or CS_VREDRAW</b>
<br>
<b> mov wc.lpfnWndProc, OFFSET WndProc</b> <br>
<b> mov wc.cbClsExtra,NULL</b> <br>
<b> mov wc.cbWndExtra,NULL</b> <br>
<b> push hInst</b> <br>
<b> pop wc.hInstance</b> <br>
<b> mov wc.hbrBackground,COLOR_WINDOW+1</b> <br>
<b> mov wc.lpszMenuName,OFFSET MenuName</b> <br>
<b> mov wc.lpszClassName,OFFSET ClassName</b>
<br>
<b> invoke LoadIcon,NULL,IDI_APPLICATION</b> <br>
<b> mov wc.hIcon,eax</b> <br>
<b> mov wc.hIconSm,eax</b> <br>
<b> invoke LoadCursor,NULL,IDC_ARROW</b> <br>
<b> mov wc.hCursor,eax</b> <br>
<b> invoke RegisterClassEx, addr wc</b> <br>
<b> invoke CreateWindowEx,WS_EX_CLIENTEDGE,ADDR ClassName,ADDR
AppName,\</b> <br>
<b> WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\</b>
<br>
<b> CW_USEDEFAULT,300,200,NULL,NULL,\</b>
<br>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -