How can I display the RunFile dialog?
Applies To: C++Builder 1 or higher
Category: Knowledge Base

-
- Start a new application.
-
- Insert in your source code:
-
- typedef void (__stdcall *pRunFileDlg)(HWND, HICON,
- LPCSTR, LPCSTR, LPCSTR, UINT);
-
- HINSTANCE hInstance;
- pRunFileDlg RunFileDlg;
-
- typedef struct {
- NMHDR hdr;
- LPCSTR lpFile;
- LPCSTR lpDirectory;
- int nShow;
- } NM_RUNFILEDLG;
-
- Edit the OnCreate event of your form:
-
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- hInstance = LoadLibrary("SHELL32.DLL");
- if(!hInstance)
- {
- ShowMessage("Error loading SHELL32.DLL\n"
- "This application will be closed");
- Application->Terminate();
- }
-
- RunFileDlg = (pRunFileDlg)
- GetProcAddress(hInstance, (char *)61);
- }
-
- Edit the OnDestroy event of your form:
-
- void __fastcall TForm1::FormDestroy(TObject *Sender)
- {
- FreeLibrary(hInstance);
- }
-
- Open your header file and insert:
-
- private:
- MESSAGE void __fastcall WMNotify(TMessage &);
- BEGIN_MESSAGE_MAP
- MESSAGE_HANDLER(WM_NOTIFY, TMessage,
- WMNotify);
- END_MESSAGE_MAP(TForm);
-
- Insert in your source code:
-
- void __fastcall TForm1::WMNotify(TMessage &Message)
- {
- NMHDR *Notification = (NMHDR*)Message.LParam;
- if(Notification->code == -510)
- {
- NM_RUNFILEDLG* RunFile = (NM_RUNFILEDLG*)
- Message.LParam;
- ShowMessage("The following application about "
- "to be started:\n" + String(RunFile->lpFile));
- }
- TForm::Dispatch(&Message);
- }
-
- Add a Button. Edit its OnClick event:
-
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- RunFileDlg(Handle, NULL, NULL, "RunFile Dialog",
- "This is a simple RunFile dialog example. "
- "Please select a file to be opened:", NULL);
- }
-
- RunFileDlg is an undocumented function that allows you to show the RunFile dialog. (For more information on undocumented functions, read Undocumented Windows Functions).
-
- The RunFileDlg function is dynamically loaded from SHELL32.DLL and executed when Button1 is pressed. Once the user has selected a file to open, Windows sends a WM_NOTIFY message to your main form.
C++Builder Developer's Network
Copyright © Yoto Yotov