Làm cách nào tôi có thể truy xuất các tiêu đề của tất cả các cửa sổ đang mở (bao gồm các cửa sổ như các tab trong Internet Explorer)?lấy tiêu đề của tất cả các cửa sổ đang mở
Trả lời
Something như thế này:
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);
}
}
bạn nên sử dụng các API EnumWindow.
có rất nhiều ví dụ về cách sử dụng nó từ C#, tôi thấy một cái gì đó ở đây:
Không thể hiểu được nó. bạn có thể cho tôi một exmple, plesae? –
Câu trả lời chỉ có liên kết không phải là câu trả lời. Liên kết – zzzzBov
http://pinvoke.net/default.aspx/user32.EnumDesktopWindows
Có một ví dụ của việc sử dụng của user.dll EnumWindow trong C# để liệt kê tất cả các cửa sổ đang mở.
bị hỏng! –
http://pinvoke.net/default.aspx/user32.EnumDesktopWindows – 62071072SP
Dựa trên các câu trả lời trước đó cho tôi một số lỗi, cuối cùng tôi sử dụng mã này với GetOpenedWindows
chức năng:
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
Cảnh báo: Bạn có thể chỉ compil/debug dưới "Bất kỳ CPU" để truy cập vào 32bits Apps. ..
Hàm Hera UpdateWindowsList_WindowMenu() mà bạn phải gọi sau khi bất kỳ thao tác nào được thực hiện trên tab.Here windowToolStripMenuItem là mục menu được thêm vào Trình đơn cửa sổ vào dải menu.
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();
}
}
}
Dưới đây là một số mã bạn có thể sử dụng để có danh sách tất cả các cửa sổ đang mở. Trên thực tế, bạn nhận được một từ điển mà mỗi mục là một KeyValuePair trong đó khóa là chốt (hWnd) của cửa sổ và giá trị là tiêu đề của nó.
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();
}
Và đây là một số mã có sử dụng nó:
foreach(KeyValuePair<IntPtr, string> window in OpenWindowGetter.GetOpenWindows())
{
IntPtr handle = window.Key;
string title = window.Value;
Console.WriteLine("{0}: {1}", handle, title);
}
Đã cố sử dụng mã này, phải thay đổi dòng đầu tiên thành 'using HWND = System.IntPtr;' thay thế –
- 1. Gửi tin nhắn tới tất cả các cửa sổ/tab đang mở sử dụng JavaScript
- 2. Nhận danh sách tất cả các cửa sổ đang mở bằng cách sử dụng AutoIt
- 3. Lệnh Vim để thoát khỏi tất cả các cửa sổ đang mở là gì?
- 4. Giảm thiểu tất cả các cửa sổ đang mở trong C#
- 5. Lấy tên của cửa sổ đang hoạt động
- 6. Xử lý WM_NCACTIVATE trong cửa sổ khởi động chặn tất cả các cửa sổ khác
- 7. Lấy tiêu đề của Cửa sổ/Tài liệu hiện tại đang hoạt động trong Mac OS X
- 8. Mở cửa sổ pyglet mà không lấy nét
- 9. Tiêu đề chung cho cửa sổ vẽ
- 10. Chuyển đổi giữa các cửa sổ đang mở trong Pharo
- 11. Toàn màn hình Python TKinter hoặc cửa sổ wxPython, nhưng 'ở phía dưới cùng' của tất cả các cửa sổ?
- 12. Autohotkey: lấy danh sách cửa sổ có tiêu đề nhất định
- 13. HttpClient truy xuất tất cả các tiêu đề
- 14. Nhận tiêu đề của cửa sổ hiện tại đang hoạt động trong Java
- 15. Làm thế nào để có được tiêu đề của cửa sổ đang hoạt động với AutoHotKey?
- 16. Danh sách tất cả các tệp đang mở
- 17. Đóng tất cả các tệp đang mở trong ipython
- 18. Cách lấy tiêu điểm cửa sổ/tab như cảnh báo()?
- 19. Làm thế nào để bạn lấy tiêu đề của cửa sổ thiết bị lô?
- 20. Leaflet.js: Mở tất cả các cửa sổ bật lên khi tải trang
- 21. hàng loạt kịch bản để đóng tất cả các cửa sổ Prompt mở Command
- 22. XmlHttpRequest getAllResponseHeaders() không trả về tất cả các tiêu đề
- 23. cách lấy danh sách tất cả các tệp đang mở trong trình chỉnh sửa nhật thực?
- 24. Lấy danh sách tất cả các chủ đề hiện đang chạy trong Java
- 25. Liệt kê tất cả các xử lý tệp đang mở?
- 26. Cửa sổ phương thức WPF bằng cách sử dụng ShowDialog() Chặn tất cả các cửa sổ khác
- 27. Ẩn tất cả các biểu mẫu đang mở
- 28. Đóng tất cả các hộp thoại đang mở? (JQuery)
- 29. C#: Đang đợi tất cả các chủ đề hoàn thành
- 30. Làm cách nào để đóng tất cả các cửa sổ trong tab hiện tại của vim
theo cách này anh sẽ chỉ nhận được cửa sổ chính của mỗi quá trình, ông yêu cầu tất cả các cửa sổ đang mở trong hệ thống. –
Ông nói: "giống như tên của tất cả các ứng dụng đang mở". – CodeCaster
Davide piras là đúng, tôi cần tất cả các cửa sổ đang mở. như các tab trong trình khai thác Intrarnt. Dù sao? Cảm ơn một lần nữa! –