回答
這需要一個鉤子,通過調用SetWindowsHookEx,WH_SHELL鉤型集。這種鉤子類型需要一個可以注入進程的DLL,這種DLL不能用C#編寫。 64位操作系統和UAC也會產生很多障礙。 This project可以提供幫助。
我從來沒有使用它,看起來很有希望。你必須嘗試。 – 2012-01-07 22:15:27
這是一個老的文章,但使用來自.NET掛鉤時,我已經成功了過去用它:
雖然你肯定可以掛鉤程序,但對於你想要做的事情來說,這確實看起來過度。這段代碼應該讓你非常接近(你必須做一些其他的事情來查詢它的當前目錄的Windows資源管理器進程,我沒有這樣做)。此外,我的需求與您的需求不同,因爲我的展示不僅僅是您對Window
的定義(工具提示彈出窗口在技術上是'窗口')。 Explorer.exe,Chrome和Internet Explorer都可以有一個進程的多個窗口,這兩個窗口都關閉。根據您的需求,你可能想看看:
Enumerate windows like alt-tab does
EnumWindows filter to only windows that show up on alt-tab
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ProcessListModel processes = Program.GetProccesses();
foreach (ProcessModel process in processes.Items)
{
Console.WriteLine("Program: "
+ process.Filename
+ "[" + process.IsWindowsExplorer.ToString() + "]");
Console.WriteLine("- Window Text: " + process.WindowText);
}
Console.ReadKey();
}
[DllImport("user32")]
public static extern int EnumWindows(EnumWindowsDelegate CallBack,
ProcessListModel Processes);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowThreadProcessId(int hWnd,
out int lpdwProcessId);
[DllImport("user32")]
internal static extern int GetAncestor(int hwnd, int gaFlags);
[DllImport("user32")]
internal static extern int GetLastActivePopup(int hWnd);
[DllImport("user32")]
internal static extern bool IsWindowVisible(int hWnd);
[DllImport("User32.Dll")]
public static extern void GetWindowText(int h,
StringBuilder s,
int nMaxCount);
internal delegate bool EnumWindowsDelegate(int Hwnd,
ProcessListModel Processes);
internal static bool EnumWindowsCallBack(int Hwnd,
ProcessListModel Processes)
{
ProcessModel model = new ProcessModel();
// Visible != Minimized (I think)
if (!IsWindowVisible(Hwnd))
return true;
// Can the Window be shown by using alt-tab
if (IsAltTabWindow(Hwnd))
{
model.WindowsHandle = Hwnd;
try
{
int pid = 0;
Program.GetWindowThreadProcessId(Hwnd, out pid);
if (pid > 0)
{
try
{
model.ProcessID = pid;
Process p = Process.GetProcessById(pid);
if (p != null)
{
string filename = p.MainModule.FileName;
filename = System.IO.Path.GetFileName(filename);
model.Filename = filename;
StringBuilder windowText = new StringBuilder(256);
Program.GetWindowText(Hwnd, windowText, 256);
model.WindowText = windowText.ToString();
if (filename.Contains("explorer.exe"))
{
model.IsWindowsExplorer = true;
}
Processes.Items.Add(model);
}
}
// Do something or not,
// catch probably if window process
// is closed while querying info
catch { }
}
}
// Do something or not,
// catch probably if window process
// is closed while querying info
catch { }
}
return true;
}
internal static bool IsAltTabWindow(int hwnd)
{
// Start at the root owner
int hwndWalk = GetAncestor(hwnd, 3);
// See if we are the last active visible popup
int hwndTry;
while ((hwndTry = GetLastActivePopup(hwndWalk)) != hwndTry)
{
if (IsWindowVisible(hwndTry))
break;
hwndWalk = hwndTry;
}
return hwndWalk == hwnd;
}
public static ProcessListModel GetProccesses()
{
ProcessListModel processes = new ProcessListModel();
EnumWindowsDelegate enumCallback =
new EnumWindowsDelegate(EnumWindowsCallBack);
EnumWindows(enumCallback, processes);
return processes;
}
public class ProcessListModel
{
public ProcessListModel()
{
this.Items = new List<ProcessModel>();
}
public List<ProcessModel> Items { get; private set; }
}
public class ProcessModel
{
public int WindowsHandle { get; set; }
public int ProcessID { get; set; }
public string Filename { get; set; }
public bool IsWindowsExplorer { get; set; }
public string WindowText { get; set; }
}
}
}
輸出:(我有很多打開的窗口,這是其中的一小名單)
注意:這不枚舉子窗口,所以如果我在IE中打開了Internet Options
並關閉它,那將是一個子窗口,而不是進程主窗口。如果你需要兒童窗戶,那麼你必須以類似的方式實施EnumChildWindows function。
你要求的是實際上很難,沒有直接的方法去做你想要的東西。
更新,創建一個新的控制檯應用程序,粘貼代碼,完成。 – 2011-12-28 00:08:13
- 1. 關閉Windows窗體中的控件中的所有窗體
- 2. Windows Azure關閉
- 3. 使用UNIX中的命令關閉所有打開的X-windows
- 4. 如何在Windows關閉時關閉FiddlerCore
- 5. 關閉所有瀏覽器的Windows PowerShell中
- 6. 如何在Windows Phone 8中記錄所有異常?
- 7. 掛鉤並在Windows消息隊列中記錄「所有內容」
- 8. 運行第一條記錄後Windows服務關閉
- 9. 如何記錄Windows服務因系統關閉而終止?
- 10. 防止Windows關閉
- 11. 關閉windows box C#
- 12. Windows服務關閉
- 13. 關閉記錄中的paramiko
- 14. Java關閉Windows關機
- 15. 關閉具有線程的Windows服務
- 16. 如何在Windows 10下的PowerShell中阻止Windows關閉
- 17. 如何在Windows 7中關閉gvim中的所有字體提示?
- 18. Windows窗體關閉後不關閉?
- 19. Windows窗體:關閉,但不關閉
- 20. Windows 2000關閉程序關閉
- 21. 有沒有辦法在Windows 7/8中關閉Windows Display Driver Model(WDDM)?
- 22. 在windows中捕獲筆記本電腦蓋關閉事件?
- 23. 如何在windows phone中關閉屏幕?
- 24. 在Windows Azure中關閉特定實例
- 25. 如何在PowerShell中關閉Windows窗體?
- 26. 在Windows中查找未記錄的API
- 27. 記錄Windows服務
- 28. 爲什麼windows窗體沒有關閉
- 29. 關閉Windows服務中的子進程
- 30. 關閉Mathematica中的Windows進程
好吧,你想要什麼聽起來像不止一件事..結帳System.Diagnostics.Process和創建日誌還檢查JavaScript和日誌記錄奇怪你爲什麼甚至想記錄所有開始。查看查詢TaskManager提示..WMI可以做到這一點以及...祝你好運 – MethodMan 2011-12-27 19:26:37