Code to associate file extensions with your application[wxWidgets]

Hi, this is a mini tutorial on how to associate file extensions to your application, so they load on double-click and show a custom icon.

This is how I'm doing it in Flan, a tilemap editor for Flixel that I made.

First of all, you must support loading of files as a parameter to your application executable.
You can add something like this to your derived wxApp class' OnInitialize function:

bool MyApplication::OnInit()
{

\t//...Create windows and stuff...

\tif(argc \g 1) {
\t\tLoadMapFromPath(argv[1]);
\t}
}

Then in your Frame or Dialog implementation file, add this two headers:
#include \lwx/msw/registry.h\g
#include \lwx/stdpaths.h\g


Then somewhere you can use this code to associate (replace .flan with your own file extension)

wxString exePath = wxStandardPaths::Get().GetExecutablePath();

wxRegKey regKey;


regKey.SetName("HKEY_CLASSES_ROOT\\\\.flan\\\\DefaultIcon");
regKey.Create();
regKey.SetValue("", wxString::Format("%s,1", exePath));
regKey.Close();

regKey.SetName("HKEY_CLASSES_ROOT\\\\.flan\\\\shell\\\\open\\\\command");
regKey.Create();
regKey.SetValue("", wxString::Format("\\"%s\\" \\"%%1\\"", exePath));
regKey.Close();

wxLogMessage(_("\\".flan\\" files successfully associated.\\n"
\t"Sometimes it takes a PC restart is required to show the proper icons."));


If you want to have a custom icon show up, you should add it as a resource to your application.
You can see how to do that in this tutorial, but with a difference:
You must add another icon to the resource (.rc) file i.e.:
IDI_APP     ICON       "app_icon.ico"
IDI_FILE ICON "file_icon.ico"


I hope you find this useful.
Best regards
-Martín

0 comments: