Win32 assembly: 11. Further study dialog

zhaozj2021-02-17  56

Eleventh lesson further learning dialog

In this lesson, we will further study dialog boxes. In particular, we will explore how to use the dialog as an input device. If you have learned the previous lesson, you will find that there is only a small amount of change in the example. It is to attach our dialog window to the main window. In addition, we must learn the usage of common dialogs.

Theory: Use the dialog as an input device to use it simply. After you create a full-stricken window, you only need to call the function createDialogparam or DialogBoxParam, the previous function will handle related messages in the process handler of the dialog. You can, and the latter you must insert a function isDialogMessage in the message loop to make it handle the keyboard of the keyboard. Because these two blocks are relatively easy, we are not detailed. You can download and take care.

Let's discuss the general dialog box. Windows has prepared a predefined dialog class, and you can use it, and these general dialogs are provided to the user with a unified interface. They include: Open files, print, select color, fonts, and search, etc. You should use them as much as possible. Handling the code for these dialogs in COMDLG32.DLL, in order to use them in your application, you must link the library file COMDLG32.LIB in the link phase. Then call the related functions. For open file common dialogs, the function is called getopenfilename, "Save as ..." dialog as getSaveFileName, printing the general dialog is PrintDLG, and so on. Each such a function receives a parameter to a pointer to a structure, you can refer to the Win32 API manual to get a detailed information, this lesson I will explain the creation and use Open File dialog box.

Below is the prototype of the open dialog function getopenfilename:

GetopenFileName Proto LPOFN: DWORD

You can see that this function has only one parameter, that is, pointing to the pointer to the structure OpenFileName. When the user selects a file and opens, the function returns true, otherwise returns false. Next, let's take a look at the definition of the structure of OpenFileName:

OpenFileName Struct

LSTRUCTSIZE DWORD?

HWNDOWNER HWND?

Hinstance Hinstance?

LPSTRFILTER LPCSTR?

LPSTRCUSTOMFILTER LPSTR?

Nmaxcustfilter DWORD?

NFILTERINDEX DWORD?

LPSTRFILE LPSTR?

NMAXFILE DWORD?

LPSTRFILETIL LPSTR?

NMAXFILETITLE DWORD?

LPSTRINTIALDIR LPCSTR?

LPSTRTILE LPCSTR?

Flags DWORD?

NFILEOFFSET WORD?

NFILEEXTENSION WORD?

LPSTRDEFEXT LPCSTR?

LCUSTDATA LPARAM?

LPFNHOOK DWORD?

LPTEMPLATENAME LPCSTR?

OpenFileName Ends

Ok, let's take a look at the meaning of members commonly used in the structure:

The size of the LSTRUCTSIZE structure OpenFileName. HWndowner has a handle of the window that opens the dialog. Hinstance has an instance handle of the application that opens the file dialog. LPSTRFILTER is one or more wildcards ending with NULL. The wildcard is paired, the previous portion is a description, and the latter part is a format of wildcard, such as Filterstring DB "all files (*. *), 0," *. * ", 0 dB" text files (* .txt ", 0," *. txt ", 0, 0 Note: Only the second part of each pair is the file used to filter the desired selection, and you must place a 0 after this section. Take the end of the string. NFILTERINDEX is used to specify the filter mode string used when the file dialog is opened for the first time. The index is calculated from 1, that is, the index of the first wildcard mode is 1, the second is 2, such as the above example. If this value is specified 2, the default display is "* .txt". LPSTRFILE needs to open the name of the name of the file, which will appear in the editing control of the open file dialog box, the buffer cannot exceed 260 characters long, and when the user opens the file, the buffer contains the full Path name, you can extract information such as the path or file name you need from this buffer. NMAXFILELPSTRFILE size. LPSTRTITLE points to the string of the dialog header. Flags This flag decides to determine the style and characteristics of the dialog. NFILEOFFSET This value is the index of the first character of the file name in full path name after the user opens a file. For example, if the full path is named "c: /windows/system/lz32.dll", the value is 18. NfileExtension This value is the index of the first character in the full path name in the full path name in the full path name. Example: In the following example, we demonstrate an open file dialog box when the user selects "file-> open". When the user selects a file to open, a dialog box will tell the file to open. The full path name, file name and file extension.

.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. Include /masm32/include/comdlg32.incinc includelib /masm32/lib/user32.lib incrudelib /masm32/lib/kernel32.lib includeliB /masm32/lib/comdlg32.lib

.const IDM_Open EQU 1 IDM_EXIT EQU 2 MaxSize EQU 260 OutputSize EQU 512

.DATA CLASSNAME DB "SimpleWinclass", 0 Appname DB "Our main window", 0 menuname DB "firstmenu", 0 OFN OpenFileName <> Filterstring DB "All Files", 0, "*. *", 0 DB "Text Files" , 0, "*. Txt", 0, 0) OURTITLE DB "- = OUR First Open file Dialog box = -: chourt the file to open", 0 fullpathname db "The Full FileName with path IS : ", 0 fullname is:", 0 extensionname db "The extension IS:", 0 OutputString DB OutputSize DUP (0) CRLF DB 0DH, 0AH, 0.Data? Hinstance Hinstance? Commandline LPSTR?

