unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
function nGetChildHandle: Integer;
public
{ Public-Deklarationen }
end;
function EIFindChildWindow(hwndParent: HWnd; ClassName: PChar): HWnd;
function EIGetWindowClass(const nHandle: HWnd): string;
var
Form1: TForm1;
implementation
var
hwndFindChildWindow : HWND;
{$R *.dfm}
function EnumTextChildProc(AHandle: hWnd): BOOL; stdcall;
var
AText: array[0..255] of Char;
s: string;
begin
Result := True;
PostMessage(AHandle, WM_GETTEXT, Sizeof(AText), integer(@AText));
s := strpas(AText);
ShowMessage(s);
end;
function GetTextFromHandle(wndhandle: HWnd): string;
var
textlength: integer;
text: PChar;
begin
result := '';
if wndhandle = 0 then Exit;
textlength := SendMessage(wndhandle, WM_GETTEXTLENGTH, 0, 0);
if textlength <> 0 then
begin
GetMem(Text, textlength+1);
SendMessage(wndhandle, WM_GETTEXT, textlength+1, Integer(text));
Result := text;
FreeMem(text);
end;
end;
function EIGetWindowClass(const nHandle: HWnd): string;
var
szClassName: array[0..255] of char;
begin
GetClassName(nHandle, szClassName, 255);
Result := szClassName;
end;
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 := EIGetWindowClass(WHandle);
bResult := CompareText(sCurrClassName, sTargetClassName) = 0;
If (bResult) then
hwndFindChildWindow := WHandle
else
EIFindChildWindow(WHandle, PChar(lParam));
end;
function EIFindChildWindow(hwndParent: HWnd; ClassName: PChar) : HWnd;
begin
try
EnumChildWindows(hwndParent, @EnumWindowsForFindChildWindowProc, LongInt(PChar(ClassName)));
Result := hwndFindChildWindow;
except
on Exception do
Result := 0;
end;
end;
function TForm1.nGetChildHandle: Integer;
var
nParentHandle: HWnd;
nChildHandle: HWnd;
begin
//Find parent window
nParentHandle := FindWindow(nil, PChar('Your window title'));
if nParentHandle = 0 then
raise Exception.Create('Couldn''t find the main window !');
//Find child window (TEdit)
nChildHandle := EIFindChildWindow(nParentHandle, PChar('TEdit'));
if nChildHandle = 0 then
raise Exception.Create('Couldn''t find the child window !');
Result := nChildHandle;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TheChildHandle: hwnd;
begin
TheChildHandle:=nGetChildHandle;
end;
end.
Your example works ok for me when used as:
Open in new window
(i put edit and button component on form)http://borland.public.delp