Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 11. To learn dialog

WIN32 compilation: 11. To learn dialog

Print View , by: iSee ,Total views: 14 ,Word Count: 2695 ,Date: Fri, 8 May 2009 Time: 3:35 PM

Section XI of the dialog box to learn


This lesson we will learn from the dialog box. In particular we are going to explore how the dialog box as an input device. If you learn a lesson, that you will find examples of this lesson is only a small number of changes, that is, the dialog window of our subsidiary to the main window. In addition, we also have to learn the use of common dialog box.

Theory:

The dialog box as an input device to use is very simple, created after the main window, you call the function as long as you can CreatedialogParam or DialogBoxParam, the former a function as long as the process in the dialog box function to deal with the related information can be , while the latter in the news cycle you have to insert a paragraph IsDialogMessage function call to deal with it the logic of the keyboard keys. Paragraph because the two procedures is relatively easy, we can not explain. You can download and careful study.
We are to discuss the following common dialog box. WINDOWS have been ready for your pre-defined category of the dialog box, you can use on the use of these common dialog boxes available to the user a uniform interface. They include: open the file, print, select the colors, fonts, and search. You should use them as much as possible. To deal with these dialog comdlg32.dll code, in order to in your application to use them, it is necessary to phase in the link-link library files comdlg32.lib. Then the correlation function call can be. Open File common dialog box for the function called GetOpenFileName, "save as ..." dialog box for GetSaveFileName, print common dialog box is PrintDlg, and so on. Each of these functions are to receive a pointer to a pointer structure of the parameters, you can refer to WIN32 API Manual of detailed information on this lesson I will create and use open file dialog box.
The following is a dialog box opens GetOpenFileName function prototype:

GetOpenFileName proto lpofn: DWORD
You can see that this function is only one parameter, that is, point the pointer OPENFILENAME structure. When the user selects a file and open, the function returns TRUE, otherwise return FALSE. Next, we take a look at the definition of the structure OPENFILENAME:

OPENFILENAME STRUCT
lStructSize DWORD?
hwndOwner HWND?
hInstance HINSTANCE?
lpstrFilter LPCSTR?
lpstrCustomFilter LPSTR?
nMaxCustFilter DWORD?
nFilterIndex DWORD?
lpstrFile LPSTR?
nMaxFile DWORD?
lpstrFileTitle LPSTR?
nMaxFileTitle DWORD?
lpstrInitialDir LPCSTR?
lpstrTitle LPCSTR?
Flags DWORD?
nFileOffset WORD?
nFileExtension WORD?
lpstrDefExt LPCSTR?
lCustData LPARAM?
lpfnHook DWORD?
lpTemplateName LPCSTR?
OPENFILENAME ENDS
Well, let us look at the structure of the members of the commonly used meanings:
lStructSize OPENFILENAME size of the structure.
hwndOwner Open the dialog box has a handle to the window.
hInstance Have the Open File dialog box applications instance handle.
lpstrFilter

To NULL at the end of one or more wildcards. Wildcard is emerging into the first part is a description of the latter part of the format is a wildcard, such as:
FilterString db "All Files (*.*)", 0, "*.*", 0
db "Text Files (*. txt)", 0, "*. txt", 0,0
Note: if each of the second part is used to filter WINDOWS select the required documents, and you must place in the after part of a 0 to indicate the end of the string.

nFilterIndex Open File dialog box used to specify the first to open when the filter pattern used in the index to start counting from 1, that is, a wildcard pattern of the first index is 1, the second is 2, such as the above example, If the designation of the value of 2, the default is to show the pattern of "*. txt".
lpstrFile Need to open the file name of the address, the name will appear in the Open File dialog box, edit control, the buffer can not be more than 260 characters long, when the user opens the file, the buffer contains the entire path of the document name, you can extract from the buffer zone you need the path or file name and other information.
nMaxFile lpstrFile size.
lpstrTitle Point to the title string dialog box.
Flags The decision marks the dialog style and characteristics.
nFileOffset The user opens a document after the value is the name of the entire path to the file name of the index of first character. For example: If the entire path name "c: \ windows \ system \ lz32.dll", then the value of 18.
nFileExtension The user opens a document after the value is the path name of the file extension to the first character of the index.

