Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 9. Sub-window control

WIN32 compilation: 9. Sub-window control

Print View , by: iSee ,Total views: 19 ,Word Count: 4398 ,Date: Fri, 8 May 2009 Time: 8:35 PM

The ninth child window control class


This lesson we will explore the controls, these controls is the major process input and output devices.

Theory:

WINDOWS provides several predefined window class for our use. Most of the time, when we put them in dialog box, so we called the general on their sub-window control. Sub-window control will be to handle the news and changes in their own state to notify the parent window. This will greatly reduce the work of our programming, so we should use them as much as possible. This lesson we put these controls on the window in order to simplify the procedure, but most of the time sub-window controls are on the dialog box. Example, we demonstrate control of the sub-window include: buttons, pull-down menus, check boxes, radio buttons, edit boxes and so on. Control the use of sub-window, the first call to CreateWindow or CreateWindowEx. WINDOWS here have been registered as a result of these sub-controls, so no need for us to re-register. Of course, we can not change their names. For example: If you would like to create a button to call the two functions must be specified category called "button". Other parameters must be specified in the parent window also handles and controls to produce a subset of the ID. Control sub-ID number is used to identify sub-controls, it must also be unique. After the sub-controls, when its status changes will send a message to the parent window. General we should be the father of a window in the WM_CREATE message generated control word. Son to the father of a window controls the message is sent WM_COMMAND, and transmission parameters at the end of the wPara include digital control number ID, message in wParam high, lParam included in the sub-window control handles. There are different types of control message code set, please see the WIN32 API reference manual. Parent window can also call the SendMessage function to send messages to the sub-controls, in which the first control parameter is the son of the window handle, the second parameter is the number to send the message that the additional parameters can be in the wParam and lParam passed, in fact, as long as know the handle of a window can use this function to send the relevant information. So have had a sub-window must be addressed in order to receive WM_COMMAND messages to the news of sub-controls.

Examples:

We will have a window in the window there is a edit box and a button. When you press the button, it will pop up a dialog box shows you the contents of the Edit box. In addition, the application also has a menu, of which there are four menu items:
  1. Say Hello - a string edit control input;
  2. Clear Edit Box - Clear the string edit control;
  3. Get Text - pop-up dialog box display the string in edit control;
  4. Exit - exit the application.
.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
includelib \ 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?
hwndEdit HWND?
buffer db 512 dup (?); buffer to store the 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, hwndEdit, 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
. ENDIF
. ENDIF
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start

Analysis:

We shall now begin the analysis,
    . 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 have a son in WM_CREATE control, which in the CreateWindowEx function to the sub-controls in a window WS_EX_CLIENTEDGE style, it makes the window appear to border control sub-concave, with three-dimensional sense. Control of each sub-class name is predefined, for example: the button is pre-defined class name "button", edit box is the "edit". The next parameter is the window style, in addition to the usual style of the window, each one has its own extension of the control style, such as: the expansion of the style category button in front of additions BS_, edit box type is: ES_, WIN32 API reference in the expansion of all description of the style. Note: CreateWindowsEx function you have to pass the menu handle to the window where the space into sub-ID number will not be any side effects, because the sub-window can not have a menu control. Have control, we saved their handler, and then call SetFocus to set focus to the edit control so that users can enter immediately. The next is how to deal with control of the notification message to send WM_COMMAND:

. ELSEIF uMsg == WM_COMMAND
mov eax, wParam
. IF lParam == 0

Select the menu said we would like to also send WM_COMMAND message, what should we distinguish between them? The table below you will read at a glance:

Low word of wParam High word of wParam lParam
Menu Menu ID 0 0
Control Control ID Notification code Child Window Handle

Which we can see that can not be used to distinguish wParam, because menus and controls may be the same ID number, and the news of sub-window space may also be for No. 0.

. 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 call the SetWindowText function to a string of harassment to the edit control, in order to ching 0, imported NULL value. SetWindowText is a general function, that is, can use it to set a window title, as well as can use it to change the text of a button. If it is necessary to get the text on the button, then call GetWindowText.

. IF ax == ButtonID
shr eax, 16
. IF ax == BN_CLICKED
invoke SendMessage, hWnd, WM_COMMAND, IDM_GETTEXT, 0
. ENDIF
. ENDIF

Fragment of the above buttons to deal with user events. He first checks the high byte of wParam to see if the button's ID No., if the low byte check messages sent to see whether its BN_CLICKED, the message is sent when the button press, and if everything is right, then turn to deal with the message, we can deal with news from the IDM_GETTEXT Department to copy the whole code, but a more professional approach is to send a message IDM_GETTEXT the process so that the main window, it should be set to send the message WM_COMMAND, then the low byte of wParam can be set to IDM_GETTEXT. In this way your code on a number of concise, so as far as possible, make use of the skills. Finally, of course not or not, we must call in the news cycle function TranslateMessage, because your application needs in the edit box enter the text readable. If you omit this function, it can not be in the edit box, enter anything.

