ICZelion TUT9

zhaozj2021-02-11  196

Tutorial 9: Child WINDOW Controls

In this Tutorial, We will Explore Child Window Controls Which Are Very Important Input and Output Devices of Our Program.

Download The Example Here.

Theory: 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 .

Examples of Predefined Window Classes Are Button, Listbox, Checkbox, Radio Button, Edit ETC.

In order to use a child window control, you must create it with CreateWindow or CreateWindowEx. Note that you do not 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 this control. You Use it to differentiate Between the controls.

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 handle in lParam window. Each child window control has different notification codes, refer to your Win32 API reference for more information.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 .

So, After Creating The Childows, The Parent Window Must Process WM_Command Messages To Be Able To Receive Notification Codes from the child windows.

Example: 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..:

Say Hello - Put A TEXT STRING INTO The Edit Box Clear Edit Box - Clear The Content of The Edit Box Get Text - Display a Message Box with The Text In The Edit Box EXIT - Close The Program.

.386

.Model flat, stdcall

Option CaseMAP: NONE

Winmain Proto: DWORD,: DWORD,: DWORD,: DWORD

Include /masm32/include/windows.inc include /masm32/include/user32.inc include /masm32/include/kernel32.inc includelib /masm32/lib/user32.lib incrudelib /masm32/lib/kernel32.lib

.data ClassName db "SimpleWinClass", 0 AppName db "Our First Window", 0 MenuName db "FirstMenu", 0 ButtonClassName db "button", 0 ButtonText db "My First Button", 0 EditClassName db "edit", 0 TestString db "WOW! I'M in an Edit Box Now", 0.data? Hinstance Hinstance? CommandLine LPSTR? HWNDBUTTON HWND? HWNDIT HWND? Buffer DB 512 DUP (?); Buffer to Store TEXT RETRIEVED from The Edit Box

.const buttonid EQU 1; the control id of the button control Editid EQU 2; The control id of the edit control idm_hello eQU 1 IDM_CLEAR EQU 2 IDM_GETTEXT EQU 3 IDM_EXIT EQU 4

. Code Start: Invoke GetModuleHandle, Null Mov Hinstance, Eax Invoke Getcommandline Mov Commandline, Eax Invoke Winmain, Hinstance, Null, CommandLine, SW_SHOWDEFAULT INVOKE EXITPROCESS, EAX

WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD LOCAL wc: WNDCLASSEX LOCAL msg: MSG LOCAL hwnd: HWND mov wc.cbSize, SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra, NULL mov wc.cbWndExtra, NULL push hInst pop wc.hInstance mov wc.hbrBackground, COLOR_BTNFACE 1 mov wc.lpszMenuName, OFFSET MenuName mov wc.lpszClassName, OFFSET ClassName invoke LoadIcon, NULL, IDI_APPLICATION mov wc. hIcon, eax mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_ARROW mov wc.hCursor, eax invoke RegisterClassEx, addr wc invoke CreateWindowEx, WS_EX_CLIENTEDGE, aDDR ClassName, / aDDR AppName, WS_OVERLAPPEDWINDOW, / CW_USEDEFAULT, CW_USEDEFAULT, / 300,200, NULL, NULL, HINST, NULL MOV HWND, EAX INVOKE ShowWindow, Hwnd, SW_SHOWNORMAL INVOKE UPDATEWINDOW, HWND .While True Invoke GetMessage, Addr MSG, NULL, 0, 0.Break .if (! EAX) Invoke TranslateMessage, Addr Msg Invoke DispatchMessage, Addr Msg .Endw Mov EAX, Msg.wParam Ret Winmain Endp

WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM .IF uMsg == WM_DESTROY invoke PostQuitMessage, NULL .ELSEIF uMsg == WM_CREATE invoke CreateWindowEx, WS_EX_CLIENTEDGE, ADDR EditClassName, NULL, / WS_CHILD or WS_VISIBLE or WS_BORDER or ES_LEFT or / ES_AUTOHSCROLL, / 50,35,200,25, hWnd, 8, hInstance, NULL mov hwndEdit, eax invoke SetFocus, hwndEdit invoke CreateWindowEx, NULL, ADDR ButtonClassName, ADDR ButtonText, / WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON, / 75,70,140, 25, hWnd, buttonID, hInstance, NULL mov hwndButton, eax .ELSEIF uMsg == WM_COMMAND mov eax, wParam .IF lParam == 0 .IF ax == IDM_HELLO invoke SetWindowText, hwndEdit, ADDR TestString .ELSEIF ax == IDM_CLEAR invoke SetWindowText , hwndite, null .elseif AX == idm_gettext invoke GetWindowText, hwndEdit, ADDR buffer, 512 invoke MessageBox, NULL, ADDR buffer, ADDR AppName, MB_OK .ELSE invoke DestroyWindow, hWnd .ENDIF .ELSE .IF ax == ButtonID shr eax, 16 .IF ax == BN_CLICKED invoke SendMessage, HWND, WM_COMMAND, IDM_GETTEXT, 0 .Endif.ndif.ndif.else Invoke DefWindowProc, Hwnd, UMSG, WPARAM, LPARAM RET WNDPROC ENDS, ENX RET WNDPROC ENDP End Startanalysis: Let's Analyze the Program.

.ELSEIF uMSG == WM_CREATE

Invoke CreateWindowex, WS_EX_CLIENTEDGE, / ADDR EditclassName, NULL, /

WS_CHILD or WS_VISIBLE OR WS_BORDER OR ES_LEFT /

OR ES_AUTOHSCROLL, /

50, 35, 200, 25, Hwnd, Editid, Hinstance, Null

Mov hwndedit, EAX

Invoke setfocus, hwndedit

Invoke CreateWindowex, Null, AddR ButtonClassName, /

AddR buttonText, /

WS_CHILD or WS_VISIBLE OR BS_DEFPUSHBUTTON, /

75, 70, 140, 25, hwnd, buttonid, hinstance, null

Mov hwndbutton, EAX

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.

After Creating Each Control, WE Keep ITS Handle IN A Variable for Future Use.

SetFOCUS Is Called to Give Input Focus to the Edit Box So The User CAN Type The Text Into It Immediately.

Now Comes The Really Exciting Part. EVERY CHILD WINDOW Control Sends Notification To Its Parent Window with WM_Command.

.ELSEIF uMSG == WM_Command Mov Eax, wparam .IF lparam == 0

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 answerLow word of wParamHigh word of wParamlParamMenuMenu ID00ControlControl IDNotification codeChild Window Handle

You can see that you should check lParam. If it's zero, the current WM_COMMAND message is from a menu. You can not 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.

.IF ax == IDM_HELLO invoke SetWindowText, hwndEdit, ADDR TestString .ELSEIF ax == IDM_CLEAR invoke SetWindowText, hwndEdit, NULL .ELSEIF ax == IDM_GETTEXT invoke GetWindowText, hwndEdit, ADDR buffer, 512 invoke MessageBox, NULL, ADDR buffer, ADDR AppName , MB_OK

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. To Get The Text In an Edit Box, you use getWindowText.

.IF AX == Buttonid SHR EAX, 16 .IF AX == BN_Clicked Invoke SendMessage, HWnd, WM_COMMAND, IDM_GETTEXT, 0 .endif .Endif.

转载请注明原文地址:https://www.9cbs.com/read-4686.html

New Post(0)