Examples:

The following example, we demonstrate that when the user selects "File-> Open" will pop up a dialog box to open the file, when the user chose to open a file, it will pop up a dialog box, and told to open the document 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.inc
include \ masm32 \ include \ comdlg32.inc
includelib \ masm32 \ lib \ user32.lib
includelib \ 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
buffer db MAXSIZE dup (0)
OurTitle db "-= Our First Open File Dialog Box =-: Choose the file to open", 0
FullPathName db "The Full Filename with Path is:", 0
FullName db "The Filename 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.lpstrTitle, OFFSET OurTitle
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 RtlZeroMemory, offset OutputString, OUTPUTSIZE
. endif
. else
invoke DestroyWindow, hWnd
. endif
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start


Analysis:

mov ofn.lStructSize, SIZEOF ofn
push hWnd
pop ofn.hwndOwner
push hInstance
pop ofn.hInstance

We are here to fill structure ofn variables relevant OPENFILENAME members.

mov ofn.lpstrFilter, OFFSET FilterString

Here is a paper filter model FilterString string address, filter mode, we specify string as follows:

FilterString db "All Files", 0 ,"*.*", 0
db "Text Files", 0, "*. txt", 0,0
Note: All model series are paired, the former one is the description of the latter model is the real, sub-Department "*.*" and "*. txt" is used to find matching WIONDOWS To open the document. We can specify any model can, but do not forget to add 0 at the end of the string to represent the end of the dialog box or you may be unstable in operation.

mov ofn.lpstrFile, OFFSET buffer
mov ofn.nMaxFile, MAXSIZE

Here is the address of the buffer on the structure, the size must be set at the same time. After editing, we can return in the buffer information.

mov ofn.Flags, OFN_FILEMUSTEXIST or \
OFN_PATHMUSTEXIST or OFN_LONGNAMES or \
OFN_EXPLORER or OFN_HIDEREADONLY

Flags in the dialog box Add style and value characteristics.
OFN_FILEMUSTEXIST and OFN_PATHMUSTEXIST which require users to open the Edit Control dialog box, type the file name or path name must exist.
Dialog box to tell OFN_LONGNAMES show long file names.
WINDOWS told OFN_EXPLORER the appearance of the dialog box to be similar to Explorer.
Do not show OFN_HIDEREADONLY designated read-only files (even if its extension in line with the filter mode).
In addition, there are many other flags, you can refer to the WIN32 API manual.

mov ofn.lpstrTitle, OFFSET OurTitle

Designated the title of the dialog box to open the file name.

invoke GetOpenFileName, ADDR ofn

GetOpenFileName function calls, and incoming point to the pointer structure ofn.
At this time, open the file dialog box out on the show, GetOpenFileName to function until the user selects a document until after the return, or when the user pressed the CANCEL button or close the dialog box appears.
When the user chose to open a file, the function returns TRUE, otherwise return 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 selects a file to open, we will display a dialog box in a string, we give the distribution of memory OutputString variables, and then PAI function call lstrcat, even all the strings together, in order to allow these string branch that we must back each string plus a newline.

mov eax, ofn.lpstrFile
push ebx
xor ebx, ebx
mov bx, ofn.nFileOffset
add eax, ebx
pop ebx
invoke lstrcat, offset OutputString, eax

A few lines above this may need some explanation. nFileOffset value of the document is opened in the entire path name of the file name of the first character of the index, as a result of nFileOffset is a WORD type variable, and lpstrFile is a DWORD pointer-shaped, so we have to make a conversion into the nFileOffset byte at the end of the ebx register, and then added to the register eax to be DWORD type indicator.

invoke MessageBox, hWnd, OFFSET OutputString, ADDR AppName, MB_OK

We in the dialog box to display the string.

invoke RtlZerolMemory, offset OutputString, OUTPUTSIZE

The next in order to correctly show that the buffer zone must be removed, we call function RtlZerolMemory to do this.



Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.