The Save As dialog box appears after you initialize the members of an OPENFILENAME structure and call the GetSaveFileName function.
Following is a Save As dialog box.
Before the call to GetSaveFileName, structure members contain such data as the name of the initial directory and a filter string. After the call, structure members contain such data as the name of the file to be saved and the number of characters in that filename.
The following example initializes an OPENFILENAME structure, calls GetSaveFileName function, and saves the file. 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; /* * Retrieve the system directory name, and store it in * szDirName. */ GetSystemDirectory(szDirName, sizeof(szDirName)); if ((cbString = LoadString(hinst, IDS_FILTERSTRING, szFilter, sizeof(szFilter))) == 0) { ErrorHandler(); return 0; } 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)); /* Initialize the OPENFILENAME members. */ szFile[0] = '\0'; ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.lpstrFilter = szFilter; ofn.lpstrFile= szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFileTitle = sizeof(szFileTitle); ofn.lpstrInitialDir = szDirName; ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT; if (GetSaveFileName(&ofn)) { . . /* Perform file operations. */ . } else ErrorHandler();
The string referred to by the IDS_FILTERSTRING constant in the preceding example is defined in the resource-definition file. It is used in exactly the same way as the IDS_FILTERSTRING constant discussed in Section 4.3.1, “Displaying the Open Dialog Box in Your Application.”