How to Detect System-Wide Drag & Drop Start (Outside App Window)?

Patrick 0 Reputation points
2025-04-06T12:10:39.31+00:00

Hi everyone,

We are developing a Windows desktop application using C# in Visual Studio 2022. The application aims to simplify common file operations for users – specifically, converting and compressing images, videos, audio, handling archives, etc. We're striving for the most intuitive user experience possible.

We'd like to implement a feature that would significantly streamline the process of getting files into our application. We envision the following scenario:

  1. A user clicks on a file (e.g., image, video) on their desktop or in File Explorer.
  2. They start dragging the file with the mouse (initiating a system-wide drag-and-drop operation).
  3. At the moment the user begins dragging the file (or while dragging it over the desktop – i.e., outside any of our application's windows), we want our application to display a small popup window (could be a toast, a small borderless window, etc.).
  4. This window would serve as a visual drop target, where the user could release the file, thereby passing it to our application for processing according to configured rules (compression, conversion, etc.).

We know how to implement drag-and-drop into an existing window of our application using standard mechanisms (.NET, WinUI 3, etc.). However, our question specifically concerns the possibility of reacting to the initiation or progress of a system drag operation that occurs outside our application's window and dynamically showing a UI element from our application in response.
Our questions are:

  1. Is this functionality technically achievable on the Windows platform using APIs accessible from C#?
  2. If yes, what technologies, frameworks, or specific Windows APIs (Win32, UWP/WinRT, .NET, UI Automation?) would be most suitable for this purpose? Links to documentation or examples would be greatly appreciated.
  3. If this isn't directly supported or easily achievable, are there any known workarounds or alternative approaches? Alternatively, does anyone know if Microsoft has plans to add similar functionality to future versions of Windows or developer frameworks?

This feature would fit perfectly with our application's concept and we believe users would find it very convenient.

Thanks in advance for any ideas, advice, or pointers!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,418 questions
{count} votes

3 answers

Sort by: Most helpful
  1. gekka 11,856 Reputation points MVP
    2025-04-07T10:34:53.7733333+00:00

    You might be able to detect it by monitoring the creation on desktop window that appears when the mouse drag operation begins.

    // Add ref UIAutomationClient and UIAutomationTypes
    var root = System.Windows.Automation.AutomationElement.RootElement;
    var walker = System.Windows.Automation.TreeWalker.RawViewWalker;
    
    System.Windows.Automation.Automation.AddStructureChangedEventHandler(root, System.Windows.Automation.TreeScope.Children, (s, e) =>
    {
        if (e.StructureChangeType == System.Windows.Automation.StructureChangeType.ChildAdded)
        {
            var child = walker.GetFirstChild(root);
            while (child != null)
            {
                string className = "";
                try
                {
                    className = child.Current.ClassName;
                }
                catch
                {
                    child = walker.GetNextSibling(child); //
                    continue;
                }
    
                if (className == "SysDragImage")
                {
                    Console.WriteLine("Detect Drag" + System.Windows.Forms.Cursor.Position);
                    break;
                }
                child = walker.GetNextSibling(child);
            }
        }
    });
    

    # Fix inifinite loop

    0 comments No comments

  2. Jiale Xue - MSFT 49,831 Reputation points Microsoft External Staff
    2025-04-08T03:12:50.8+00:00

    Hi @Patrick , Welcome to Microsoft Q&A,

    You can create a borderless, transparent, click-through (optional) small window (winforms);

    This window resides on the desktop or in a certain area of ​​the screen;

    Use RegisterDragDrop to register as a drag and drop target;

    When the user drags a file over the window in the Explorer, it becomes visible or semi-transparent and interactive, and the user can release the file into this area;

    Once the file is released, your application can receive the drag and drop event and start processing.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. RLWA32 48,151 Reputation points
    2025-04-08T08:26:29.2433333+00:00

    Another possibility is to use WinEvents to receive notifications of drag and drop events. Events for WinEvents are documented at Event Constants and include the following for drag and drop -

    Dragdropevents

    Drag and drop events are also supported by UI Automation. For example, see Event Identifiers (UIAutomationClient.h)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.