日期:2014-05-17 浏览次数:21252 次
uses ShlObj;
function ExecDropFile( // 模拟文件拖拽
AHandle: THandle; // 目标窗体句柄
AFileName: string // 文件名
): Boolean; // 返回执行是否成功
var
vDropFiles: TDropFiles;
vProcessId: DWORD;
vProcess: THandle;
vPointer: PChar;
vNumberOfBytesRead: Cardinal;
begin
Result := False;
if not FileExists(AFileName) or not IsWindow(AHandle) then Exit;
GetWindowThreadProcessId(AHandle, @vProcessId);
vProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or
PROCESS_VM_WRITE, False, vProcessId);
try
vPointer := VirtualAllocEx(vProcess, nil, 4096, MEM_RESERVE or MEM_COMMIT,
PAGE_READWRITE);
try
FillChar(vDropFiles, SizeOf(vDropFiles), 0);
vDropFiles.pFiles := SizeOf(TDropFiles);
WriteProcessMemory(vProcess, vPointer,
@vDropFiles, SizeOf(vDropFiles), vNumberOfBytesRead);
WriteProcessMemory(vProcess, vPointer + SizeOf(vDropFiles),
PChar(AFileName), Length(AFileName) + 1, vNumberOfBytesRead);
SendMessage(AHandle, WM_DROPFILES, Integer(vPointer), 0);
finally
VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
end;
finally
CloseHandle(vProcess);
end;
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ExecDropFile(FindWindow('Notepad', nil), 'c:/temp/temp.txt');
end;
//SendMessage(
// (HWND) hWndControl, // handle to destination control
// (UINT) WM_DROPFILES, // message ID
// (WPARAM) wParam, // = (WPARAM) (HDROP) hDrop;
// (LPARAM) lParam // = 0; not used, must be zero
// );