In this lesson we'll learn more about dialog boxes. In particular, we will explore how to treat a dialog box as an input device. If you have studied the previous lesson, you will find that there are only a few changes to the example in this lesson, that is, attaching our dialog window to the main window. In addition, we have to learn the use of common dialog boxes.
Theory:
Using a dialog box as an input device is really very simple, after you create the main window, you just call the function Createdialogparam or Dialogboxparam, and the previous function can only handle the relevant messages in the process function of the dialog box. And the latter you have to insert a function IsDialogMessage call in the message loop segment to handle the keyboard's key logic. Because these two sections are relatively easy, we have an unknown solution. You can download and study it carefully.
Let's discuss the common dialog box below. Windows has prepared a predefined dialog class for you, which you can use to provide the user with a unified interface. They include: open files, print, select colors, fonts, and search. You should use them as much as you can. The code that handles these dialog boxes is in Comdlg32.dll, and in order to use them in your application, you must link the library file Comdlg32.lib in the link phase. Then call the correlation function. For the Open File Common dialog box, the function is named GetOpenFileName, the "Save As ..." dialog box is GetSaveFileName, the Print Common dialog box is PrintDlg, and so on. Each of these functions receives an argument to a pointer to a struct, and you can refer to the WIN32 API manual for detailed information, which I will explain in this lesson about creating and using the Open File dialog box.
The following is the prototype that opens the dialog box function GetOpenFileName:
GetOpenFileName proto lpofn:DWORD
As you can see, the function has only one parameter, that is, a pointer to the openfilename of the struct body. When the user selects a file and opens it, the function returns True, otherwise it returns false. Next we 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's take a look at the meaning of the members that are commonly used in this structure:
The size of the openfilename of the lstructsize structural body.
hWndOwner has a handle to the window that opens the dialog box.
HInstance the instance handle of the application that owns the Open File dialog box.
Lpstrfilter one or more wildcard characters with a null ending. Wildcard characters appear in pairs, the first part is the description part, and the latter part is the format of the wildcard character, 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 one that Windows uses to filter the files you need to select, and you must place a 0 in that section to show the end of the string.
Nfilterindex is used to specify the filter mode string to use when opening the file dialog box for the first time. The index is calculated from 1, that is, the index of the first wildcard pattern is 1, and the second is 2, as in the example above, if you specify the value of 2, the default pattern string is "*.txt".
Lpstrfile the address of the name of the file you want to open, which will appear in the edit control in the Open File dialog box, which cannot exceed 260 characters, and when the user opens the file, the buffer contains the full pathname of the file. You can extract information such as the path or filename you need from the buffer.
The size of the nMaxFile lpstrfile.
Lpstrtitle a string that points to the title of the dialog box.
Flags This decision determines the style and characteristics of the dialog box.
Nfileoffset The value is the index to the first character of the filename in the full path name after the user opens a file. For example: If the full path name is "C:\windows\system\lz32.dll", the value is 18.
Nfileextension The value is the index of the full path name that points to the first character of the file name extension after the user has opened a file.