//取控件值并修改的几个函数-----------------------------------------------
function SplitString(Source, Deli: string ): TStringList;stdcall;
var
EndOfCurrentString: byte;
StringList:TStringList;
begin
StringList:=TStringList.Create;
while Pos(Deli, Source)>0 do
begin
EndOfCurrentString := Pos(Deli, Source);
StringList.add(Copy(Source, 1, EndOfCurrentString - 1));
Source := Copy(Source, EndOfCurrentString + length(Deli), length(Source) - EndOfCurrentString);
end;
Result := StringList;
StringList.Add(source);
end;
// 获得窗口文本
function GetWndText(hWnd: HWND): String;
Var
Ret:LongInt;
mText
Char;
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 SetWndText(hWnd: HWND; Text: String);
Var
//Ret:LongInt;
mText
Char;
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;
// 取得窗口句柄
function GetSoftWnd(str:string): HWND;
var
hCurrentWindow: HWnd;
WndText:String;
begin
hCurrentWindow := GetWindow(Application.Handle, GW_HWNDFIRST);
while hCurrentWindow <> 0 do
begin
WndText:=GetWndText(hCurrentWindow);
if Pos(str,WndText)>0 then
begin
Result:=hCurrentWindow;
Exit;
end;
hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
Result:=0;
end;
//------取控件值并修改的几个函数结束----------------------------------------------
//具体这么用
procedure TForm1.Button1Click(Sender: TObject);
function EnumChildWindowsProc(hwnd: Integer): Boolean; stdcall;
var
buffer: array[0..255] of Char;
begin
Result := True;
GetClassName(hwnd,buffer,256); //得到类名
//if ComNum=61 then SetWndText(hwnd,'奶奶的');
Form1.Memo2.Lines.add(inttostr(ComNum)+' ◆ '+GetWndText(hwnd)+' ◆ '+buffer);//加到Memo显示
inc(ComNum);
end;
var
hwnd: Integer;
//buffer: Array[0..1023] of Char;
begin
ComNum:=0;
Memo2.Clear;
hwnd := GetSoftWnd(edit4.text); //得到目标窗口句柄
if hwnd<>0 then
EnumChildWindows(hwnd,@EnumChildWindowsProc,Integer(@hwnd));
ComNum:=0;
end;