If you have registered your own file type (extension) for an application, you can tell Windows to start your application when a user double clicks the file (or selects "Open" from the context popup menu) in the Windows Explorer.
Another approach to allowing the user to send a file to your application for processing, without the need to register a file type, is to add your application to the special Windows "Send To" menu.
The "Send To" appears as an item in the context menu when you, for example, right click on a file in the Windows Explorer.
By using the Send To command, you can quickly send a file to different locations including a floppy disk, your desktop, another person using e-mail, or the My Documents folder. The SendTo folder contains the shortcuts for the destinations that are displayed on the Send To menu. Every user on the computer has a SendTo folder and can customize its contents.
Here's how to place a shortcut to your Delphi application to the Send To menu:
- Create a shortcut to your application,
- Place it in the special Windows "Send To" folder,
- When your application is activated, process the file being "sent".
procedure CreateShortcutIn(const SpecialFolderCSIDL: integer; const shortcutName : string) ; var IObject : IUnknown; ISLink : IShellLink; IPFile : IPersistFile; PIDL : PItemIDList; InFolder : array[0..MAX_PATH] of Char; TargetName : String; LinkName : WideString; begin TargetName := ParamStr(0) ; IObject := CreateComObject(CLSID_ShellLink) ; ISLink := IObject as IShellLink; IPFile := IObject as IPersistFile; with ISLink do begin SetPath(pChar(TargetName)) ; SetWorkingDirectory(pChar(ExtractFilePath(TargetName))) ; end; //get the location of the "special folder" SHGetSpecialFolderLocation(0, SpecialFolderCSIDL, PIDL) ; SHGetPathFromIDList(PIDL, InFolder) ; LinkName := Format('%s\%s.lnk',[InFolder, shortcutName]) ; IPFile.Save(PWChar(LinkName), false) ; end;
//CSIDL_SENDTO = "Send TO" special folder
CreateShortcutIn(CSIDL_SENDTO,'My Delphi Application') ;
Processing the file sent to the application in the form's OnCreate event handler:
Note: to manually add a destination to the Send To menu, you must add a shortcut to the SendTo folder. To do this, follow these steps:
procedure TMainForm.FormCreate(Sender: TObject) ; begin //get the file that was "send to" this application if ParamCount > 0 then ShowMessage(ParamStr(1)) ; end;
- Click Start, and then click Run
- In the Open box, type sendto, and then click OK.
- Use the drag-and-drop operation to move the item that you want to the SendTo folder.
Delphi tips navigator:
» How to Translate a Virtual Key Code to a Character
« Drawing a Focus Rectangle Around the Active Control