The usage is as follows:
nParentHandle: HWnd;
nChildHandle: HWnd;
nParentHandle := FindWindow(nil, ''Notepad'');
if nParentHandle <> 0 then
nChildHandle := FindChildWindow(nParentHandle, ''SomeChildEditsClassName'');
------ Function code ------
var
hwndFindChildWindow : HWND;
function EnumWindowsForFindChildWindowProc(WHandle: HWND; lParam: LPARAM): BOOL; export; stdcall;
const
MAX_WINDOW_NAME_LEN = 80;
var
sTargetClassName: string;
nHandle: HWnd;
sCurrClassName: string;
bResult: Boolean;
Begin
if (hwndFindChildWindow <> 0) then
exit;
sTargetClassName := PChar(lParam);
sCurrClassName := GetWindowClass(WHandle);
bResult := CompareText(sCurrClassName, sTargetClassName) = 0;
If (bResult) then
hwndFindChildWindow := WHandle
else
FindChildWindow(WHandle, PChar(lParam));
end;
function FindChildWindow(hwndParent: HWnd; ClassName: PChar) : HWnd;
Begin
try
EnumChildWindows(hwndParent, @EnumWindowsForFindChildWindowProc, LongInt(PChar(ClassName)));
Results := hwndFindChildWindow;
except
on Exception do
Results := 0;
end;
end;
//Return to the current form with focus
function GetFocusedWindowFromParent(ParentWnd:HWnd):HWnd;
var
OtherThread,
Buffer : DWord;
idCurrThread: DWord;
Begin
OtherThread := GetWindowThreadProcessID(ParentWnd, @Buffer);
idCurrThread := GetCurrentThreadID;
if AttachThreadInput(idCurrThread, OtherThread, true) then begin
Result := GetFocus;
AttachThreadInput(idCurrThread, OtherThread, false);
end
else
Result:= GetFocus;
end;
//Get the subform that currently gets focus, even if it is the form of other applications
function GetFocusedChildWindow: HWnd;
Begin
Result := GetFocusedWindowFromParent(GetForegroundWindow);
end;
//Get the text of the form
function EIGetWinText(nHandle: Integer): string;
var
pcText: array[0..32768] of char;
Begin
SendMessage(nHandle, WM_GETTEXT, 32768, LongInt(@pcText));
Results := pcText;
end;
//Set the text of the form
procedure EISetWinText(nHandle: Integer; const sNewText: string);
Begin
SendMessage(nHandle, WM_SETTEXT, Length(sNewText), LongInt(PChar(Trim(sNewText))));
end;
//Return the form's class name
function EIGetWindowClass(const nHandle: HWnd): string;
var
szClassName: array[0..255] of char;
Begin
GetClassName(nHandle, szClassName, 255);
Result := szClassName;
end;