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,
(UINT) WM_DROPFILES,
(WPARAM) wParam,
(LPARAM) lParam
);
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;
}