输入框中传字串且触发它的确定按纽:
思路是得到这两个的句柄,再发送相关消息:
以下可以参考一下:
//发送文字到QQ发送窗口,并使QQ窗口自动发送
procedure TForm1.QQSend;
var WndText:string;
HWinHand:HWnd;//发送消息窗口的句柄
HSendBtn:HWnd;//发送按钮的句柄
HSendText:HWnd;//发送文本框的句柄
i:integer;
begin
HWinHand:=GetWindow(Application.Handle, GW_HWNDFIRST);
while HWinHand<>0 do
begin
WndText:=GetWndText(HWinHand);
if (Pos('聊天中',WndText)>0) or (Pos('发送消息',WndText)>0) then
begin
HSendBtn:=GetDlgItem(HWinHand,1);//得到发送按钮的句柄
HSendText:=GetWindow(GetDlgItem(HWinHand,0),GW_CHILD);//得到消息文本框的句柄
i:=Random(10);
SetWndText(HSendText,say[i]);
SendMessage(HSendBtn,WM_LBUTTONDOWN ,MK_LBUTTON,0);
SendMessage(HSendBtn,WM_LBUTTONUP,0,0);
//exit;
end;
HWinHand:= GetWindow(HWinHand,GW_HWNDNEXT);
end;
end;
//得出窗口文本
function TForm1.GetWndText(hWnd: HWND): String;
Var
Ret:LongInt;
mText:PChar;
Buf:Integer;
begin
Ret:=SendMessage(hWnd,WM_GETTEXTLENGTH,0,0)+1;
GetMem(mText,Ret);
try
Buf:=LongInt(mText);
SendMessage(hWnd,WM_GETTEXT,Ret,Buf);
Result:=StrPas(mText);
finally
FreeMem(mText,Ret);
end;
end;
// 发送文本到窗口
procedure TForm1.SetWndText(hWnd: HWND; Text: String);
Var
mText:PChar;
Buf:Integer;
begin
GetMem(mText,Length(Text));
StrCopy(mText,PChar(Text));
try
Buf:=LongInt(mText);
SendMessage(hWnd,WM_SETTEXT,0,Buf);
finally
FreeMem(mText,Length(Text));
end;
end;
if not CreateProcess(nil,pchar(cmdLine),nil,nil,false,CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo) then
MessageBox(Application.handle,'指定程序启动失败!','错误',MB_OK or MB_ICONSTOP)
else
begin
WaitForSingleObject(pInfo.hProcess,INFINITE);
GetExitCodeProcess(pInfo.hProcess,exitCode);
end;