windows 如何获取当前正在运行的实际窗口的标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5951631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:49:40  来源:igfitidea点击:

How to get captions of actual windows currently running?

windowsdelphilistcaptions

提问by Martin Melka

I have a question: I simply need to get captions of all windows in a list, by captions I mean the "Notepad", "Total Commander" - simply the text shown in the top-edge of a window.

我有一个问题:我只需要获取列表中所有窗口的标题,标题我的意思是“记事本”、“总指挥官”——只是显示在窗口顶部边缘的文本。

So far I've gotten here

到目前为止,我已经到了这里

function EnumWindowProc(hHwnd: HWND; lParam : integer): boolean; stdcall;
var
  pPid : DWORD;
  title, ClassName : string;
begin
  if (hHwnd=NULL) then
  begin
    result := false;
  end
  else
  begin
    GetWindowThreadProcessId(hHwnd,pPid);
    SetLength(ClassName, 255);
    SetLength(ClassName,
              GetClassName(hHwnd,
                           PChar(className),
                           Length(className)));
    SetLength(title, 255);
    SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
    OptionsForm.ListBox1.Items.Add(title);
    OptionsForm.Memo1.Lines.Add
      ('Class Name = ' + className +
       '; Title = ' + title +
       '; HWND = ' + IntToStr(hHwnd) +
       '; Pid = ' + IntToStr(pPid));
    Result := true;
  end;
end;

But well, it returns all kinds of "windows", different focuses of forms etc. How can I get only the "main" ones?

但是,它返回各种“窗口”,不同的表单焦点等。我怎么能只得到“主要”的?

Here is a sample of the results:

以下是结果示例:

Class Name = Shell_TrayWnd; Title = ; HWND = 65898; Pid = 3776
Class Name = CiceroUIWndFrame; Title = CiceroUIWndFrame; HWND = 65976; Pid = 3776
Class Name = THelpInsightWindowImpl; Title = HelpInsightWindow; HWND = 1577734; Pid = 4852
Class Name = THelpInsightWindowImpl; Title = HelpInsightWindow; HWND = 591660; Pid = 4852
Class Name = TTokenWindow; Title = CodeParamWindow; HWND = 985436; Pid = 4852
Class Name = TaskSwitcherWnd; Title = P?epínání úloh; HWND = 66824; Pid = 3776
Class Name = tooltips_class32; Title = ; HWND = 198982; Pid = 1768
Class Name = tooltips_class32; Title = ; HWND = 66046; Pid = 3776
Class Name = _SearchEditBoxFakeWindow; Title = ; HWND = 66024; Pid = 3776
Class Name = tooltips_class32; Title = ; HWND = 66008; Pid = 3776
Class Name = tooltips_class32; Title = ; HWND = 131538; Pid = 3776
Class Name = Desktop User Picture; Title = Magicmaster; HWND = 65982; Pid = 3776
Class Name = DV2ControlHost; Title = Nabídka Start; HWND = 65978; Pid = 3776
Class Name = tooltips_class32; Title = ; HWND = 327840; Pid = 1768
Class Name = tooltips_class32; Title = ; HWND = 460808; Pid = 1768
Class Name = CTSCTooltip; Title = ; HWND = 266710; Pid = 2792
Class Name = Auto-Suggest Dropdown; Title = ; HWND = 69884; Pid = 4732
Class Name = Auto-Suggest Dropdown; Title = ; HWND = 69802; Pid = 4732
Class Name = TaskbarNotifierClass; Title = DAP Message Center; HWND = 68924; Pid = 4732
Class Name = tooltips_class32; Title = ; HWND = 134356; Pid = 1992
Class Name = ATKOSD; Title = ATKOSD; HWND = 65884; Pid = 3636

Thank you in advance!

先感谢您!

回答by David Heffernan

The important information is contained in the MSDN topic describing the taskbar. Essentially you need to enumerate the top-level windows and pick out those that are visible, unowned and have the WS_EX_APPWINDOWwindow style.

重要信息包含在描述任务栏MSDN 主题中。本质上,您需要枚举顶级窗口并挑选出那些可见的、无主的和具有WS_EX_APPWINDOW窗口样式的窗口。

This program shows you how it is done:

该程序向您展示了它是如何完成的:

program EnumTaskbarWindows;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
var
  s: string;
  IsVisible, IsOwned, IsAppWindow: Boolean;
begin
  Result := True;//carry on enumerating

  IsVisible := IsWindowVisible(hwnd);
  if not IsVisible then
    exit;

  IsOwned := GetWindow(hwnd, GW_OWNER)<>0;
  if IsOwned then
    exit;

  IsAppWindow := GetWindowLongPtr(hwnd, GWL_STYLE) and WS_EX_APPWINDOW<>0;
  if not IsAppWindow then
    exit;

  SetLength(s, GetWindowTextLength(hwnd));
  GetWindowText(hwnd, PChar(s), Length(s)+1);
  Writeln(s);
end;

begin
  EnumWindows(@EnumWindowsProc, 0);
end.

回答by MSalters

You want to check the properties of these windows. For instance, exclude any window that's not visible. You do this with GetWindowInfo. The properties to look for: no caption (missing WS_CAPTIONin dwStyle), or not visible (WS_VISIBLEflag). You might also want to check for windows that are moved off screen, but that's slightly tricky (there could be multiple monitors, even at negative offsets)

您想检查这些窗口的属性。例如,排除任何不可见的窗口。你用GetWindowInfo. 要查找的属性:无标题(WS_CAPTION在 中丢失dwStyle)或不可见(WS_VISIBLE标志)。您可能还想检查移出屏幕的窗口,但这有点棘手(可能有多个监视器,即使在负偏移处)