Click here to Skip to main content
14,216,534 members
Rate this:
Please Sign up or sign in to vote.
See more:
Well I have an external app, which I can Identify its windows handles by spying or searching their class names. One of this windows is a tree type windows, showing folders and files inside the window. I am wondering how I can send drop message to the specified window to simulate drag drop of several files.

So I have searched the internet, and found some information, I have interest to do this in c#:

I found this info:

WM_DROPFILES message: Sent when the user drops a file on the window of an application that has registered itself as a recipient of dropped files.

PostMessage(

    (HWND) hWndControl,   // handle to destination control

    (UINT) WM_DROPFILES,  // message ID

    (WPARAM) wParam,      // = (WPARAM) (HDROP) hDrop;

    (LPARAM) lParam       // = 0; not used, must be zero 

);


So on what I know: hDrop: A handle to an internal structure describing the dropped files.

1) And the function return should be zero on success. 2) The HDROP is a handle to an internal structure describing the dropped files which is declared in Shellapi.h. 3) WM_DROPFILES number is 0x233 4) lParam: Must be zero.

So the code should be something like this:
private const UInt32 WM_DROPFILES = 0x0233;

PostMessage( SomeHWND, WM_DROPFILES, wParam, intPtr.Zero );


Anyone knows how to fill wparam in this case?

What I have tried:

// DragAndDrop.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <shlobj.h>
#include <tchar.h>


int main(int argc, char* argv[]) {

for (int i = 0; i <= WM_DROPFILES; i++)
{
ChangeWindowMessageFilter (i, MSGFLT_ADD);
}

if (HWND hwnd = FindWindow ("OpusApp", NULL)) {

//HGLOBAL hGlobal = GlobalAlloc (GMEM_FIXED,
//sizeof ("d:\\DragMe.txt") + 2);
//char *strFile = (char*) GlobalLock
//(hGlobal);
//strcpy (strFile, "d:\\DragMe.txt");
//strFile [strlen ("d:\\DragMe.txt") +
//1] = NULL;
char filename[] = "d:\\DragMe.txt";

POINT point;
point.x = 480;
point.y = 480;

HGLOBAL hMem = GlobalAlloc(GHND, sizeof(DROPFILES) + strlen(filename)+2);

DROPFILES *dfiles = (DROPFILES*) GlobalLock(hMem);
if (!dfiles)
{
GlobalFree(hMem);
return NULL;
}

dfiles->pFiles = sizeof(DROPFILES);
dfiles->pt = point;
dfiles->fNC = TRUE;
dfiles->fWide = FALSE;
memcpy(&dfiles[1], filename, strlen(filename));
GlobalUnlock(hMem);

printf ("Sending Message...\n");

if (!PostMessage(hwnd, WM_DROPFILES, (WPARAM)hMem, 0)) {
printf("Error Posting Message!");
GlobalFree(hMem);
}
}

int temp = 0;
scanf("&d", temp);
return 0;
}
Posted
Updated 2-Feb-19 3:17am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It is all described here: WM_DROPFILES message - Windows applications | Microsoft Docs[^]. Note that this may be a waste of time if the receiving application does not handle the WM_DROPFILES message.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100