有没有办法将
FindWindowByWindowName()
与部分匹配(或通配符)一起使用?
我的窗口名称中有一个文件名,因此它可以根据打开的文件的名称而有所不同。
这将记录所有窗口的名称。
function EnumWindows(lpEnumFunc: LongWord; lParam: LongWord): Boolean;
external '[email protected] stdcall';
function GetWindowText(hWnd: HWND; lpString: string; nMaxCount: Integer): Integer;
external '[email protected] stdcall';
function EnumWindowsProc(hWnd: HWND; lParam: LongWord): Boolean;
var
Buf: string;
Len: Integer;
begin
Result := True;
SetLength(Buf, 256);
Len := GetWindowText(hWnd, Buf, 256);
SetLength(Buf, Len);
if Buf <> '' then
begin
Log(Buf);
end;
end;
procedure FindAllWindows;
begin
EnumWindows(CreateCallback(@EnumWindowsProc), 0);
end;
修改它以仅查找符合您条件的名称。
如果需要/适当,您可以使用
IsWindowVisible
仅过滤可见窗口。
基于:
https://github.com/KngStr/Inno-All-in-One-Setup/blob/master/Examples/CallbackCtrl/test2.iss
仅使用 Inno Setup 6 的新功能进行简化(特别是
CreateCallback
)
基于 Martin Prikryl 的其他答案的更具体答案是:
[Code]
{-------------------------------------------------------------------------}
type
EnumWindowsParam=record
hWnd : HWND;
Name : String;
end;
function EnumWindows(lpEnumFunc: LongWord; var lParam: EnumWindowsParam): Boolean;
external '[email protected] stdcall';
{-------------------------------------------------------------------------}
function GetWindowText(hWnd: HWND; lpString: string; nMaxCount: Integer): Integer;
external '[email protected] stdcall';
{-------------------------------------------------------------------------}
function IsWindowVisible(hWnd: HWND): Boolean;
external '[email protected] stdcall';
{-------------------------------------------------------------------------}
{ parameter must use lower case name for matching }
function EnumWindowsProc(hwnd: HWND; var lparam: EnumWindowsParam): Boolean;
var
windowname: string;
windownamelen: Integer;
begin
{ continue search }
Result := True;
{ ensure normal visible window }
if IsWindowVisible(hWnd) then
begin
{ setup buffer to get window name from - name truncated if too large for buffer }
SetLength(windowname, 256);
windownamelen := GetWindowText(hwnd, windowname, 256);
SetLength(windowname, windownamelen);
if windowname <> '' then
begin
{ work with lower case names for matching }
windowname:=lowercase(windowname);
if WildcardMatch(windowname, lparam.Name) then
begin
{ found item }
lparam.hWnd:=hwnd;
{ don't need to enumerate additional windows }
Result := False;
end;
end;
end;
end;
{-------------------------------------------------------------------------}
function FindWindowByWildcard(var windowname: string) : HWND;
var
enumresults : ENumWindowsParam;
begin
{ setup parameter structure }
enumresults.Name:=lowercase(windowname);
enumresults.hWnd:=0;
{ make call }
EnumWindows(CreateCallback(@EnumWindowsProc), enumresults);
{ provide result }
Result:=enumresults.hWnd;
end;