PWindowInfo = ^TWindowInfo;
TWindowInfo = record
Handle:THandle;
IsVCLWindow:Boolean;
VCLObject:TWinControl;
WndClass:string;
WndText:string;
end;
const
MAX_WND_CLASS = 256;
MAX_WND_TEXT = 1024;
function IsVCLWindow(aWnd:THandle;var aObj:TWinControl):Boolean;
var
sWndAtom,sCtrlAtom:string;
CM_GET_OBJECT:Cardinal;
ThreadID,ProcID:Cardinal;
begin
Result:=False;
ThreadID:=GetWindowThreadProcessId(aWnd,ProcID);
if ThreadID=0 then
Exit;
sWndAtom := Format('Delphi%.8X',[ProcID]);
sCtrlAtom := Format('ControlOfs%.8X%.8X', [HInstance, ThreadID]);
CM_GET_OBJECT := RegisterWindowMessage(PChar(sCtrlAtom));
aObj := Pointer(GetProp(aWnd, MakeIntAtom(GlobalFindAtom(PChar(sCtrlAtom)))));
if (aObj = nil) and (CM_GET_OBJECT<>0) then
aObj:=Pointer(SendMessage(aWnd, CM_GET_OBJECT, 0, 0));
Result:=(aObj<>nil);
if GetCurrentProcessId<>ProcID then
aObj:=nil;
end;
function ClassNameOfWindow(aWnd:THandle):string;
begin
SetLength(Result,MAX_WND_CLASS+1);
SetLength(Result,RealGetWindowClass(aWnd,PAnsiChar(Result),MAX_WND_CLASS+1));
end;
function TextOfWindow(aWnd:THandle):string;
begin
SetLength(Result,MAX_WND_TEXT+1);
SetLength(Result,GetWindowText(aWnd,PAnsiChar(Result),MAX_WND_TEXT+1));
end;
function GetControlAtCursor(var wi:TWindowInfo):Boolean;
var
cur,rel:TPoint;
hChild:THandle;
begin
Result:=GetCursorPos(cur);
if not Result then
Exit;
hChild:=WindowFromPoint(cur);
if hChild=0 then
Exit;
while hChild<>0do
begin
rel:=cur;
ScreenToClient(hChild,rel);
wi.Handle:=hChild;
hChild:=ChildWindowFromPoint(hChild,rel);
if hChild=wi.Handle then
Break;
end;
wi.WndClass:=ClassNameOfWindow(wi.Handle);
wi.WndText:=TextOfWindow(wi.Handle);
wi.IsVCLWindow:=IsVCLWindow(wi.Handle,wi.VCLObject);
end;