The ninth child window control class


This lesson we will explore the controls, these controls is the major process input and output devices.

Theory:

WINDOWS provides several predefined window class for our use. Most of the time, when we put them in dialog box, so we called the general on their sub-window control. Sub-window control will be to handle the news and changes in their own state to notify the parent window. This will greatly reduce the work of our programming, so we should use them as much as possible. This lesson we put these controls on the window in order to simplify the procedure, but most of the time sub-window controls are on the dialog box. Example, we demonstrate control of the sub-window include: buttons, pull-down menus, check boxes, radio buttons, edit boxes and so on. Control the use of sub-window, the first call to CreateWindow or CreateWindowEx. WINDOWS here have been registered as a result of these sub-controls, so no need for us to re-register. Of course, we can not change their names. For example: If you would like to create a button to call the two functions must be specified category called "button". Other parameters must be specified in the parent window also handles and controls to produce a subset of the ID. Control sub-ID number is used to identify sub-controls, it must also be unique. After the sub-controls, when its status changes will send a message to the parent window. General we should be the father of a window in the WM_CREATE message generated control word. Son to the father of a window controls the message is sent WM_COMMAND, and transmission parameters at the end of the wPara include digital control number ID, message in wParam high, lParam included in the sub-window control handles. There are different types of control message code set, please see the WIN32 API reference manual. Parent window can also call the SendMessage function to send messages to the sub-controls, in which the first control parameter is the son of the window handle, the second parameter is the number to send the message that the additional parameters can be in the wParam and lParam passed, in fact, as long as know the handle of a window can use this function to send the relevant information. So have had a sub-window must be addressed in order to receive WM_COMMAND messages to the news of sub-controls.

Examples:

We will have a window in the window there is a edit box and a button. When you press the button, it will pop up a dialog box shows you the contents of the Edit box. In addition, the application also has a menu, of which there are four menu items:

  1. Say Hello - a string edit control input;
  2. Clear Edit Box - Clear the string edit control;
  3. Get Text - pop-up dialog box display the string in edit control;
  4. Exit - exit the application.

.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
includelib \ 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?
hwndEdit HWND?
buffer db 512 dup (?); buffer to store the 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, hwndEdit, 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
. ENDIF
. ENDIF
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start

Analysis:

We shall now begin the analysis,

    . 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 have a son in WM_CREATE control, which in the CreateWindowEx function to the sub-controls in a window WS_EX_CLIENTEDGE style, it makes the window appear to border control sub-concave, with three-dimensional sense. Control of each sub-class name is predefined, for example: the button is pre-defined class name "button", edit box is the "edit". The next parameter is the window style, in addition to the usual style of the window, each one has its own extension of the control style, such as: the expansion of the style category button in front of additions BS_, edit box type is: ES_, WIN32 API reference in the expansion of all description of the style. Note: CreateWindowsEx function you have to pass the menu handle to the window where the space into sub-ID number will not be any side effects, because the sub-window can not have a menu control. Have control, we saved their handler, and then call SetFocus to set focus to the edit control so that users can enter immediately. The next is how to deal with control of the notification message to send WM_COMMAND:

. ELSEIF uMsg == WM_COMMAND
mov eax, wParam
. IF lParam == 0

Select the menu said we would like to also send WM_COMMAND message, what should we distinguish between them? The table below you will read at a glance:

Low word of wParam High word of wParam lParam
Menu Menu ID 0 0
Control Control ID Notification code Child Window Handle

Which we can see that can not be used to distinguish wParam, because menus and controls may be the same ID number, and the news of sub-window space may also be for No. 0.

. 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 call the SetWindowText function to a string of harassment to the edit control, in order to ching 0, imported NULL value. SetWindowText is a general function, that is, can use it to set a window title, as well as can use it to change the text of a button. If it is necessary to get the text on the button, then call GetWindowText.

. IF ax == ButtonID
shr eax, 16
. IF ax == BN_CLICKED
invoke SendMessage, hWnd, WM_COMMAND, IDM_GETTEXT, 0
. ENDIF
. ENDIF

Fragment of the above buttons to deal with user events. He first checks the high byte of wParam to see if the button's ID No., if the low byte check messages sent to see whether its BN_CLICKED, the message is sent when the button press, and if everything is right, then turn to deal with the message, we can deal with news from the IDM_GETTEXT Department to copy the whole code, but a more professional approach is to send a message IDM_GETTEXT the process so that the main window, it should be set to send the message WM_COMMAND, then the low byte of wParam can be set to IDM_GETTEXT. In this way your code on a number of concise, so as far as possible, make use of the skills. Finally, of course not or not, we must call in the news cycle function TranslateMessage, because your application needs in the edit box enter the text readable. If you omit this function, it can not be in the edit box, enter anything.



Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.