Isaac Levin
Isaac Levin

Reputation: 2899

Drag and Drop from Outlook Window to Application .EXE file (or Icon) in .Net

This has been asked before, but with the methods I have seen, I cannot get what I want to happen. Currently, I have a Windows Form that if I run the .EXE (and bring up the form itself), I can drop Emails from outlook into it no problem. However, what I am looking for is to have this functionality when a user drops the message directly from Outlook to the Icon on the .EXE file. I can do this fine if I save the file locally and drop it onto the icon, but straight from Outlook, I get a circle with a line through it. Is there a property I need to set on the app to make this work. I used this code to get the dropping the message onto the form window to work.

http://www.codeproject.com/Articles/28209/Outlook-Drag-and-Drop-in-C

This is the code that I wrote that drops onto the Icon.

 static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var form = new Form1();

        if (args.Length > 0)
        {
            form.ProcessCommandLine(args[0]);
        }

        Application.Run(form);
    }
}

 public void ProcessCommandLine(string commandLine)
    {
        lstFiles.Items.Clear();

        var fileAttributes = File.GetAttributes(commandLine);
        if (fileAttributes.HasFlag(FileAttributes.Directory))
        {
            ProcessDirectory(commandLine);
        }
        else
        {
            ProcessFile(commandLine);
        }
    }

Any help would be appreciated, thank you.

Upvotes: 1

Views: 3037

Answers (1)

Kevin McCormick
Kevin McCormick

Reputation: 2398

You are looking to create a Shell Drop Handler. As you have discovered, the default drop handler for .EXE files accepts any file as a droppable item, and it automatically launches the application with the path to the dropped file. Other items, such as an mail or Calendar object being directly dragged from Outlook, is not supported by Windows Explorer directly. One example of a drop handler that is included with Windows is if you drag a file onto a ZIP file, it automatically adds that file to the ZIP archive when you drop.

If you still want to create your own drop handler, you can perform any custom action when any dropped item is dropped on any file (such as your program's icon, a shortcut, etc.) This is not a trivial task, and writing shell extensions from managed code (C# or VB) is generally not recommended. (See: http://blogs.msdn.com/b/oldnewthing/archive/2006/12/18/1317290.aspx)

Once you create your drop handler, it is a two-step process:

  1. During program installation, create a file on the desktop with a unique file extension (such as .myprogdroptarget).
  2. Register the drop handler for .myprogdroptarget so that this icon becomes a "magic" drop target for objects.

For some sample code on how to create a Drop Handler in ATL/C++, check out the Microsoft All-In-One code framework, specifically the class ATLShellExtDragAndDropHandler.cpp

Alternate solution:

Consider creating a Windows Desktop Gadget that performs similar functionality. The coding should be simpler since you won't have to dig into C++. There was once a Vista gadget called the Magic Folder that accepted items as drop targets, however I can no longer find it on the Windows gallery. Here's an article that described how it worked:

http://www.howtogeek.com/howto/windows-vista/keep-your-vista-desktop-clean-with-the-magic-folder/

And here is a link to the author (maybe he'll share the source code if you ask nicely): http://davecra.wordpress.com/

Upvotes: 3

Related Questions