How to Associate a File Extension with Your Program
Here's
a quick "How-to" on how to associate a file extension with your
program. I normally place this code in my mainforms "OnCreate"
function. This of course has the disadvantage that you register the association
every time you execute your program. You can avoid this, by checking which
program (if any) is associated with the this file extension, before you
register your own program. If it's your program, just skip the rest of the
association process (hint: this can be done by reading the default value
("") for the ".fjf" key below. If that string is the same
as your program then just skip the rest).
NB:
Remember to add #include <registry.hpp> to your file.
Register the .fjf file extension
Create
a new instance of the Registry class (you don't have to do it with pointers but
I like it better this way ;)
TRegistry* thisReg = new TRegistry();
Set the
root key to HKEY_CLASSES_ROOT, which is where file associations are stored
thisReg->RootKey = HKEY_CLASSES_ROOT;
Open/Create
the key for the file extension you want to change/register
thisReg->OpenKey(".fjf",
true);
Set the
default value to the program you want to associate the extension with NB: This
can be what ever you want. The name (here FormLoader) is just a link to the
next key we are going to create
thisReg->WriteString("",
"FormLoader");
Close
Key
thisReg->CloseKey();
Write the default Open command (here we open/create the key with the
association name from above)
thisReg->OpenKey("FormLoader",
true);
Set the
desciption of the file association
thisReg->WriteString("",
"FormLoader Jobfile");
Create
Key "shell" and set "open" as default command (which is
actually the FormLoader->shell key ;)
thisReg->OpenKey("shell",
true);
thisReg->WriteString("", "open");
Open
the "Open" key (which is actually the FormLoader->shell->open
key ;)
thisReg->OpenKey("open",
true);
Write
the text (which will be shown in the popup menu for the object on the explorer)
thisReg->WriteString("",
"&Open");
Create
Key "command", which is what is caried out if the user select
"Open" --- *
thisReg->OpenKey("command",
true);
Write
what should be done when "Open" is choosen ParamStr(0) is the name
and path to my program %1 is the paramter to my program. This means that my
program will be started with the object, on which we have choosen
"Open," as parameter.
thisReg->WriteString("",
ParamStr(0)+" \"\%1\"");
Close
Key
thisReg->CloseKey();
Write Open batch command
Here is
shown how to create an extra command for your extension.
thisReg->OpenKey("FormLoader",
true);
thisReg->OpenKey("shell", true); thisReg->OpenKey("open
batch", true);
thisReg->WriteString("", "Open &Batch");
thisReg->OpenKey("command", true);
thisReg->WriteString("", ParamStr(0)+" -b
\"\%1\"");
thisReg->CloseKey();
Set default icon
This is
the icon that will be given each object/file that has extension
".fjf"
thisReg->OpenKey("FormLoader",
true);
thisReg->OpenKey("DefaultIcon", true);
Here is
set the icon to the 2 icon resource in my programs exe file. To give the
objects/files the same icon as your own program just write
thisReg->WriteString("", ParamStr(0)+",0"); instead
thisReg->WriteString("",
ParamStr(0)+",1");
Clean up
delete thisReg;