CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2003
    Location
    Sweden
    Posts
    381

    Windows right click menu...

    How can I write a program that ads an item on windows right click menu?
    I've searched for articles in this topic but no result...
    ...and justice for all

  2. #2
    Join Date
    Oct 2006
    Location
    Timisoara, Romania
    Posts
    123

  3. #3
    Join Date
    Oct 2003
    Location
    Sweden
    Posts
    381

    Re: Windows right click menu...

    I can't see how I can manipulate the "Windows-explorer-right-click-menu" with that...
    ...and justice for all

  4. #4
    Join Date
    Oct 2006
    Location
    Timisoara, Romania
    Posts
    123

    Re: Windows right click menu...

    Maybe I misunderstood. Do you want to modify a right-click pop-up menu? The Windows Explorer menu? Could you please be a little more specific?

  5. #5
    Join Date
    Oct 2003
    Location
    Sweden
    Posts
    381

    Re: Windows right click menu...

    The windows explorer menu.
    (When I right-click some files in windows explorer I want these files as input to my program that shoud invoke when my own option in the menu is choosen).
    ...and justice for all

  6. #6
    Join Date
    Oct 2006
    Location
    Timisoara, Romania
    Posts
    123

    Re: Windows right click menu...

    You do that by adding a few registry entries. First you associate your file extention with a key. Let's say you have a .tst file. You have to create a key in HKEY_CLASSES_ROOT named ".tst" with a value of "TstFile". Then create a "TstFile" key and, if you want to associate the icon of your application with that file type (so their icon will be the icon of your application), create a subkey named "DefaultIcon" inside "TstFile" with the default value being your executable (full-path and name).

    To associate the open command with your application add another subkey inside "TstFile" named "shell". An "open" subkey inside "shell" and a "command" subkey inside "open". Set the default value of the "command" subkey to your executable (full path and name) and add a %1 at the end, and quote both. For example "C:\program files\myapp\myapp.exe" "%1".

    Now when you double click on a .tst file or right-click and select open, it will start your application, passing it as a parameter the file.

    You can do all of this programatically using the following code:

    Code:
    using Microsoft.Win32;
    
    ...
    
    Registry.SetValue("HKEY_CLASSES_ROOT\\.tst", "", "TstFile");
    Registry.SetValue("HKEY_CLASSES_ROOT\\TstFile\\DefaultIcon", "", Application.ExecutablePath); // this is the full path and file name of this application - you might want to put another path and name here
    Registry.SetValue("HKEY_CLASSES_ROOT\\TstFile\\shell\\open\\command", "", "\"" + Application.ExecutablePath + "\" \"%1\"");
    If you want another command instead of open, create another subkey and give it a name so that Explorer knows what to show in the menu:

    Code:
    Registry.SetValue("HKEY_CLASSES_ROOT\\TstFile\\shell\\mycmd", "", "My Command"); // Explorer will show "My Command" on the menu
    Registry.SetValue("HKEY_CLASSES_ROOT\\TstFile\\shell\\mycmd\\command", "", "\"" + Application.ExecutablePath + "\" \"%1\"");
    This will behave like open but will have a different name. You can pass any parameters to your application in the above string.

    Now, inside your application, check if the Main method accepts arguments.
    If it looks like static void Main() change it to static void Main(string[] args).
    The args array will contain parameters passed to your application, in this case, the full path and file name of the file a user opened from explorer.

    I'm sorry if this sounds a bit confusing and don't hesitate to ask for clarifications

  7. #7
    Join Date
    Oct 2003
    Location
    Sweden
    Posts
    381

    Re: Windows right click menu...

    Perfectt!
    ...and justice for all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured