16
Nov
0

C++ – Qt, contextmenu windows, Right click on image shortcut

In my freeware CliMage (used to resize image) you can right click on images to send images to my software. To do that I put some line in the windows registry. Here is the code.

Before starting

  • To modify the registry you have to be an administrator.
  • If you select many files and use your shortcut, windows will open as many programs as files you select (So you may need a SingleAppInstance).
  • This code is useful only for system files (.txt, .png, .jpg, etc…) but not for personal files extension!

The code

////////////////////////////////////////////////////////
// Register Right Click
////////////////////////////////////////////////////////
void Widget::Register(const QString& actionName, const QString& label, const QString& executable){
        QSettings settings("HKEY_CLASSES_ROOT",QSettings::NativeFormat);

        settings.beginGroup("jpegfile");
        settings.beginGroup("shell");
        settings.beginGroup(actionName);
        settings.setValue(".",label);
        settings.beginGroup("command");
        settings.setValue(".", "\"" + QDir::currentPath().replace("/","\\") + "\\" + executable + "\"" + " \"%1\"");
        settings.endGroup();
        settings.endGroup();
        settings.endGroup();
        settings.endGroup();

        settings.beginGroup("pngfile");
        settings.beginGroup("shell");
        settings.beginGroup(actionName);
        settings.setValue(".",label);
        settings.beginGroup("command");
        settings.setValue(".", "\"" + QDir::currentPath().replace("/","\\") + "\\" + executable + "\"" + " \"%1\"");
        settings.endGroup();
        settings.endGroup();
        settings.endGroup();
        settings.endGroup();
}

void Widget::Unregister(const QString& actionName){
        QSettings settings("HKEY_CLASSES_ROOT",QSettings::NativeFormat);

        settings.beginGroup("jpegfile");
        settings.beginGroup("shell");
        settings.remove(actionName);
        settings.endGroup();
        settings.endGroup();

        settings.beginGroup("pngfile");
        settings.beginGroup("shell");
        settings.remove(actionName);
        settings.endGroup();
        settings.endGroup();
}

Resources

http://msdn.microsoft.com/en-us/library/cc144171%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa969321.aspx

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.

Comments are closed.

Celadon theme by the Themes Boutique