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

📄 lion-tutorial11.htm

📁 内有一些代码
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<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>
&nbsp; 
<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>
&nbsp; 
<blockquote><b>OPENFILENAME&nbsp; STRUCT</b> 
  <blockquote><b>&nbsp;lStructSize DWORD&nbsp; ?</b> <br>
    <b>&nbsp;hwndOwner HWND&nbsp; ?</b> <br>
    <b>&nbsp;hInstance HINSTANCE ?</b> <br>
    <b>&nbsp;lpstrFilter LPCSTR&nbsp; ?</b> <br>
    <b>&nbsp;lpstrCustomFilter LPSTR&nbsp; ?</b> <br>
    <b>&nbsp;nMaxCustFilter DWORD&nbsp; ?</b> <br>
    <b>&nbsp;nFilterIndex DWORD&nbsp; ?</b> <br>
    <b>&nbsp;lpstrFile LPSTR&nbsp; ?</b> <br>
    <b>&nbsp;nMaxFile DWORD&nbsp; ?</b> <br>
    <b>&nbsp;lpstrFileTitle LPSTR&nbsp; ?</b> <br>
    <b>&nbsp;nMaxFileTitle DWORD&nbsp; ?</b> <br>
    <b>&nbsp;lpstrInitialDir LPCSTR&nbsp; ?</b> <br>
    <b>&nbsp;lpstrTitle LPCSTR&nbsp; ?</b> <br>
    <b>&nbsp;Flags&nbsp; DWORD&nbsp; ?</b> <br>
    <b>&nbsp;nFileOffset WORD&nbsp; ?</b> <br>
    <b>&nbsp;nFileExtension WORD&nbsp; ?</b> <br>
    <b>&nbsp;lpstrDefExt LPCSTR&nbsp; ?</b> <br>
    <b>&nbsp;lCustData LPARAM&nbsp; ?</b> <br>
    <b>&nbsp;lpfnHook DWORD&nbsp; ?</b> <br>
    <b>&nbsp;lpTemplateName LPCSTR&nbsp; ?</b></blockquote>
  <b>OPENFILENAME&nbsp; ENDS</b></blockquote>
Let's see the meaning of the frequently used members. <br>
&nbsp; 
<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&nbsp; pairs of null terminated strings. 
        The first string in each pair is the description. The second string is 
        the filter pattern. for example: <br>
        &nbsp;&nbsp;&nbsp;&nbsp; FilterString&nbsp;&nbsp; db "All Files (*.*)",0, 
        "*.*",0 <br>
        &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; 
        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.&nbsp; <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&nbsp; db "Our Main Window",0</b> <br>
  <b>MenuName db "FirstMenu",0</b> <br>
  <b>ofn&nbsp;&nbsp; OPENFILENAME &lt;></b> <br>
  <b>FilterString db "All Files",0,"*.*",0</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  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&nbsp; 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>&nbsp;&nbsp;&nbsp; invoke GetModuleHandle, NULL</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp; hInstance,eax</b> <br>
  <b>&nbsp;&nbsp;&nbsp; invoke GetCommandLine<br>
  &nbsp;&nbsp;&nbsp;&nbsp;mov CommandLine,eax</b> <br>
  <b>&nbsp;&nbsp;&nbsp; invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp; invoke ExitProcess,eax</b> 
<p><b>WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp; LOCAL wc:WNDCLASSEX</b> <br>
  <b>&nbsp;&nbsp;&nbsp; LOCAL msg:MSG</b> <br>
  <b>&nbsp;&nbsp;&nbsp; LOCAL hwnd:HWND</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.cbSize,SIZEOF WNDCLASSEX</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.style, CS_HREDRAW or CS_VREDRAW</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.lpfnWndProc, OFFSET WndProc</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.cbClsExtra,NULL</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.cbWndExtra,NULL</b> <br>
  <b>&nbsp;&nbsp;&nbsp; push&nbsp; hInst</b> <br>
  <b>&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp; wc.hInstance</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.hbrBackground,COLOR_WINDOW+1</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.lpszMenuName,OFFSET MenuName</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.lpszClassName,OFFSET ClassName</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp; invoke LoadIcon,NULL,IDI_APPLICATION</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.hIcon,eax</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.hIconSm,eax</b> <br>
  <b>&nbsp;&nbsp;&nbsp; invoke LoadCursor,NULL,IDC_ARROW</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp; wc.hCursor,eax</b> <br>
  <b>&nbsp;&nbsp;&nbsp; invoke RegisterClassEx, addr wc</b> <br>
  <b>&nbsp;&nbsp;&nbsp; invoke CreateWindowEx,WS_EX_CLIENTEDGE,ADDR ClassName,ADDR 
  AppName,\</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CW_USEDEFAULT,300,200,NULL,NULL,\</b> 
  <br>

⌨️ 快捷键说明

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