?? lion-tutorial10.htm
字號(hào):
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
equ 3000</b> <br>
<b>IDC_BUTTON equ 3001</b> <br>
<b>IDC_EXIT
equ 3002</b> <br>
<b>IDM_GETTEXT equ 32000</b> <br>
<b>IDM_CLEAR equ 32001</b> <br>
<b>IDM_EXIT equ
32002</b> <br>
<p><b>.code</b> <br>
<b>start:</b> <br>
<b> invoke GetModuleHandle, NULL</b> <br>
<b> mov hInstance,eax</b> <br>
<b> invoke DialogBoxParam, hInstance, ADDR DlgName,NULL, addr
DlgProc, NULL</b> <br>
<b> invoke ExitProcess,eax</b>
<p><b>DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</b> <br>
<b> .IF uMsg==WM_INITDIALOG</b> <br>
<b> invoke GetDlgItem, hWnd,IDC_EDIT</b>
<br>
<b> invoke SetFocus,eax</b> <br>
<b> .ELSEIF uMsg==WM_CLOSE</b> <br>
<b> invoke SendMessage,hWnd,WM_COMMAND,IDM_EXIT,0</b>
<br>
<b> .ELSEIF uMsg==WM_COMMAND</b> <br>
<b> mov eax,wParam</b> <br>
<b> .IF lParam==0</b> <br>
<b> .IF ax==IDM_GETTEXT</b>
<br>
<b>
invoke GetDlgItemText,hWnd,IDC_EDIT,ADDR buffer,512</b> <br>
<b>
invoke MessageBox,NULL,ADDR buffer,ADDR AppName,MB_OK</b> <br>
<b> .ELSEIF
ax==IDM_CLEAR</b> <br>
<b>
invoke SetDlgItemText,hWnd,IDC_EDIT,NULL</b> <br>
<b> .ELSEIF
ax==IDM_EXIT</b> <br>
<b>
invoke EndDialog, hWnd,NULL</b> <br>
<b> .ENDIF</b>
<br>
<b> .ELSE</b> <br>
<b> mov edx,wParam</b>
<br>
<b> shr edx,16</b>
<br>
<b> .if dx==BN_CLICKED</b>
<br>
<b>
.IF ax==IDC_BUTTON</b> <br>
<b>
invoke SetDlgItemText,hWnd,IDC_EDIT,ADDR TestString</b> <br>
<b>
.ELSEIF ax==IDC_EXIT</b> <br>
<b>
invoke SendMessage,hWnd,WM_COMMAND,IDM_EXIT,0</b> <br>
<b>
.ENDIF</b> <br>
<b> .ENDIF</b>
<br>
<b> .ENDIF</b> <br>
<b> .ELSE</b> <br>
<b> mov eax,FALSE</b> <br>
<b> ret</b> <br>
<b> .ENDIF</b> <br>
<b> mov eax,TRUE</b> <br>
<b> 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
3000</b> <br>
<b>#define IDC_BUTTON
3001</b> <br>
<b>#define IDC_EXIT
3002</b>
<p><b>#define IDR_MENU1
3003</b>
<p><b>#define IDM_GETTEXT
32000</b> <br>
<b>#define IDM_CLEAR
32001</b> <br>
<b>#define IDM_EXIT
32003</b> <br>
<p><b>MyDialog DIALOG 10, 10, 205, 60</b> <br>
<b>STYLE 0x0004 | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |</b> <br>
<b>WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | 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> EDITTEXT
IDC_EDIT, 15,17,111,13, ES_AUTOHSCROLL | ES_LEFT</b> <br>
<b> DEFPUSHBUTTON "Say Hello", IDC_BUTTON,
141,10,52,13</b> <br>
<b> PUSHBUTTON "E&xit",
IDC_EXIT, 141,26,52,13</b> <br>
<b>END</b> <br>
<p><b>IDR_MENU1 MENU</b> <br>
<b>BEGIN</b> <br>
<b> POPUP "Test Controls"</b> <br>
<b> BEGIN</b> <br>
<b> MENUITEM "Get Text", IDM_GETTEXT</b>
<br>
<b> MENUITEM "Clear Text", IDM_CLEAR</b>
<br>
<b> MENUITEM "", , 0x0800 /*MFT_SEPARATOR*/</b>
<br>
<b> MENUITEM "E&xit", IDM_EXIT</b>
<br>
<b> END</b> <br>
<b>END</b>
<p>
<hr width="100%">
<br>
The analysis follows:
<p><b> 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> 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> .IF uMsg==WM_INITDIALOG</b> <br>
<b> invoke GetDlgItem, hWnd,IDC_EDIT</b>
<br>
<b> invoke SetFocus,eax</b> <br>
<b> .ELSEIF uMsg==WM_CLOSE</b> <br>
<b> 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>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -