Simulate the FindWindow function in the Windows API in Kylix

zhaozj2021-02-17  47

Simulate the FindWindow function in the Windows API in Kylix

(Translator Note: Because I haven't used Kylix, I have a lot of mistakes in the first translation of Kylix. If my translation adds difficulties to your reading, please refer to the original text, Deep apologeta. Slightly deleted this time)

Summary: This article describes a very convenient FindWindow function. Author: Matthias Thoma.

There is no FindWindow function similar to the Windows API in the XWindow interface function library XLIB. However, the name of getting a window with a known ID is possible, and it is very easy. This will use the XFETCHNAME function, the XFETCHNAME prototype is:

Function XFETCHNAME (Display: pdisplay; w: twindow; windownamereturn: ppchar): TSTATUS; CDECL

Amazing step is how to get the ID of each window. To do this, you need an XQueryTree function in Xlib, which retrieves a list of specified windows, gets a list of sub-windows, roots, parent windows, and sub-windows. Joint these two functions and a standard lookup algorithm, you get a function that has the same functionality with Windows's FindWindow.

The prototype of XQueryTree is: function xquerytree (Display: pdisplay; w: twindow; rootreturn: PWindow; ParentReturn: PWindow; _pa5: ppwindow; nchildrenreturn: pcardinal: tstatus;

The Following Code Shows How To Do It. It is a kylix translation of a public domain source by brian paul. The following code demonstrates how to complete this feature.

{FINDWINDOW function

INPUT: DPY - THE X DISPLAY SCR - THE X Screen Number START - Where start search, usually root window Name -}}}

Function FindWindow (Display: pdisplay; screen: integer; start: twindow; name: pchar): twindow; type achildren = array [0..0] of window; pchildren = ^ achildren;

Var Stat: TSTATUS; N: INTEGER; Num: cardinal; W: WINDOW; ROOT: Window; Parent: Window; children: pchildren; title: pchar;

Begin if (Display, Start, @title = 1) THEN Begin IF (Strcmp (Name, Title) = 0) THEN Begin Xfree (Title); Result: = Start; EXIT; End; Xfree (Title); ;

Stat: = xQuerytree (Display, Start, @Root, @parent, @children, @num);

IF (stat = 1) Then Begin {search Each child window for a match:} for n: = Num-1 Downto 0 Do Begin IF (Xfetchname (Display, Start, @title) = 1) THEN Begin IF (Strcmp (Name , Title) = 0) Then Begin {FOUND IT} Xfree (Title); Result: = Start; EXIT; End; Xfree (Title); end; end; {search the descendents of each child for a match:}

For N: = NUM-1 Downto 0 Do Begin W: = FindWindow (Display, Screen, Children ^ [N], Name); if (W <> 0) THEN Begin Xfree (Children); Result: = W; END;

IF (Children <> NIL) THEN BEGIN XFree (Children); end;

RESULT: = 0;

If Kylix is ​​running, you can find this function:

Function iskylixrunning: boolean; begin result: = FindWindow (qtdisplay, xdefaultscreen (qtdisplay), xdefaultrootwindow (qtdisplay), 'Kylix') <> 0;

Maybe you want some ifdef, now delphi is a cross-platform: function isdelphirunning: boolean; begin result: = false;

{$ IFDEF Linux} Result: = FindWindow (qtdisplay, xdefaultscreen (qtdisplay), XDefaultrootwindow (QTDisplay), 'Kylix') <> 0; {$ ENDIF}

{$ IFDEF WIN32} Result: = FINDWINDOW ('Tappbuilder', NIL) <> 0; {$ ENDIF} END;

转载请注明原文地址:https://www.9cbs.com/read-29743.html

New Post(0)