|
|
|
A customized Windows "Save as" common dialog boxFor Stony Brook Modula-2By Frank Schoonjans (frank.schoonjans@ugent.be)
In SPSS (a statistical software package) the following dialog box is displayed when you want to save a graph:
Notice the "Options" button. It allows, amongst others, to select the width and height of the JPG image. But how did the programmers put this button in this dialog box? Step 1. The basic Save as dialog boxThe standard Windows Save as dialog box is one of the common dialog boxes available from COMMDLG library. It's basic use is illustrated in the following example.
Basically, the Save as dialog box is obtained by initialising the members of an OPENFILENAME record and calling the GetSaveFileName procedure (both exported from COMMDLG). This results in the following dialog box.
Adding a Help buttonThe next step is to add a Help button in the dialog. This only requires a minor change in the code above, namely adding the flag OFN_SHOWHELP:
Flags := COMMDLG.OFN_HIDEREADONLY BOR COMMDLG.OFN_SHOWHELP;
This gives us a Help button:
Customizing the dialog and the Help buttonSo now we need to change the text of the help button into "Options...", and add some functionality to the button. First we add the flags: OFN_EXPLORER and OFN_ENABLEHOOK.
Flags := COMMDLG.OFN_HIDEREADONLY BOR COMMDLG.OFN_EXPLORER
BOR COMMDLG.OFN_ENABLEHOOK BOR COMMDLG.OFN_SHOWHELP;
The OFN_ENABLEHOOK flag enables the hook function specified in the lpfnHook member of the OPENFILENAME record.
lpfnHook := HookProc;
Our HookProc procedure is listed below.
<*/PUSH*>
<*/CALLS:WIN32SYSTEM*>
PROCEDURE HookProc(hwnd : HWND; uint : UINT; wparam : WPARAM; lparam : LPARAM): UINT [EXPORT];
VAR lpnmhdr : LPNMHDR;
str : String;
hwndParent : HWND;
BEGIN
CASE uint OF
| WM_INITDIALOG :
hwndParent := GetParent(hwnd);
str:="&Options...";
COMMDLG.CommDlg_OpenSave_SetControlText(hwndParent,Dlgs.pshHelp,str);
| WM_NOTIFY : lpnmhdr:=CAST(LPNMHDR,lparam);
CASE lpnmhdr^.code OF
| COMMDLG.CDN_HELP :
FUNC MessageBox(GetParent(hwnd),"Options button clicked.","Alert",MB_OK);
ELSE
END;
ELSE
END;
RETURN 0;
END HookProc;
<*/POP*>
The main problem we had to solve here is to find out the identifier of the Help button, to use as the second parameter in the CommDlg_OpenSave_SetControlText procedure. We found the following information on msdn.microsoft.com (search for "Open and Save As Dialog Boxes"): The following table shows the identifiers of the standard controls in the Explorer-style Open and Save As dialog boxes. The identifiers are constants defined in Dlgs.h and Winuser.h.
So we learn that the identifier of the Help button is pshHelp which is defined in Dlgs.h. We did not find a translated header Dlgs.def in the Stony Brook M2 distribution, so we provide a translation in this project download below (the value of pshHelp actually is 040EH). So here is our customized Save as dialog box:
For illustration, we only display a simple message box when the user clicks Help. The idea is of course to display a new dialog box requesting additional input from the user. DownloadYou can download a complete Stony Brook M2 project, including the example source and the Dlgs.def definition module.
|