2011-05-10 70 views
3

我有一個問題:我只需要獲得列表中所有窗口的標題,標題是我的意思是「記事本」,「總指揮官」 - 只是顯示在窗口頂部的文本。如何獲取當前正在運行的實際窗口的標題?

到目前爲止,我在這裏得到了

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 

預先感謝您!

+1

那麼,通過決定什麼「主」的意思開始。 – 2011-05-10 15:01:51

+0

Rob的權利。你在找什麼。作爲一個瘋狂的猜測,我懷疑你只需要在任務欄中有按鈕的窗口。 – 2011-05-10 15:13:28

+0

從[MSDN](http://msdn.microsoft.com/en-us/library/cc144179(VS.85).aspx#Managing_Taskbar_But):每當應用程序創建一個窗口時,Shell在任務欄上創建一個按鈕不擁有。爲確保窗口按鈕位於任務欄上,使用WS_EX_APPWINDOW擴展樣式創建一個無主窗口。 – 2011-05-10 15:19:38

回答

6

重要信息包含在MSDN topic describing the taskbar中。本質上,您需要枚舉頂層窗口並挑選那些可見,無主窗口並具有窗口樣式的窗口。

這個程序顯示了它是如何做:

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. 
+0

就是這樣。謝謝你的MSDN鏈接。我不知道窗戶的標準。 – 2011-05-11 12:10:50

+0

@David Heffernan - 快速瀏覽之後,我意識到不幸的是,您的代碼不會顯示某些窗口,例如BSPlayer和Kaspersky KIS和Delphi。 – Ampere 2013-08-10 16:54:08

+0

爲什麼不呢?調試器告訴你什麼? – 2013-08-10 16:54:37

1

你想檢查這些窗口的屬性。例如,排除任何不可見的窗口。你可以用GetWindowInfo來做到這一點。要查找的屬性:沒有標題(在dwStyle中缺少WS_CAPTION),或不可見(WS_VISIBLE標誌)。您可能還想檢查移出屏幕的窗口,但這有點棘手(即使在負偏移時也可能有多個監視器)

相關問題