Easily write dialog-based programs with compilation

zhaozj2021-02-16  62

Writing a dialog-based program can be used as clearly simple and simple as writing VB programs, and quite a number of programs are structures. The program is almost like this with VB.

Design program interface

Like the VC, the interface (dialog has various controls) is generally existing in the way. Editing the resource file can fully use the VC resource editor. But familiar with the writing and significance of resource documents is necessary. Below is an example of a resource file:

#include "/masm32/include/resource.h" #define IDC_EDIT 3000 #define IDC_BUTTON 3001 #define IDC_EXIT 3002 MyDialog DIALOG 10, 10, 205, 60 STYLE 0x0004 | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK CAPTION "My Dialog" {EditText IDC_EDIT, 15, 17, 111, 13, ES_AUTOHSCROLL | ES_LEFT DEFPUSHBUTTON "Display the content in the editing box", IDC_Button, 141, 10, 52, 13 pushbutton "exit program", IDC_EXIT, 141, 26, 52, 13}

Remember, in order to make compilation successfully, you must save it as RSRC.RC this file name (it doesn't matter if you use the command line). Choose Project-> Compile Resource File. If the resource file is successful, it can be used to be used. It is recommended that you open this resource file with the VC resource editor.

The first line of #include "/masm32/include/resource.h" is like C, which is a header file. There are various constants in this file, such as Pushbuotton, etc., if you are interested, you can open this file. Look.

Some constants are defined next, just like the usage in C, is to enhance the readability of the program.

"MyDialog Dialog 10, 10, 205, 60" This sentence uses Dialog this keyword to define a dialog named MyDialog, which is of course casual. The following four numbers are the location size of the dialog, usually debugging in the resource editor.

"Style 0x0004 | DS_CENTER | WS_CAPTION | WS_MINIMIMIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK" is a style describing the dialog. If WS_MINIMIZEBOX is, there is a minimization button. DS_3dlook is that the dialog is 3D shape, etc. These generally change in the resource editor, the meaning of various constants, can be found in the MSDN.

CAPTION "My Dialog" is the title of the specified dialog is "My Dialog"

Then use the {} box below to the respective controls in the dialog. This example uses the EditText editing box control, the PushButton button control, the Defpushbutton default button control (default selected status), IDC_EDIT, IDC_EXIT, IDC_BUTTON are the 3 ID numbers, pay attention to the above definition, is not the same. It is not necessary to use #define to pre-define it, and the use of numbers can be used, and the former can enhance the readability of the program. Other uses of many controls such as radical boxes, check boxes, etc. You can read the source code after editing under the resource editor.

The program's interface is designed, and next is the design program. Program design

Let's do a simple program. When you press the "Content" button in the Edit box, you will pop up a message box to display the contents of the current edit box. Press the "Exit" button will exit the program.

.386 .Model flat, Stdcall Option Casemap: NONE

DLGPROC 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 DLGNAME DB "MyDialog", 0 AppName DB "My Own Dialog", 0

.DATA? HINSTANCE HINSTANCE? BUFFER DB 512 DUP (?)

.const IDC_EDit EQU 3000 IDC_Button EQU 3001 IDC_EXIT EQU 3002

. Code Start: Invoke GetModuleHandle, Null Mov Hinstance, Eax Invoke Dialogboxparam, Hinstance, Addr Dlgname, Null, Addr DlgProc, Null Invoke EXITPROCESS, EAX

DlgProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM .IF uMsg == WM_INITDIALOG invoke GetDlgItem, hWnd, IDC_EDIT invoke SetFocus, eax .ELSEIF uMsg == WM_CLOSE invoke EndDialog, hWnd, NULL .ELSEIF uMsg == WM_COMMAND mov eax, wParam .IF eax == IDC_BUTTON invoke GetDlgItemText, hWnd, IDC_EDIT, ADDR buffer, 512 invoke MessageBox, NULL, ADDR buffer, ADDR AppName, MB_OK .ELSEIF ax == IDC_EXIT invoke EndDialog, hWnd, NULL .ENDIF .ELSE Mov Eax, False Ret .endif Mov Eax, True Ret DLGPROC ENDS End Start

When I look at it, this program is very long. If I use VB to implement this function, I only need to write 5-6 lines. However, a considerable part of the code in this program is fixed, that is, writing any dialog-based programs need to be written. VB is a VB compiler to automatically make these work.

.386; Declaration uses 386 instruction system. Model flat, stdcall; declaration of memory usmap: none; declaration is sensitive to case sensitive

The above 3 lines can be considered fixed, must

DLGPROC Proto: DWORD,: DWORD,: DWORD,: DWORD; ........ Declare your defined function, the above is the main function of the dialog (must be), the name can be replaced Other

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 dlgname DB "MyDialog", 0 Appname DB "My OWN DIALOG", 0; ............ Here, each pre-assigned variable is defined, DB is defined by one byte length, DW is defined A word length, etc., there are many types of macro, which are included in Windows.h.

.data? Hinstance Hinstance?; .......... Here, each variable is defined, but there is no pre-value

.const; ........ If you need to predefix some constants, put it in this section, use keyword EQU definitions

.code; The following is the code segment, pay attention to the function is also placed in the code section! START :; Not necessarily use of start, you can be any string such as Begin, Entry, etc. Invoke GetModuleHandle, Null Mov Hinstance, EAX Invoke Dialogboxparam, Hinstance, Addr Dlgname, Null, Addr DlgProc, Null Invoke EXITPROCESS, EAX

The above lines are also fixed, invoke is used to call the function, people who have used VB know the call, which is equivalent to that command

DLGPROC PROC HWND: HWND, UMSG: UINT, WPARAM: WPARAM, LPARAM: LPARAM; .................. This is where you focus mainly

DLGPROC ENDP End Start; Main and START

Basically, you can use the green part of the above as a template, just add the variables and constants you need in .data and .data? And .const segments. The rest of the task is to write the above red, that is, to deal with each message.

Let's analyze a few messages. After calling the Dialogboxprarm created the dialog, the dialog will receive the WM_INITDIALOGE dialog initialization message, and the UMSGZ parameter accepts the message. In this program, we use the getDlgitem function to get the handle of the edit box control, then call SetFocus to set the focus on it. Note that these functions are API functions, which can be found in the MSDN.

When the shutdown button in the upper right corner of the dialog is generated, it is generated to handle this function, otherwise it is not possible to close the dialog box (this is due to the built-in function without designing this message), and for maximum minimization, etc. The message has default processing.

WM_COMMAND messages are generated when the button on the dialog is pressed, and the WPARAM is passed is the ID of the control. Different functions according to different IDs, in this case, if it is an IDC_BUTTON this ID, the control button that displays the edit box content is pressed, so that the getDlgiteMtext function gets the contents of the edit box, then use MessageBox to display. If you are IDC_EXIT description Press the "Exit Program" button, execute the endDialog this function exit program. The program is to complete various messages! In larger programs are also this knot structure, but only the news is more.

My home page ASM.91I.NET

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

New Post(0)