📄 lion-tutorial09.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 9: Child Window Controls</p>
<hr size="1">
In this tutorial, we will explore child window controls which are very important
input and output devices of our programs.
<p>Download the example <a href="files/tut09.zip">here</a>.
<h3> Theory:</h3>
Windows provides several predefined window classes which we can readily use in
our own programs. Most of the time we use them as components of a dialog box so
they're usually called child window controls. The child window controls process
their own mouse and keyboard messages and notify the parent window when their
states have changed. They relieve the burden from programmers enormously so you
should use them as much as possible. In this tutorial, I put them on a normal
window just to demonstrate how you can create and use them but in reality you
should put them in a dialog box. <br>
Examples of predefined window classes are button, listbox, checkbox, radio button,edit
etc. <br>
In order to use a child window control, you must create it with CreateWindow or
CreateWindowEx. Note that you don't have to register the window class since it's
registered for you by Windows. The class name parameter MUST be the predefined
class name. Say, if you want to create a button, you must specify "button" as
the class name in CreateWindowEx. The other parameters you must fill in are the
parent window handle and the control ID. The control ID must be unique among the
controls. The control ID is the ID of that control. You use it to differentiate
between the controls. <br>
After the control was created, it will send messages notifying the parent window
when its state has changed. Normally, you create the child windows during WM_CREATE
message of the parent window. The child window sends WM_COMMAND messages to the
parent window with its control ID in the low word of wParam, the notification
code in the high word of wParam, and its window handle in lParam. Each child window
control has different notification codes, refer to your Win32 API reference for
more information. <br>
The parent window can send commands to the child windows too, by calling SendMessage
function. SendMessage function sends the specified message with accompanying values
in wParam and lParam to the window specified by the window handle. It's an extremely
useful function since it can send messages to any window provided you know its
window handle. <br>
So, after creating the child windows, the parent window must process WM_COMMAND
messages to be able to receive notification codes from the child windows.
<h3> Example:</h3>
We will create a window which contains an edit control and a pushbutton. When
you click the button, a message box will appear showing the text you typed in
the edit box. There is also a menu with 4 menu items:
<ol>
<li> <b>Say Hello</b> -- Put a text string into the edit box</li>
<li> <b>Clear Edit Box</b> -- Clear the content of the edit box</li>
<li> <b>Get Text </b>-- Display a message box with the text in the edit box</li>
<li> <b>Exit </b>-- Close the program.</li>
</ol>
<b>.386</b> <br>
<b>.model flat,stdcall</b> <br>
<b>option casemap:none</b>
<p><b>WinMain 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>ClassName db "SimpleWinClass",0</b> <br>
<b>AppName db "Our First Window",0</b> <br>
<b>MenuName db "FirstMenu",0</b> <br>
<b>ButtonClassName db "button",0</b> <br>
<b>ButtonText db "My First Button",0</b> <br>
<b>EditClassName db "edit",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>hwndButton HWND ?</b> <br>
<b>hwndEdit HWND ?</b> <br>
<b>buffer db 512 dup(?)
; buffer to store the text retrieved from the edit box</b>
<p><b>.const</b> <br>
<b>ButtonID equ 1
; The control ID of the button control</b> <br>
<b>EditID equ 2
; The control ID of the edit control</b> <br>
<b>IDM_HELLO equ 1</b> <br>
<b>IDM_CLEAR equ 2</b> <br>
<b>IDM_GETTEXT equ 3</b> <br>
<b>IDM_EXIT equ 4</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_BTNFACE+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,
\</b> <br>
<b>
ADDR AppName, WS_OVERLAPPEDWINDOW,\</b> <br>
<b>
CW_USEDEFAULT, CW_USEDEFAULT,\</b> <br>
<b>
300,200,NULL,NULL, hInst,NULL</b> <br>
<b> mov hwnd,eax</b> <br>
<b> invoke ShowWindow, hwnd,SW_SHOWNORMAL</b> <br>
<b> invoke UpdateWindow, hwnd</b> <br>
<b> .WHILE TRUE</b> <br>
<b> invoke GetMessage, ADDR msg,NULL,0,0</b>
<br>
<b> .BREAK .IF (!eax)</b> <br>
<b> invoke TranslateMessage, ADDR
msg</b> <br>
<b> invoke DispatchMessage, ADDR msg</b>
<br>
<b> .ENDW</b> <br>
<b> mov eax,msg.wParam</b> <br>
<b> ret</b> <br>
<b>WinMain endp</b>
<p><b>WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</b> <br>
<b> .IF uMsg==WM_DESTROY</b> <br>
<b> invoke PostQuitMessage,NULL</b>
<br>
<b> .ELSEIF uMsg==WM_CREATE</b> <br>
<b> invoke CreateWindowEx,WS_EX_CLIENTEDGE,
ADDR EditClassName,NULL,\</b> <br>
<b>
WS_CHILD or WS_VISIBLE or WS_BORDER or ES_LEFT or\</b> <br>
<b>
ES_AUTOHSCROLL,\</b> <br>
<b>
50,35,200,25,hWnd,8,hInstance,NULL</b> <br>
<b> mov hwndEdit,eax</b> <br>
<b> invoke SetFocus, hwndEdit</b>
<br>
<b> invoke CreateWindowEx,NULL, ADDR
ButtonClassName,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> <br>
<b> .ELSEIF uMsg==WM_COMMAND</b> <br>
<b> mov eax,wParam</b> <br>
<b> .IF lParam==0</b> <br>
<b> .IF ax==IDM_HELLO</b>
<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -