{ *
* AControl: Control handle determined by Spy++ (e.g. 0037064A)
* ANewText: Text to assign to control
* AWinTitle: Window Title/Caption
* }
function ControlSetText(const AControl, ANewText, AWinTitle: string): boolean;
function EnumChildren(AWindowHandle: HWND; ALParam: lParam): bool; stdcall;
begin
ShowMessage(AControl); // if commented out - code works fine
TStrings(ALParam).Add(IntToStr(GetDlgCtrlID(AWindowHandle)));
Result := true;
end;
var
_MainWindowHandle: HWND;
_WindowControlList: TStringlist;
i: integer;
_ControlHandle: integer;
begin
Result := false;
_MainWindowHandle := FindWindow(nil, PWideChar(AWinTitle));
if _MainWindowHandle <> 0 then
begin
_WindowControlList := TStringlist.Create;
try
if TryStrToInt('$' + Trim(AControl), _ControlHandle) then
try
EnumChildWindows(_MainWindowHandle, @EnumChildren,
UINT_PTR(_WindowControlList));
for i := 0 to _WindowControlList.Count - 1 do
begin
if (StrToInt(_WindowControlList[i]) = _ControlHandle)
then
begin
SendMessage(StrToInt(_WindowControlList[i]), WM_SETTEXT, 0,
integer(PCHAR(ANewText)));
Result := true;
end;
end;
except
on E: Exception do
MessageDlg(E.Message, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0)
end;
finally
FreeAndNil(_WindowControlList);
end;
end;
end;
The debugger raises an exception with the message
--------------------------- Debugger Exception Notification ---------------------------
Project Default_project.exe raised exception class $C0000005 with message 'access violation at 0x00406fae: write of address 0x00408dbb'.
It breaks at:
for i := 0 to _WindowControlList.Count - 1 do
I call it like this:
ControlSetText('00070828', 'New TEdit text', 'Delphi_test_app');
I am planning an update, so, not only control handle could be passed, but also control type+identifier e.g. 'Edit1'.
EDIT:
What I am trying is to do is to implement http://www.autohotkey.com/docs/commands/ControlSetText.htm