加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 04:02:36 所属栏目:Windows 来源:网络整理
导读:我有一个问题:我只需要在列表中获取所有窗口的标题,通过标题我的意思是“记事本”,“总指挥官” – 只是窗口顶部边缘显示的文字. 到目前为止,我已经到了这里 function EnumWindowProc(hHwnd: HWND; lParam : integer): boolean; stdcall;var pPid : DWORD;
我有一个问题:我只需要在列表中获取所有窗口的标题,通过标题我的意思是“记事本”,“总指挥官” – 只是窗口顶部边缘显示的文字.

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

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;

但是,它返回各种各样的“窗口”,不同的形式焦点等.我怎样才能得到“主要”的?

以下是结果示例:

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

先感谢您!

解决方法

重要信息包含在 MSDN topic describing the taskbar中.基本上,您需要枚举顶级窗口并选择那些可见,无主并且具有WS_EX_APPWINDOW窗口样式的窗口.

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

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读