類比檔案拖拽到指定表單中。

來源:互聯網
上載者:User
如何類比檔案拖拽,這個問題得反過來分析:“如何接受檔案拖拽”?
分別要用到API:
DragAcceptFiles()函數,令表單接受檔案拖拽的訊息
WM_DROPFILES訊息檔案拖拽時接收

看來得向表單發送WM_DROPFILES訊息,發送訊息是SendMessage()函數
為節約時間我們要站在巨人的肩膀--搜尋
關鍵詞就基本確定為:“WM_DROPFILES SendMessage 類比檔案拖拽”
此處省略1000字

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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.