Drag&Drop Files into Game possible?

Hi,

I have to make my game’s window capable of reacting to drag&drop operations coming from the outside of the application (especially windows), like dragging a png-file from my desktop into my application (e.g. to be able to get its filepath).

I successfully tried adding an IWindowsMessageHandler to my FWindowsApplication in c++ but this does not seem to be the right way since I can’t get any useful information out of it.

DropMessageHandler = FDropMessageHandler();
FWindowsApplication* WindowsApplication = (FWindowsApplication*)GenericApplication.Get();
WindowsApplication->AddMessageHandler(DropMessageHandler);

class FDropMessageHandler
: public IWindowsMessageHandler
{

public:

virtual bool ProcessMessage(HWND hwnd, uint32 msg, WPARAM wParam, LPARAM lParam, int32& OutResult) override
{

//If right approach, what to do here?

return true;

}
};

Am I missing something with the MessageHandler or is there any other way to solve my problem?

I solved the problem myself. I already had the right approach and this is how I got it working:

#include "AllowWindowsPlatformTypes.h"
#include "shellapi.h"
#include "HideWindowsPlatformTypes.h"

class FDropMessageHandler : public IWindowsMessageHandler { 
public:

virtual bool ProcessMessage(HWND hwnd, uint32 msg, WPARAM wParam, LPARAM lParam, int32& OutResult) override {

int32 NumFiles;
HDROP hDrop;
TCHAR NextFile[MAX_PATH];

if (msg == WM_DROPFILES)
	{
		hDrop = (HDROP)wParam;
		// Get the # of files being dropped.
		NumFiles = DragQueryFile(hDrop, -1, NULL, 0);

		for (int32 File = 0; File < NumFiles; File++)
		{
			// Get the next filename from the HDROP info.
			if (DragQueryFile(hDrop, File, NextFile, MAX_PATH) > 0)
			{
				FString Filepath = NextFile;
               //Do whatever you want with the filepath
			}
		}
	}    
return true; 
}
 };

The DragQueryFile function that gives me the information i need is only available if the shellapi.h is included.
This is only for Windows, make shure you surround all the platform specific code with #if PLATFORM_WINDOWS … #endif

1 Like

I think that this is a decent solution since we do not have platform-agnostic code for drag & drop in runtime modules yet. I referenced it in a related tutorial.

@MBatzdorf Hi, I’m following your solution, but the event WM_DROPFILES doesn’t show up anytime (I am printing all of them, and I get, WM_WINDOWPOSCHANGED, WM_MOVING and stuff like that, but no WM_DROPFILES).

I have tried to enable the event using ChangeWindowMessageFilter, but it doesn’t work either (don’t know if I am doing it properly)

Also, when I drag something into the window, the mousse pointer changes to a circle with a bar crossing it, stating (I guess) that you can’t do that action.

How did you get it working?

Thanks in advance!!

Bump! I’m at the same point as havidarou. The mouse pointer changes to the crossed cricle and a WM_DROPFILES Message doesn’t show up… Any progress since the last year at this?

Y’all need to add:
HWND hwndd = GetActiveWindow();
DragAcceptFiles(hwndd, true);
when adding the message handler to the FWindowsApplication

Peace and love :slight_smile:

Here is how you get the PlatformApplication.
Don’t forget to unregister the handle!

        FSlateApplication& slateApp = FSlateApplication::Get();
        TSharedPtr<class GenericApplication> platformApp = slateApp.GetPlatformApplication();
        if (platformApp.IsValid())
        {
#if PLATFORM_WINDOWS
... do your stuff
#endif // PLATFORM_WINDOWS
        }

And you should not return true always!
Return true only when the message was handled!