📄 lion-tutorial09.htm
字号:
<b>
invoke SetWindowText,hwndEdit,ADDR TestString</b> <br>
<b> .ELSEIF
ax==IDM_CLEAR</b> <br>
<b>
invoke SetWindowText,hwndEdit,NULL</b> <br>
<b> .ELSEIF
ax==IDM_GETTEXT</b> <br>
<b>
invoke GetWindowText,hwndEdit,ADDR buffer,512</b> <br>
<b>
invoke MessageBox,NULL,ADDR buffer,ADDR AppName,MB_OK</b> <br>
<b> .ELSE</b>
<br>
<b>
invoke DestroyWindow,hWnd</b> <br>
<b> .ENDIF</b>
<br>
<b> .ELSE</b> <br>
<b> .IF ax==ButtonID</b>
<br>
<b>
shr eax,16</b> <br>
<b>
.IF ax==BN_CLICKED</b> <br>
<b>
invoke SendMessage,hWnd,WM_COMMAND,IDM_GETTEXT,0</b> <br>
<b>
.ENDIF</b> <br>
<b> .ENDIF</b>
<br>
<b> .ENDIF</b> <br>
<b> .ELSE</b> <br>
<b> invoke DefWindowProc,hWnd,uMsg,wParam,lParam</b>
<br>
<b> ret</b> <br>
<b> .ENDIF</b> <br>
<b> xor eax,eax</b> <br>
<b> ret</b> <br>
<b>WndProc endp</b> <br>
<b>end start</b>
<h3> <b>Analysis:</b></h3>
Let's analyze the program.
<ul>
<b> .ELSEIF uMsg==WM_CREATE</b> <br>
<b> invoke CreateWindowEx,WS_EX_CLIENTEDGE,
\</b> <br>
<b>
ADDR EditClassName,NULL,\</b> <br>
<b>
WS_CHILD or WS_VISIBLE or WS_BORDER or ES_LEFT\</b> <br>
<b>
or ES_AUTOHSCROLL,\</b> <br>
<b>
50,35,200,25,hWnd,EditID,hInstance,NULL</b> <br>
<b> mov hwndEdit,eax</b> <br>
<b> invoke SetFocus, hwndEdit</b>
<br>
<b> invoke CreateWindowEx,NULL, ADDR
ButtonClassName,\</b> <br>
<b>
ADDR ButtonText,\</b> <br>
<b>
WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,\</b> <br>
<b>
75,70,140,25,hWnd,ButtonID,hInstance,NULL</b> <br>
<b> mov hwndButton,eax</b>
</ul>
We create the controls during processing of WM_CREATE message. We call CreateWindowEx
with an extra window style, WS_EX_CLIENTEDGE, which makes the client area look
sunken. The name of each control is a predefined one, "edit" for edit control,
"button" for button control. Next we specify the child window's styles. Each control
has extra styles in addition to the normal window styles. For example, the button
styles are prefixed with "BS_" for "button style", edit styles are prefixed with
"ES_" for "edit style". You have to look these styles up in a Win32 API reference.
Note that you put a control ID in place of the menu handle. This doesn't cause
any harm since a child window control cannot have a menu. <br>
After creating each control, we keep its handle in a variable for future use.
<br>
SetFocus is called to give input focus to the edit box so the user can type the
text into it immediately. <br>
Now comes the really exciting part. Every child window control sends notification
to its parent window with WM_COMMAND.
<p><b> .ELSEIF uMsg==WM_COMMAND</b> <br>
<b> mov eax,wParam</b> <br>
<b> .IF lParam==0</b>
<p>Recall that a menu also sends WM_COMMAND messages to notify the window about
its state too. How can you differentiate between WM_COMMAND messages originated
from a menu or a control? Below is the answer <br>
<center>
<table BORDER width="100%" >
<tr>
<td> </td>
<td>Low word of wParam</td>
<td>High word of wParam</td>
<td>lParam</td>
</tr>
<tr>
<td>Menu</td>
<td>Menu ID</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>Control</td>
<td>Control ID</td>
<td>Notification code</td>
<td>Child Window Handle</td>
</tr>
</table>
</center>
<p>You can see that you should check lParam. If it's zero, the current WM_COMMAND
message is from a menu. You cannot use wParam to differentiate between a menu
and a control since the menu ID and control ID may be identical and the notification
code may be zero.
<p><b> .IF ax==IDM_HELLO</b>
<br>
<b>
invoke SetWindowText,hwndEdit,ADDR TestString</b> <br>
<b> .ELSEIF
ax==IDM_CLEAR</b> <br>
<b>
invoke SetWindowText,hwndEdit,NULL</b> <br>
<b> .ELSEIF
ax==IDM_GETTEXT</b> <br>
<b>
invoke GetWindowText,hwndEdit,ADDR buffer,512</b> <br>
<b>
invoke MessageBox,NULL,ADDR buffer,ADDR AppName,MB_OK</b>
<p>You can put a text string into an edit box by calling SetWindowText. You clear
the content of an edit box by calling SetWindowText with NULL. SetWindowText
is a general purpose API function. You can use SetWindowText to change the caption
of a window or the text on a button. <br>
To get the text in an edit box, you use GetWindowText.
<p><b> .IF ax==ButtonID</b>
<br>
<b>
shr eax,16</b> <br>
<b>
.IF ax==BN_CLICKED</b> <br>
<b>
invoke SendMessage,hWnd,WM_COMMAND,IDM_GETTEXT,0</b> <br>
<b>
.ENDIF</b> <br>
<b> .ENDIF</b>
<p>The above code snippet deals with the condition when the user presses the button.
First, it checks the low word of wParam to see if the control ID matches that
of the button. If it is, it checks the high word of wParam to see if it is the
notification code BN_CLICKED which is sent when the button is clicked. <br>
The interesting part is after it's certain that the notification code is BN_CLICKED.
We want to get the text from the edit box and display it in a message box. We
can duplicate the code in the IDM_GETTEXT section above but it doesn't make
sense. If we can somehow send a WM_COMMAND message with the low word of wParam
containing the value IDM_GETTEXT to our own window procedure, we can avoid code
duplication and simplify our program. SendMessage function is the answer. This
function sends any message to any window with any wParam and lParam we want.
So instead of duplicating the code, we call SendMessage with the parent window
handle, WM_COMMAND, IDM_GETTEXT, and 0. This has identical effect to selecting
"Get Text" menu item from the menu. The window procedure doesn't perceive any
difference between the two. <br>
You should use this technique as much as possible to make your code more organized.
<br>
Last but not least, do not forget the TranslateMessage function in the message
loop. Since you must type in some text into the edit box, your program must
translate raw keyboard input into readable text. If you omit this function,
you will not be able to type anything into your edit box.
<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 + -