API - findchildwindow e findwindow |
Top Previous Next |
// Pelo que entendi, e não testei... este fonte ensina a fusar o bendito // FindChildWindow que tras o Handle de componentes dentro de programas
nParentHandle: HWnd; nChildHandle : HWnd;
nParentHandle := FindWindow(nil, 'Notepad'); if nParentHandle <> 0 then nChildHandle := FindChildWindow(nParentHandle, 'SomeChildEditsClassName');
conceptually could find an edit control down in the Windows hiearchy in Notepad... This sample is totally made up... But you probably caught that already ;-)
------listing begins here------
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))); Result := hwndFindChildWindow; except on Exception do Result := 0; end; end;
// Returns the handle to the currently focused window in // an application. You could use this function with the // 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;
// Returns the handle to the currently focused window, even // if it's in another application... function GetFocusedChildWindow: HWnd; begin Result := GetFocusedWindowFromParent(GetForegroundWindow); end;
// Returns the text of a window as a string. function EIGetWinText(nHandle: Integer): string; var pcText: array[0..32768] of char; begin SendMessage(nHandle, WM_GETTEXT, 32768, LongInt(@pcText)); Result := pcText; end;
// Sets the text of a window as a string. procedure EISetWinText(nHandle: Integer; const sNewText: string); begin SendMessage(nHandle, WM_SETTEXT, Length(sNewText), LongInt(PChar(Trim(sNewText)))); end;
// Returns the windows class of a wnidow as a string. function EIGetWindowClass(const nHandle: HWnd): string; var szClassName: array[0..255] of char; begin GetClassName(nHandle, szClassName, 255); Result := szClassName; end; |