想取得另一个程序窗口中的若干个编辑框,并编辑解决方法
想取得另一个程序窗口中的若干个编辑框,并编辑
能够得到这些编辑框的句柄,当被编辑的程序每次启动后的编辑框句柄都不一样,
如何得到能够唯一标识这些编辑框的内容呢?
谢谢
------解决方案--------------------
每次窗口创建都会出现句柄变化,用“窗口”类、“窗口”名判断。
看好了,是窗口,不是窗体。TEDIT也属于窗口
------解决方案--------------------
程序里面控件的位置始终是固定的,记下坐标,可以根据那些坐标来得到编辑框。
p.x:=...
p.y:=...
h := WindowFromPoint(p);
------解决方案--------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
var
roothandle: THandle;
p: TPoint;
procedure addH(H: THandle; N: TTreeNode);
var
text, s: string;
newN: TTreeNode;
R: TRect;
begin
if h = 0 then
exit;
setlength(s, 255);
FillChar(s[1], 255, 0);
GetWindowText(H, @(s[1]), 255);
text := trim(s);
FillChar(s[1], 255, 0);
GetClassName(H, @(s[1]), 255);
text := text + ' ( ' + trim(s) + ') ';
s := ' ';
GetWindowRect(h, r);
s := Format( ' %d,%d,%d,%d ', [r.left, r.Top, r.right, r.Bottom]);
text := text + s;
newN := TreeView1.Items.AddChild(N, text);
h := GetWindow(h, GW_CHILD);
h := GetWindow(h, GW_HWNDFIRST);
addh(h, newN);
while h > 0 do
begin
h := GetWindow(h, GW_HWNDNEXT);
addh(h, newN);
end;
end;
begin
TreeView1.Items.Clear;
GetCursorPos(p);
roothandle := WindowFromPoint(p);
if roothandle = 0 then
exit;
addH(roothandle, nil);
TreeView1.FullExpand;
end;
end.
object Form1: TForm1
Left = 638
Top = 223
BorderStyle = bsToolWindow
Caption = 'Form1 '
ClientHeight = 255
ClientWidth = 284
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif '
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object TreeView1: TTreeView
Left = 0
Top = 0
Width = 284
Height = 255
Align = alClient
Indent = 19
TabOrder = 0
end
object Timer1: TTimer
OnTimer = Timer1Timer
Left = 568
Top = 120
end
end
能够得到这些编辑框的句柄,当被编辑的程序每次启动后的编辑框句柄都不一样,
如何得到能够唯一标识这些编辑框的内容呢?
谢谢
------解决方案--------------------
每次窗口创建都会出现句柄变化,用“窗口”类、“窗口”名判断。
看好了,是窗口,不是窗体。TEDIT也属于窗口
------解决方案--------------------
程序里面控件的位置始终是固定的,记下坐标,可以根据那些坐标来得到编辑框。
p.x:=...
p.y:=...
h := WindowFromPoint(p);
------解决方案--------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
var
roothandle: THandle;
p: TPoint;
procedure addH(H: THandle; N: TTreeNode);
var
text, s: string;
newN: TTreeNode;
R: TRect;
begin
if h = 0 then
exit;
setlength(s, 255);
FillChar(s[1], 255, 0);
GetWindowText(H, @(s[1]), 255);
text := trim(s);
FillChar(s[1], 255, 0);
GetClassName(H, @(s[1]), 255);
text := text + ' ( ' + trim(s) + ') ';
s := ' ';
GetWindowRect(h, r);
s := Format( ' %d,%d,%d,%d ', [r.left, r.Top, r.right, r.Bottom]);
text := text + s;
newN := TreeView1.Items.AddChild(N, text);
h := GetWindow(h, GW_CHILD);
h := GetWindow(h, GW_HWNDFIRST);
addh(h, newN);
while h > 0 do
begin
h := GetWindow(h, GW_HWNDNEXT);
addh(h, newN);
end;
end;
begin
TreeView1.Items.Clear;
GetCursorPos(p);
roothandle := WindowFromPoint(p);
if roothandle = 0 then
exit;
addH(roothandle, nil);
TreeView1.FullExpand;
end;
end.
object Form1: TForm1
Left = 638
Top = 223
BorderStyle = bsToolWindow
Caption = 'Form1 '
ClientHeight = 255
ClientWidth = 284
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif '
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object TreeView1: TTreeView
Left = 0
Top = 0
Width = 284
Height = 255
Align = alClient
Indent = 19
TabOrder = 0
end
object Timer1: TTimer
OnTimer = Timer1Timer
Left = 568
Top = 120
end
end