1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133:
| function RealWindowFromPoint(pt: TPoint{$IFDEF RWFPCHOICE}; swinvis: boolean = true{$ENDIF}): HWND;
type PCHILDS_ENUM = ^CHILDS_ENUM; CHILDS_ENUM = record nDiff: integer; hWndFound: HWND; pt: TPoint; {$IFDEF RWFPCHOICE} showinvis: boolean; {$ENDIF RWFPCHOICE} end;
var ce: CHILDS_ENUM;
function EnumProc(hwndChild: HWND; lParam: LPARAM): Boolean; stdcall; var rc: TRECT; begin GetWindowRect(hwndChild, rc);
with PCHILDS_ENUM(lParam)^, rc do {$IFDEF RWFPCHOICE} case showinvis of true: if (pt.x >= Left) and (pt.x < Right) and (pt.y >= Top) and (pt.y < Bottom) and (nDiff > (Right - Left) + (Bottom - Top)) then begin hWndFound := hwndChild; nDiff := (Right - Left) + (Bottom - Top); end; else if (pt.x >= Left) and (pt.x < Right) and (pt.y >= Top) and (pt.y < Bottom) and (nDiff > (Right - Left) + (Bottom - Top)) and IsWindowVisible(hwndChild) then begin hWndFound := hwndChild; nDiff := (Right - Left) + (Bottom - Top); end; end; {$ELSE RWFPCHOICE} if (pt.x >= Left) and (pt.x < Right) and (pt.y >= Top) and (pt.y < Bottom) and (nDiff > (Right - Left) + (Bottom - Top)) then begin hWndFound := hwndChild; nDiff := (Right - Left) + (Bottom - Top); end; {$ENDIF RWFPCHOICE} Result := True; end;
begin ce.nDiff := MAXLONG; ce.hWndFound := WindowFromPoint(pt); ce.pt.x := pt.x; ce.pt.y := pt.y; {$IFDEF RWFPCHOICE} ce.showinvis := swinvis; {$ENDIF RWFPCHOICE} if (ce.hWndFound <> 0) then begin if (GetWindowLong(ce.hWndFound, GWL_STYLE) and WS_CHILD <> 0) then ce.hwndFound := GetParent(ce.hwndFound); EnumChildWindows(ce.hWndFound, @EnumProc, Integer(@ce)); end; Result := ce.hwndFound; end;
function _GetWindowText(Wnd: HWND): string; var TextLn: Integer; StrBuf: PChar; begin Result := ''; TextLn := SendMessage(Wnd, WM_GETTEXTLENGTH, 0, 0); if TextLn = 0 then Exit; GetMem(StrBuf, TextLn + 1); try SendMessage(Wnd, WM_GETTEXT, TextLn + 1, Integer(StrBuf)); Result := StrBuf; finally FreeMem(StrBuf); end; end;
function _GetClassName(Handle: Hwnd): string; var ClassName: string; begin SetLength(className, MAX_PATH); SetLength(className, GetClassName(Handle, pchar(className), MAX_PATH)); Result := ClassName; end;
procedure TForm1.Timer1Timer(Sender: TObject); var C: Array[0..255] of Char; h: hWnd; p: TPoint; const {$j+} x: Integer= -1; y: Integer= -1; {$j-} begin p := Mouse.CursorPos; if (p.x <> x) and (p.y <> y) then begin h := RealWindowFromPoint(Mouse.CursorPos); x := p.x; y := p.y; caption := _GetClassName(h) + '-' +_GetWindowText(h); end; end; |