Add the SaveAsDlg Dialog Function

You need to supply a dialog function for the Save As dialog box. The function will retrieve a filename from the edit control and copy the name to the global variable, FileName. The dialog function should look like this:

int FAR PASCAL SaveAsDlg(hDlg, message, wParam, lParam)

HWND hDlg;

unsigned message;

WORD wParam;

LONG lParam;

{

char TempName[128];

switch(message) {

case WM_INITDIALOG:

if(!FileName[0])

bSaveEnabled = FALSE;

else {

bSaveEnabled = TRUE;

DlgDirList(hDlg, DefPath, NULL, IDC_PATH, 0x4010);

SetDlgItemText(hDlg, IDC_EDIT, FileName);

SendDlgItemMessage(hDlg, IDC_EDIT, EM_SETSEL, 0,

MAKELONG(0, 0x7fff));

}

EnableWindow(GetDlgItem(hDlg, IDOK), bSaveEnabled);

SetFocus(GetDlgItem(hDlg, IDC_EDIT));

return (FALSE); /* FALSE since Focus was changed */

case WM_COMMAND:

switch (wParam) {

case IDC_EDIT:

if(HIWORD(lParam) == EN_CHANGE && !bSaveEnabled)

EnableWindow(GetDlgItem(hDlg, IDOK),

bSaveEnabled = TRUE);

return(TRUE);

case IDOK:

GetDlgItemText(hDlg, IDC_EDIT, TempName, 128);

if(CheckFileName(hDlg, FileName, TempName)) {

SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,

(LPSTR) FileName);

if(str[0]) strcpy(DefPath, str);

EndDialog(hDlg, IDOK);

}

return(TRUE);

case IDCANCEL:

EndDialog(hDlg, IDCANCEL);

return(TRUE);

}

break;

}

return(FALSE);

}

The WM_INITDIALOG case enables or disables the Save button. The button should be disabled if there is no current filename. The EnableWindow function, along with the bSaveEnabled variable, enables or disables the button. If there is a current filename, it should be the proposed name. The SetDlgItemText function copies the filename to the edit control, and the SendDlgItemMessage function selects the entire name for editing. The DlgDirList function sets the IDC_PATH control to the current directory. Since there is no list box to fill, no list-box ID is given.

The WM_COMMAND case processes notification messages from the controls in the dialog box. When the function receives the EN_CHANGE notification from the edit control, IDC_EDIT, it uses the EnableWindow function to enable the Save button, if it is not already enabled.

When the function receives a notification from the Save button, it uses the GetDlgItemText function to retrieve the filename in the edit control, then checks the validity of the filename by using the locally defined CheckFileName function. This function checks the filename to make sure it contains no path separators or wildcard characters. It then checks to see if the file already exists; if it does, CheckFileName uses the MessageBox function to ask the user whether the file should be overwritten. Finally, the dialog function uses the SeparateFile function to copy the filename to the DefSpec and DefPath variables.