Generate a Windows2000 file dialog in MFC with MFC
Since the WINDOWS2000 is launched, its new user interface makes us a chance to change the taste. For example, its fades into the light-out animation menu, transparent window, another change is the change in the appearance of its file dialog box, the dialog box that occurs when the save file is opened in the program, it is better than the original in Windows 98. More: Let's take a look at the following two pictures:
Figure one
Figure II
Figure 1 is our most common file dialog (hereinafter referred to as an old-fashioned file dialog), Figure 2 is a common file dialog box in Windows2000 (herein, referred to as a new file dialog). The same is the file dialog, but there are different interfaces. In Figure 2, we can see a shortcut toolbar on the left side of the dialog, which can help us quickly position in your computer, which is more convenient to operate more than the dialog box shown in Figure. Surprisingly, the dialog shown in Figure 2 is not a patent of Windows2000, because when we run Office2000 in Windows 98, you can also see the file dialog shown in Fig. 2; in Windows2000, you can also see Figure 1 The dialog (such as the use interface of the VC). Moreover, more importantly, we use the file dialog generated by the CFiledialog of the VC, and cannot produce the interface shown in Figure 2. What is going on? Another point is not to understand, some applications (such as "Notepad program) actually have different file dialog interfaces when Windows2000 and in Windows 98, respectively? What is going on here? With these questions, I carefully check the MSDN, and there is a one-point understanding of the dialog interface. Below I write a little in writing a file dialog program, discuss with you:
The File dialog is a specific window, and the response to various messages is done by the "hook function" of the conversation. For the appearance of the dialog, we can implement the "File Dialog Template"; Function "is done. In VC, the system provides us with ready-made file dialog classes CFiledialog, using this class, we can easily generate file dialogs. When using CFiledialog, the most important work is to initialize its member variable m_ofn.
m_ofn is actually a structure of an OpenFileName type (STRUCT), and OpenFileName is defined as follows:
Typedef struct tagofn {
DWORD LSTRUCTSIZE; // OpenFileName size
DWORD FLAGS;
................ This is a specific member variable ......................
#if (_WIN32_WINNT> = 0x0500)
Void * pvreserved;
DWORD DWRESERVED;
DWORD flagsex;
#ENDIF / / (_WIN32_WINNT> = 0x0500)
OpenFileName, * lpopenfilename;
As can be seen, there is a Flags member variable in OpenFileName, which determines the appearance of the dialog, which is a set of predefined macros. With it, we can customize the personalized file dialog. For example, in Figure 1, "Open in a read-only mode is because" OFN_READOONLY "is included in the FLAGS. When we use the OpenFileName structure, we are used to setting the following statements to set lstructsize:
OpenFileName DlgfileOpen;
Dlgfileopen.lstructSize = sizeof (OpenFileName);
// Note: Whether it is running under Windows98 or Windows2000, the value of lstructSize will only be 76
In fact, if we carefully calculate, 76 is only #if (_win32_winnt> = 0x0500) The length of the member variable before, if you add #if (_win32_winnt> = 0x0500) three variables (PVReServed, dwreserved, flagsex) The value of LSSTRUCTSIZE should be 76 3 * 4 = 88. It is because of this habitual operation, we are inadvertently intended to make Windows2000 show the old-fashioned file dialog, what is the reason? please watch the following part:
We know that the version number of Windows 2000 has broken 5. If you use the _win32_winnt = 0x0500 to compile the program, the first two member parameters after the # if (_win32_winnt> = 0x0500) are used as the retention value, the remaining and one member parameter flagsex, there is a new flag value. For selection: OFN_EX_NOPLACESBAR, it is because of OFN_EX_NOPLACESBAR, you can't see the shortcut toolbar in the program you have written. Surprisingly, the VC does not provide a selection such as "OFN_EX_SHOWPLACESBAR compared to OFN_EX_NOPLACESBAR.
The reason why the value of lstructSize is calculated because the value of LSTRUCTSIZE directly affects the file dialog box when using the dialog built in the WINDOWS2000. If the size of OpenFileName is 76, the file dialog does not display shortcut toolbar; if the OpenFileName size is 88, the file dialog displays the shortcut toolbar. Of course, this statement has a prerequisite, in the following narratives, you will have a more profound understanding.
If Windows 2000 determines the way the file dialog is displayed by discriminating the OpenFileName's lstructSize value, then why there are programs that are running under Windows 98 can still display new file dialogs in Windows 2000 (such as "Notepad in Windows 98". Program)? Obviously, the lstructSize value under Windows 98 must be 76, so Windows 2000 must have another way to determine what form of use to display the file dialog, what?
There is a Flags value in the OpenFileName structure, which determines the appearance of the file dialog. Windows 2000 is the value of using this value to display the file dialog box (accurately, if you use the dialog that the MFC's cfiledialog generated is to determine the use through Flags and LSTRUCTSIZE What form is to display the file dialog. If the Flags value contains OFN_ENABLEHOK (enable hook functions), the lstructSize value is 88, displays the new file dialog; if Flags does not contain OFN_ENABLEHOOK (Note: CFiledialog's Flags must contain OFN_ENABLEHOOK, then, regardless of the value of lstructsize? Displays the new file dialog; if OpenFileName uses the dialog hook function, and the value of lstructSize is 76, Windows 2000 displays the old-fashioned file dialog; this display mechanism explains the use of MFC's cfiledialog under Windows 2000 Operation always shows the old-fashioned file dialog. Because MFC uses dialog hook functions in the file dialog, do not believe, you can see the CFiledialog class's implementation source program DLGFILE.CPP 76 line: assert (m_ofn.flags & offn_enablehook, this is fundamentally explained, The MFC dialog box must use hook functions! Moreover, "DLGFileOpen.lstructSize = SIZEOF (OpenFileName)" of us is only forced to require Windows2000 to display old-federated file dialogs. This is destined to use the CFILEDIALOG class file dialog to be displayed as a vintage file dialog box under Windows 2000.
The problem is obvious, summarizing the above, we can draw the following conclusions: 1. If you are in Windows 98, the dialog that you generate with the MFC's CFiledialog class is the display of the new dialog box. Maybe you will ask, when running Office2000 in Windows 98, the system can display the new file dialog box. The author is guess here, that is because the Office2000's file dialog does not use the MFC's conversation principle, if you want to be in Windows 98 Show new file dialogs, you have to redefine a file dialog class. 2. Obviously, not all file dialog boxes are generated by the CFiledialog of the MFC. If you don't have to use the CFileDialog dialog, you don't have to consider the appearance of the file dialog, the newly generated dialog box will automatically switch between the new and old-fashioned. 3. Under Windows 2000, use the MFC generated dialog, if you can confirm that the file dialog does not use the dialog hook function (such as getopenfilename () function), then the program you develop must display new dialogs ( Don't care about the value of lstructsizer in OpenFileName), and this program does not have to care about the operating system platform when using, it is not difficult to understand the "Notepad" program running in Windows 98 and Windows2000. Different file dialog interface interface If the MFC file dialog uses the hook function, then, and only the way is to modify the size of the OpenFileName. When the OpenFileName is 88, the Window2000 displays the new file dialog, otherwise the old-fashioned file dialog box is displayed. At the same time, it is to mention that the value of OpenFileName is set to 88. This program is not working properly in Windows 98 (you can't see the dialog in the dialog), you have to join the application's run platform Judgment code. With the above understandings, we can also use the MFC to make the file dialog box for the system platform. As "writing": in Windows 98, the old file dialog box is displayed, and the new file dialog is displayed in Windows2000. frame. Try the following procedure:
Built a Single Document (SDI) project in the VC, add two menu items in the resource editor: "Dialog Boxes with CFiledialog" and "Dialog Boxes with GetopenFileName", simultaneous join the two menus Command events The response function is as follows:
Void CMAINFRAME :: oncfiledialog ()
{
Cfiledialog DlgfileOpen (TRUE);
INT STRUCTSIZE = 0;
DWORD DWVERSION, DWWINDOWSMAJORVERSION, DWWINDOWSMINORVERSION;
// Test the current operating system, get the specific usage of the GetVersion, MSDN
DWVersion = getVersion ();
DWINDOWSMAJORVERSION = (DWORD)));
DWWINDOWSMINORVERSION = (DWORD));
// If the operating system is Windows NT / 2000
IF (dwversion <0x80000000)
STRUCTSIZE = 88; // Displays the new file dialog
Else
// Running operating system Windows 95 / 98structSize = 76; // Show old file dialog
DLGFILEOPEN.M_OFN.LSTRUCTSIZE = STRUCTSIZE;
Tchar lpstrfilename [max_path] = ""
DLGFILEOPEN.M_OFN.LPSTRFILE = LPSTRFILENAME;
IF (DLGFileOpen.domodal () == idok)
Messagebox ("The file you open is:" (cstring) DLGFileOpen.m_ofn.lpstrfile;
Else
MessageBox ("Open file error!");
}
Void CMAINFRAME :: OnGetopenFileName ()
{
OpenFileName OFN;
/ / Set the name of the file storage space
Tchar lpstrfilename [max_path] = ""
// Empty OpenFileName
ZeromeMory (& OFN);
OFN.LSTRUCTSIZE = SizeOf (OpenFileName); // LstructSize's return value is 76
// Set the parent window
OFN.HWNDOWNER = this-> m_hwnd;
// / Set the type of open file
OFN.LPSTRFILTER = "All Files / 10*.*/0 Text File / 0*.txt/0";
OFN.NMAXFILE = Max_path;
OFN.LPSTRFILE = LPSTRFILENAME;
IF (GetopenFileName (& off))
MessageBox ("The file you open is:" (cstring) OFN.LPSTRFILE;
Else
MessageBox ("Open file error!");
}
The above program can be downloaded in "Individual World" in my website "Hostel House" (http://nationaltax.home.chinaren.com). If you still want to further modify the MFC file dialog, you can use the "Open \ Save" in your system menu, you can use the new file dialog, you can redefine the CFiledialog class, the specific method does not make a detailed description, but You can also download it on the website I mentioned above.
It should be said that MFC is doing very well on the handling of file dialogue. If you go to see MSDN, you can't find this article as described. A simple question, but do so much work, this is also MFC "Feature"!