Monday, July 12, 2010

Dialog box for further study



We will further study this course in the dialog box. In particular, we will explore how the dialog box as the input device. If you learn a lesson, it will find an example of this lesson is only a small amount of change, that is, the dialog window of our subsidiary to the main window. In addition, we also study the use of common dialog boxes.

Theory:

The dialog box as an input device to use is really very simple, you create the main window, you just call the function CreatedialogParam or DialogBoxParam on it, before a function so long as the process in the dialog box handler can handle the message , which you must insert a paragraph in the message loop function IsDialogMessage call it logic to handle the keyboard keys. Because these two are relatively easy to program segment, we would not Xiangjie. You can download and study carefully.

Here we discuss the common dialog box. WINDOWS have been ready for your pre-defined dialog box class, you can put on the use of these common dialog boxes available to users with a unified interface. They include: open the file, print, choose colors, fonts, and searching. You should try to use them. To deal with these dialog code comdlg32.dll, in order to in your application to use them, you must link library files in the link stage comdlg32.lib. Then you can call one of the correlation function. Open File common dialog box for the function called GetOpenFileName, "Save as ..." dialog box for the GetSaveFileName, print common dialog box is PrintDlg, etc.. Each of these functions takes a pointer to point to a structure parameter, you can refer to the WIN32 API manual for detailed information, this lesson I will explain the creation and use of open file dialog box.
Here is the prototype of the Open dialog box function GetOpenFileName:

GetOpenFileName proto lpofn: DWORD
You can see that the function has only one parameter, that is, a pointer pointing to OPENFILENAME structure. When the user selects a file and open, the function returns TRUE, otherwise returns FALSE. Next we look at the definition of 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 in the common meaning of members:

lStructSize structure OPENFILENAME size.
hwndOwner Open dialog box has a handle of the window.
hInstance has the file open dialog application instance handle.
lpstrFilter to NULL at the end of one or more wildcard. Wildcards are in pairs, the first part of description, the last part is the wildcard format, 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 required to filter WINDOWS selected files, and the other after the part you must place a 0 to indicate the end of the string.

nFilterIndex open file dialog box to specify when the first open string with the filter, the index is counted from a beginning, that is a wildcard pattern of the index is 1, the second is 2, for example, the example above If specified the value of 2, then the default display mode is string "*. 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 exceed 260 characters long, when the user opens the file, the buffer contains all the files path name, you can extract from the buffer you need a path or file name and other information.
nMaxFile lpstrFile size.
lpstrTitle point dialog title string.
Flags mark the decision of the dialog box determines the style and features.
nFileOffset the user opens a file, the value is the full path name of the file name points to the index of first character. For example: If the full path name "c: windowssystemlz32.dll", then the value is 18.
nFileExtension the user opens a file, the value is the full path name pointing to a file extension of the first character of the index.


Examples:
The following example, we have demonstrated that when the user selects "File-> Open", it will pop up a dialog box to open the file, when the user selects a file to open, it will pop up a dialog box, told to open the file full path name, file name and file extension.
.386
. Model flat, stdcall
option casemap: none
WinMain proto: DWORD,: DWORD,: DWORD,: DWORD
include masm32includewindows.inc
include masm32includeuser32.inc
include masm32includekernel32.inc
include masm32includecomdlg32.inc
includelib masm32libuser32.lib
includelib masm32libkernel32.lib
includelib masm32libcomdlg32.lib

. Const
IDM_OPEN equ 1
IDM_EX99v 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 of the members of the OPENFILENAME ofn variables.

mov ofn.lpstrFilter, OFFSET FilterString

Here FilterString the file filter string address, we specify the filter string is as follows:

FilterString db "All Files", 0 ,"*.*", 0
db "Text Files", 0, "*. txt", 0,0
Note: All model series are paired, one a description of the latter is the real model, sub-office "*.*" and "*. txt" is used to find matching WIONDOWS want to open documents. When can we can specify any mode, but do not forget to add 0 to represent the end of the string has ended, or your operation may be unstable in the dialog box.
mov ofn.lpstrFile, OFFSET buffer
mov ofn.nMaxFile, MAXSIZE

Here is the address of the buffer into the structure, the size must be set. Since we are free to edit the information returned in the buffer.

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

Flags in the dialog box into the style and property value.
One OFN_FILEMUSTEXIST and OFN_PATHMUSTEXIST require the user to the edit control in the Open dialog box, enter the file name or path name must exist.
OFN_LONGNAMES tell the dialog box display long file names.
OFN_EXPLORER told WINDOWS dialog must look like Explorer.
OFN_HIDEREADONLY designated read-only files do not show (even if it is the extension of the filter mode).
In addition, there are many other flags, you can refer to the WIN32 API manual.

mov ofn.lpstrTitle, OFFSET OurTitle

Open File dialog box specify the title name.

invoke GetOpenFileName, ADDR ofn

GetOpenFileName function call, and pass a pointer to point to structure ofn.
At this time, open the file dialog box that come, GetOpenFileName function to wait until after the user selects a file will be returned, or when the user presses the CANCEL button or close the dialog box.
When the user selects to 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 selects to open a file, we will display a dialog box in a string, we first give OutputString variables to allocate memory, then call the PAI function lstrcat, with all the strings together, in order for these strings branches shows that we must add a line break the back of each string.

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

Above these lines may require some explanation. nFileOffset value equal to the full path name to open the file in the file name's first character in the index, as nFileOffset is a WORD type variable, and lpstrFile is a DWORD pointer shape, so we will have to deposit a conversion to nFileOffset the bottom byte ebx register, and then add to the eax register to get a pointer type DWORD.

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

We are in the dialog box display the string.

invoke RtlZerolMemory, offset OutputString, OUTPUTSIZE

In order to correctly display the next time, we must clear the buffer, we call the function RtlZerolMemory to do it.







Recommand Link:



matroska VIDEO file



Simple Telephony



Youtube To Mobile Phone Products



.mkv FILES



My favorite Seasonal - Screen Savers



Youtube FLV To Psion Shareware



CABLE TV On PC



SuperAVConverter



Premier Genealogy



Youtube Video to Laptops Mixer



mp4 3gp video Converter



Games Sports Report



Christmas-Idea DVD to PS3 PDA



CHRISTMASGIFT RMVB to DVD



flac to mp3



Bluesea XviD Convert



X-Cloner Apple TV Converter



No comments:

Post a Comment