The Open dialog box appears after you initialize the members of an OPENFILENAME structure and call the GetOpenFileName function.
Following is an Open dialog box.
Before the call to GetOpenFileName, structure members contain such data as the name of the directory and the filter that are to appear in the dialog box. (A filter is a filename extension. The common dialog box code uses the extension to filter appropriate filenames from a directory.) After the call, structure members contain such data as the name of the selected file and the number of characters in that filename.
To display an Open dialog box, an application should perform the following steps:
1.Store the valid filters in a character array.
2.Set the lpstrFilter member to point to this array.
3.Set the nFilterIndex member to the value of the index that identifies the default filter.
4.Set the lpstrFile member to point to an array that contains the initial filename and receives the selected filename.
5.Set the nMaxFile member to the value that specifies the length of the filename array.
6.Set the lpstrFileTitle member to point to a buffer that receives the title of the selected file.
7.Set the nMaxFileTitle member to specify the length of the buffer.
8.Set the lpstrInitialDir member to point to a string that specifies the initial directory. (If this member does not point to a valid string, it must be set to 0 or point to a string that is set to NULL.)
9.Set the lpstrTitle member to point to a string specifying the name that should appear in the title bar of the dialog box. (If this pointer is NULL, the title will be Open.)
10.Initialize the lpstrDefExt member to point to the default extension. (This extension can be 0, 1, 2, or 3 characters long.)
11.Call the GetOpenFileName function.
The following example initializes an OPENFILENAME structure, calls the
GetOpenFileName function, and opens the file by using the lpstrFile member
of the structure. The OPENFILENAME structure should be global or declared
as a static variable.
OPENFILENAME
ofn; char szDirName[256]; char szFile[256], szFileTitle[256]; UINT i, cbString; char chReplace; /* string separator for szFilter */ char szFilter[256]; HFILE hf; /* Get the system directory name, and store in szDirName. */ GetSystemDirectory(szDirName, sizeof(szDirName)); szFile[0] = '\0'; if ((cbString = LoadString(hinst, IDS_FILTERSTRING, szFilter, sizeof(szFilter))) == 0) { ErrorHandler(); return 0L; } chReplace = szFilter[cbString - 1]; /* retrieve wildcard */ for (i = 0; szFilter[i] != '\0'; i++) { if (szFilter[i] == chReplace) szFilter[i] = '\0'; } /* Set all structure members to zero. */ memset(&ofn, 0, sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.lpstrFilter = szFilter; ofn.nFilterIndex = 1; ofn.lpstrFile= szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFileTitle = sizeof(szFileTitle); ofn.lpstrInitialDir = szDirName; ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if (GetOpenFileName(&ofn)) { hf = _lopen(ofn.lpstrFile, OF_READ); . . /* Perform file operations. */ . } else ErrorHandler();
The string referred to by the IDS_FILTERSTRING constant in the preceding example is defined as follows in the resource-definition file:
STRINGTABLE
BEGIN IDS_FILTERSTRING "Write Files(*.WRI)|*.wri|Word Files(*.DOC)|*.doc|" END
The vertical bars in this string are used as wildcards. After using the LoadString function to retrieve the string, the wildcards are replaced with NULL. The wildcard can be any unique character and must be included as the last character in the string. Initializing strings in this manner guarantees that the parts of the string are contiguous in memory and that the string is terminated with two null characters.
Applications that can open files over a network can create a new message identifier for the string defined by the SHAREVISTRING constant. The application creates the new message identifier by calling the RegisterWindowMessage function and passing this constant as the single parameter. After calling RegisterWindowMessage, the application is notified whenever a sharing violation occurs during a call to the OpenFile function. For more information about processing registered window messages, see Section 4.5, “Using Find and Replace Dialog Boxes.”