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

📄 lion-tutorial10.htm

📁 内有一些代码
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  Note another difference from the previous tutorial. When the window procedure 
  wants to get the text from the edit control, it calls GetDlgItemText function 
  instead of GetWindowText. GetDlgItemText accepts a control ID instead of a window 
  handle. That makes the call easier in the case you use a dialog box. <br>
<hr width="100%">
<br>
Now let's go to the second approach to using a dialog box as a main window. In 
the next example, I 'll create an application modal dialog box. You'll not find 
a message loop or a window procedure because they're not necessary! 
<hr width="100%">
<center>
  <b>dialog.asm (part 2)</b> 
</center>
<hr width="100%">
<br>
<b>.386</b> <br>
<b>.model flat,stdcall</b> <br>
<b>option casemap:none</b> 
<p><b>DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD</b> 
<p><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>includelib \masm32\lib\user32.lib</b> <br>
  <b>includelib \masm32\lib\kernel32.lib</b> 
<p><b>.data</b> <br>
  <b>DlgName db "MyDialog",0</b> <br>
  <b>AppName db "Our Second Dialog Box",0</b> <br>
  <b>TestString db "Wow! I'm in an edit box now",0</b> 
<p><b>.data?</b> <br>
  <b>hInstance HINSTANCE ?</b> <br>
  <b>CommandLine LPSTR ?</b> <br>
  <b>buffer db 512 dup(?)</b> 
<p><b>.const</b> <br>
  <b>IDC_EDIT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  equ 3000</b> <br>
  <b>IDC_BUTTON&nbsp;&nbsp;&nbsp;&nbsp; equ 3001</b> <br>
  <b>IDC_EXIT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  equ 3002</b> <br>
  <b>IDM_GETTEXT&nbsp; equ 32000</b> <br>
  <b>IDM_CLEAR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; equ 32001</b> <br>
  <b>IDM_EXIT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; equ 
  32002</b> <br>
  &nbsp; 
<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 DialogBoxParam, hInstance, ADDR DlgName,NULL, addr 
  DlgProc, NULL</b> <br>
  <b>&nbsp;&nbsp;&nbsp; invoke ExitProcess,eax</b> 
<p><b>DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</b> <br>
  <b>&nbsp;&nbsp;&nbsp; .IF uMsg==WM_INITDIALOG</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke GetDlgItem, hWnd,IDC_EDIT</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SetFocus,eax</b> <br>
  <b>&nbsp;&nbsp;&nbsp; .ELSEIF uMsg==WM_CLOSE</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendMessage,hWnd,WM_COMMAND,IDM_EXIT,0</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp; .ELSEIF uMsg==WM_COMMAND</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov eax,wParam</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .IF lParam==0</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .IF ax==IDM_GETTEXT</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  invoke GetDlgItemText,hWnd,IDC_EDIT,ADDR buffer,512</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  invoke MessageBox,NULL,ADDR buffer,ADDR AppName,MB_OK</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ELSEIF 
  ax==IDM_CLEAR</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  invoke SetDlgItemText,hWnd,IDC_EDIT,NULL</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ELSEIF 
  ax==IDM_EXIT</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  invoke EndDialog, hWnd,NULL</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ENDIF</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .ELSE</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov edx,wParam</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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;&nbsp;&nbsp;&nbsp; mov eax,FALSE</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret</b> <br>
  <b>&nbsp;&nbsp;&nbsp; .ENDIF</b> <br>
  <b>&nbsp;&nbsp;&nbsp; mov eax,TRUE</b> <br>
  <b>&nbsp;&nbsp;&nbsp; ret</b> <br>
  <b>DlgProc endp</b> <br>
  <b>end start</b> <br>
<hr width="100%">
<center>
  <b>dialog.rc (part 2)</b> 
</center>
<hr width="100%">
<br>
<b>#include "resource.h"</b> 
<p><b>#define IDC_EDIT&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  3000</b> <br>
  <b>#define IDC_BUTTON&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;&nbsp;&nbsp;&nbsp;&nbsp; 
  3001</b> <br>
  <b>#define IDC_EXIT&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  3002</b> 
