如何檢索所有打開的窗口的標題(包括Internet Explorer中的選項卡等窗口)?獲取所有打開的窗口的標題
回答
事情是這樣的:
using System.Diagnostics;
Process[] processlist = Process.GetProcesses();
foreach (Process process in processlist)
{
if (!String.IsNullOrEmpty(process.MainWindowTitle))
{
Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle);
}
}
不能理解它,我afirde。你能給我一個例子嗎? –
僅鏈接答案不是答案。 – zzzzBov
http://pinvoke.net/default.aspx/user32.EnumDesktopWindows
有使用USER.DLL的示例EnumWindow在C#中列出所有打開的窗口。
鏈接已損壞! –
http://pinvoke.net/default.aspx/user32.EnumDesktopWindows – 62071072SP
基於以前的答案,給我一些錯誤,finaly我用這個代碼GetOpenedWindows
功能:
public class InfoWindow
{
public IntPtr Handle = IntPtr.Zero;
public FileInfo File = new FileInfo(Application.ExecutablePath);
public string Title = Application.ProductName;
public override string ToString() {
return File.Name + "\t>\t" + Title;
}
}//CLASS
/// <summary>Contains functionality to get info on the open windows.</summary>
public static class RuningWindows
{
internal static event EventHandler WindowActivatedChanged;
internal static Timer TimerWatcher = new Timer();
internal static InfoWindow WindowActive = new InfoWindow();
internal static void DoStartWatcher() {
TimerWatcher.Interval = 500;
TimerWatcher.Tick += TimerWatcher_Tick;
TimerWatcher.Start();
}
/// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary>
/// <returns>A dictionary that contains the handle and title of all the open windows.</returns>
public static IDictionary<IntPtr , InfoWindow> GetOpenedWindows()
{
IntPtr shellWindow = GetShellWindow();
Dictionary<IntPtr , InfoWindow> windows = new Dictionary<IntPtr , InfoWindow>();
EnumWindows(new EnumWindowsProc(delegate(IntPtr hWnd , int lParam) {
if (hWnd == shellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
StringBuilder builder = new StringBuilder(length);
GetWindowText(hWnd , builder , length + 1);
var info = new InfoWindow();
info.Handle = hWnd;
info.File = new FileInfo(GetProcessPath(hWnd));
info.Title = builder.ToString();
windows[hWnd] = info;
return true;
}) , 0);
return windows;
}
private delegate bool EnumWindowsProc(IntPtr hWnd , int lParam);
public static string GetProcessPath(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd , out pid);
if (hwnd != IntPtr.Zero) {
if (pid != 0) {
var process = Process.GetProcessById((int) pid);
if (process != null) {
return process.MainModule.FileName.ToString();
}
}
}
return "";
}
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc , int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr hWnd , StringBuilder lpString , int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
//WARN: Only for "Any CPU":
[DllImport("user32.dll" , CharSet = CharSet.Auto , SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle , out uint processId);
static void TimerWatcher_Tick(object sender , EventArgs e)
{
var windowActive = new InfoWindow();
windowActive.Handle = GetForegroundWindow();
string path = GetProcessPath(windowActive.Handle);
if (string.IsNullOrEmpty(path)) return;
windowActive.File = new FileInfo(path);
int length = GetWindowTextLength(windowActive.Handle);
if (length == 0) return;
StringBuilder builder = new StringBuilder(length);
GetWindowText(windowActive.Handle , builder , length + 1);
windowActive.Title = builder.ToString();
if (windowActive.ToString() != WindowActive.ToString()) {
//fire:
WindowActive = windowActive;
if (WindowActivatedChanged != null) WindowActivatedChanged(sender , e);
Console.WriteLine("Window: " + WindowActive.ToString());
}
}
}//CLASS
警告:你只能在「任何CPU」 compil /調試訪問到32位應用程序。 ..
Hera的函數UpdateWindowsList_WindowMenu(),你必須調用任何操作後Tabs.Here windowToolStripMenuItem是菜單項添加到窗口菜單菜單條。
public void UpdateWindowsList_WindowMenu()
{
//get all tab pages from tabControl1
TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
//get windowToolStripMenuItem drop down menu items count
int n = windowToolStripMenuItem.DropDownItems.Count;
//remove all menu items from of windowToolStripMenuItem
for (int i = n - 1; i >=2; i--)
{
windowToolStripMenuItem.DropDownItems.RemoveAt(i);
}
//read each tabpage from tabcoll and add each tabpage text to windowToolStripMenuItem
foreach (TabPage tabpage in tabcoll)
{
//create Toolstripmenuitem
ToolStripMenuItem menuitem = new ToolStripMenuItem();
String s = tabpage.Text;
menuitem.Text = s;
if (tabControl1.SelectedTab == tabpage)
{
menuitem.Checked = true;
}
else
{
menuitem.Checked = false;
}
//add menuitem to windowToolStripMenuItem
windowToolStripMenuItem.DropDownItems.Add(menuitem);
//add click events to each added menuitem
menuitem.Click += new System.EventHandler(WindowListEvent_Click);
}
}
private void WindowListEvent_Click(object sender, EventArgs e)
{
//casting ToolStripMenuItem to ToolStripItem
ToolStripItem toolstripitem = (ToolStripItem)sender;
//create collection of tabs of tabContro1
//check every tab text is equal to clicked menuitem then select the tab
TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
foreach (TabPage tb in tabcoll)
{
if (toolstripitem.Text == tb.Text)
{
tabControl1.SelectedTab = tb;
//call UpdateWindowsList_WindowMenu() to perform changes on menuitems
UpdateWindowsList_WindowMenu();
}
}
}
下面是一些代碼,你可以用它來獲取所有打開的窗口的列表。實際上,你會得到一個字典,其中每個項目都是一個KeyValuePair,其中的關鍵是窗口句柄(hWnd),值是它的標題。
using HWND = IntPtr;
/// <summary>Contains functionality to get all the open windows.</summary>
public static class OpenWindowGetter
{
/// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary>
/// <returns>A dictionary that contains the handle and title of all the open windows.</returns>
public static IDictionary<HWND, string> GetOpenWindows()
{
HWND shellWindow = GetShellWindow();
Dictionary<HWND, string> windows = new Dictionary<HWND, string>();
EnumWindows(delegate(HWND hWnd, int lParam)
{
if (hWnd == shellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
StringBuilder builder = new StringBuilder(length);
GetWindowText(hWnd, builder, length + 1);
windows[hWnd] = builder.ToString();
return true;
}, 0);
return windows;
}
private delegate bool EnumWindowsProc(HWND hWnd, int lParam);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(HWND hWnd);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(HWND hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
}
這裏是一個使用它的一些代碼:
foreach(KeyValuePair<IntPtr, string> window in OpenWindowGetter.GetOpenWindows())
{
IntPtr handle = window.Key;
string title = window.Value;
Console.WriteLine("{0}: {1}", handle, title);
}
以這種方式,他將只能得到每一個進程的主窗口
嘗試使用此代碼,必須將第一行更改爲'using HWND = System.IntPtr;'而不是 –
- 1. Qt - 獲取所有打開的窗口標題
- 2. C#獲取所有打開的窗口標題
- 3. 獲取標題打開窗口並關閉特定標題?
- 4. 如何獲取由彈出窗口打開的窗口的標題?
- 5. 獲取所有打開的窗口使用AutoIt的列表
- 6. 獲取MacOS上所有打開的窗口的PID
- 7. 如何獲取所有已打開的子窗口的引用
- 8. 使用Javascript獲取爲域名打開的所有窗口
- 9. 獲取所有打開窗口的位置和大小
- 10. 獲取列表中所有打開的MDI窗口
- 11. 最小化所有打開的窗口?
- 12. 獲取打開的窗口可可
- 13. 獲取打開的窗口列表C#
- 14. 獲取窗口標題?
- 15. 獲取所有chrome窗口
- 16. 在mac上的java中獲取打開窗口的座標
- 17. 所有窗口在QT4中單獨打開的問題
- 18. 有打開的窗口
- 19. Selenium - 如何獲取窗口中打開的標籤數量?
- 20. 獲取所有打開的資源管理器窗口的報告
- 21. 包含所有打開的窗口的窗口標題/句柄對的二維數組
- 22. 獲取Qt中的所有窗口
- 23. 獲取窗口的所有句柄
- 24. 如何在javascript中打開沒有標題欄的新窗口
- 25. 打開窗口並獲取其URL?
- 26. 提取窗口的所有子窗口
- 27. 獲取所有擁有任務欄圖標的窗口
- 28. 獲取與我的窗口重疊的所有窗口
- 29. 如何從CLI中獲取KDE中所有打開的窗口列表?
- 30. 窗口打開問題
,他要求在所有打開的窗口系統。 –
他說:「更像所有打開的應用程序的名稱」。 – CodeCaster
大衛皮拉斯是正確的,我需要所有打開的窗戶。像Intrarnt攻擊者中的標籤。任何方式?再次感謝! –