. 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_WINDOW 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_COMMAND mov eax, wParam .if ax == IDM_OPEN mov ofn.lStructSize, SIZEOF ofn push hWnd pop ofn.hwndOwner push hInstance pop ofn.hInstance mov ofn.lpstrFilter, OFFSET FilterString mov ofn.lpstrFile, OFFSET buffer mov ofn.nMaxFile, MAXSIZE mov ofn.Flags, OFN_FILEMUSTEXIST or / OFN_PATHMUSTEXIST or OFN_LONGNAMES or / OFN_EXPLORER or OFN_HIDEREADONLY mov ofn .lpstitle, Offset OURTITE Invoke GetopenFileName, Addr OFN .IF EAX ==

TRUE invoke lstrcat, offset OutputString, OFFSET FullPathName invoke lstrcat, offset OutputString, ofn.lpstrFile invoke lstrcat, offset OutputString, offset CrLf invoke lstrcat, offset OutputString, offset FullName mov eax, ofn.lpstrFile push ebx xor ebx, ebx mov bx, ofn .nFileOffset add eax, ebx pop ebx invoke lstrcat, offset OutputString, eax invoke lstrcat, offset OutputString, offset CrLf invoke lstrcat, offset OutputString, offset ExtensionName mov eax, ofn.lpstrFile push ebx xor ebx, ebx mov bx, ofn.nFileExtension add Eax, EBX POP EBX INVOKE LSTRCAT, OFFSET OUTPUTSTRING, EAX INVOKE Messagebox, Hwnd, Offset OutputString, Addr Appname, MB_OK Invoke RTLZEROME Mory, Offset OutputString, OutputSize .ndif.lse Invoke DestroyWindow, Hwnd .ndif.lse Invoke DefWindowProc, HWND, UMSG, WPARAM, LPARAM RET WNDPROC ENDP END Start Analysis:

Mov ofn.lstructsize, Sizeof off

Push hwnd

POP OFN.HWNDOWNER

Push hinstance

POP OFN.HINSTANCE

We fill in the article OpenFileName variable OFN related members.

Mov ofn.lpstrfilter, Offset Filterstring

Here Filterstring is a string address of the file filter mode, and we specified filter mode strings are as follows:

Filterstring DB "All Files", 0, "*. *", 0

DB "Text Files", 0, "*. TXT", 0, 0

Note: All mode strings are paired, the previous one is a description, the latter one is the real mode, the next "*. *" And "* .txt" are the file used to find the match to open. We can specify any modes, but don't forget to add 0 at the end to represent the string, otherwise your dialog may be unstable during operation. Mov off.lpstrfile, offset buffer mov offsize, maxsize

Here is the address of the buffer to be placed in the structure and must be set. We will then edit the information returned in the buffer.

Mov offsflags, OFN_FILEMUSTEXIST OR / OFN_PATHMUSTEXIST OR OFN_LONGNAMES OR / OFN_EXPLORER OR OFN_HIDEREADONLY

The style and characteristic value of the dialog is placed in the Flags. Where OFN_FILEMUSTEXIST and OFN_PATHMUSTEXIST require the user that the user entered in the editing control of the dialog box must exist. The OFN_LONGNAMES tells the dialog to display long file names. The appearance of OFN_EXPLORER tells the Windows dialog requires similar resource manager. Ofn_hideReadOnly Specifies not to display read-only files (both make it in line with filtering mode). In addition, there are many other logo, you can refer to the Win32 API manual.

Mov off.lpstitle, Offset OURTITLE

Specifies the title name of the open file dialog.

Invoke GetopenFileName, Addr OFN

Call the GetopenFileName function and pass to the pointer to the structure of the structure. At this time, the open file dialog is displayed, the getopenfilename function will continue until the user selects a file, or when the user presses the Cancel key or shut down the dialog. When the user selects open a file, the function returns true, otherwise returns false.

.if eax == TRUE invoke lstrcat, offset OutputString, OFFSET FullPathName invoke lstrcat, offset OutputString, ofn.lpstrFile invoke lstrcat, offset OutputString, offset CrLf invoke lstrcat, offset OutputString, offset FullName

When the user chooses to open a file, we display a string in a dialog. Let's assign memory to the OutputString variable, then call the PAI function lstrcat, connect all the strings together, to let these string branches We must add a newline in every string.

MOV EAX, OFN.LPSTRFILE PUSH EBX XOR EBX, EBX MOV BX, OFN.NFILEOFFSET Add Eax, EBX POP EBX INVOKE LSTRCAT, OFFSET OUTPUTSTRING, EAX

There may be some explanations above these lines. NfileOffset's value is equal to the index of the first character of the file name in the full path name being opened, because NfileOffset is a Word type variable, and lpstrfile is a DWORD-shaped pointer, so we have to convert NFILEOFFSET Enter the bottom byte of the EBX register and then add the DWORD type pointer to the EAX register. Invoke Messagebox, HWnd, Offset OutputString, Addr Appname, MB_OK

We display the string in the dialog box.

Invoke RTLZEROLMEMORY, OFFSet OutputString, OutputSize

In order to display the next time, the buffer must be cleared, we call the function RTLZEROLMEMORY to do this.

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

New Post(0)