日期:2014-05-17  浏览次数:21215 次

c#模拟拖拽文件到窗体的功能
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;


已经在百度上找了半天了没找到c#的例子,只有c++的 压根看不懂。再就是上面这段代码,
目的是想模拟拖拽一张图片到qq上,让它发出去,在stackflow上看到人家说用Pinvoke调用SendMessage来模拟拖拽,可是SendMessage的第三个参数不知如何构造。
MSDN上SendMessage的原型如下:
//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 

          //  );

能否帮忙解答下,或者把那段代码改写成c#版本的,谢谢!!!