<p><b>#define IDR_MEN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APPED | DS_MODALFRAME | DS_3DLOOK</b> <br>
  <b>CAPTION "Our Second Dialog Box"</b> <br>
  <b>MENU IDR_MENU1</b> <br>
  <b>BEGIN</b> <br>
  <b>&nbsp;&nbsp;&nbsp; EDITTEXT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  IDC_EDIT,&nbsp;&nbsp; 15,17,111,13, ES_AUTOHSCROLL | ES_LEFT</b> <br>
  <b>&nbsp;&nbsp;&nbsp; DEFPUSHBUTTON&nbsp;&nbsp; "Say Hello", IDC_BUTTON,&nbsp;&nbsp;&nbsp; 
  141,10,52,13</b> <br>
  <b>&nbsp;&nbsp;&nbsp; PUSHBUTTON&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "E&amp;xit", 
  IDC_EXIT,&nbsp; 141,26,52,13</b> <br>
  <b>END</b> <br>
  &nbsp; 
<p><b>IDR_MENU1&nbsp; MENU</b> <br>
  <b>BEGIN</b> <br>
  <b>&nbsp;&nbsp;&nbsp; POPUP "Test Controls"</b> <br>
  <b>&nbsp;&nbsp;&nbsp; BEGIN</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MENUITEM "Get Text", IDM_GETTEXT</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MENUITEM "Clear Text", IDM_CLEAR</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MENUITEM "", , 0x0800 /*MFT_SEPARATOR*/</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MENUITEM "E&amp;xit", IDM_EXIT</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp; END</b> <br>
  <b>END</b> 
<p> 
<hr width="100%">
<br>
The analysis follows: 
<p><b>&nbsp;&nbsp;&nbsp; DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD</b> 
<p>We declare the function prototype for DlgProc so we can refer to it with <b>addr 
  </b>operator in the line below: 
<p><b>&nbsp;&nbsp;&nbsp; invoke DialogBoxParam, hInstance, ADDR DlgName,NULL, 
  addr DlgProc, NULL</b> 
<p>The above line calls DialogBoxParam function which takes 5 parameters: the 
  instance handle, the name of the dialog box template, the parent window handle, 
  the address of the dialog box procedure, and the dialog-specific data. DialogBoxParam 
  creates a modal dialog box. It will not return until the dialog box is destroyed. 
<p><b>&nbsp;&nbsp;&nbsp; .IF uMsg==WM_INITDIALOG</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke GetDlgItem, hWnd,IDC_EDIT</b> 
  <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SetFocus,eax</b> <br>
  <b>&nbsp;&nbsp;&nbsp; .ELSEIF uMsg==WM_CLOSE</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendMessage,hWnd,WM_COMMAND,IDM_EXIT,0</b> 
<p>The dialog box procedure looks like a window procedure except that it doesn't 
  receive WM_CREATE message. The first message it receives is WM_INITDIALOG. Normally 
  you can put the initialization code here. Note that you must return the value 
  TRUE in eax if you process the message. <br>
  The internal dialog box manager doesn't send our dialog box procedure the WM_DESTROY 
  message by default when WM_CLOSE is sent to our dialog box. So if we want to 
  react when the user presses the close button on our dialog box, we must process 
  WM_CLOSE message. In our example, we send WM_COMMAND message with the value 
  IDM_EXIT in wParam. This has the same effect as when the user selects Exit menu 
  item. EndDialog is called in response to IDM_EXIT. <br>
  The processing of WM_COMMAND messages remains the same. <br>
  When you want to destroy the dialog box, the only way is to call EndDialog function. 
  Do not try DestroyWindow! EndDialog doesn't destroy the dialog box immediately. 
  It only sets a flag for the internal dialog box manager and continues to execute 
  the next instructions. <br>
  Now let's examine the resource file. The notable change is that instead of using 
  a text string as menu name we use a value, IDR_MENU1. This is necessary if you 
  want to attach a menu to a dialog box created with DialogBoxParam. Note that 
  in the dialog box template, you have to add the keyword <b>MENU</b> followed 
  by the menu resource ID. <br>
  A difference between the two examples in this tutorial that you can readily 
  observe is the lack of an icon in the latter example. However, you can set the 
  icon by sending the message WM_SETICON to the dialog box during WM_INITDIALOG. 
<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 + -