2015-06-28 72 views
2

的標題我可以得到一個窗口的標題,它使用任務欄上會出現下面的代碼得到一個隱藏的窗口

foreach(Process p in Process.GetProcessesByName("processNameHere")) 
{ 
    string title = p.MainWindowTitle; 
} 

但如果Windows不能在takbask出現,串標題是空的。

所以我想問我怎樣才能得到沒有出現在任務欄上的窗口的標題?

+1

可能幫助http://stackoverflow.com/questions/2531828/how-to-enumerate-all-windows-belonging-to-a-particular-process-using-net – keyboardP

回答

1

爲此,我用我自己的Window類來處理這個問題,並加上一些額外的東西。

參見:

public class Window 
{ 
    public IntPtr Handle; 
    public string Title; 
    public int ProcessId; 
    public bool Visible 
    { 
     get 
     { 
      return IsWindowVisible(Handle); 
     } 
    } 
    public FormWindowState WindowState 
    { 
     get 
     { 
      WindowPlacement winp = new WindowPlacement(); 
      winp.Length = Marshal.SizeOf(winp); 
      GetWindowPlacement(Handle, ref winp); 
      return (FormWindowState)(winp.ShowCmd - 1); 
     } 
     set 
     { 
      ShowWindowAsync(Handle, (int)value + 1); 
     } 
    } 

    public Window(IntPtr handle) 
    { 
     Handle = handle; 
    } 
    public void Close() 
    { 
     PostMessage(Handle, 0x10, 0, 0); 
    } 
    public void SetTitle(string title) 
    { 
     SetWindowText(Handle, title); 
    } 

    public static Window[] GetAllWindows() 
    { 
     List<Window> list = new List<Window>(); 
     EnumWindows(delegate(IntPtr hWnd, int lParam) 
     { 
      try 
      { 
       StringBuilder title = new StringBuilder(256); 
       GetWindowText(hWnd, title, 256); 
       Window window = new Window(hWnd); 
       window.Title = title.ToString(); 
       GetWindowThreadProcessId(hWnd, out window.ProcessId); 
       lock (list) list.Add(window); 
      } 
      catch { } 
      return true; 
     }, 0); 
     return list.ToArray(); 
    } 

    private struct Rect 
    { 
     int Left, Top, Right, Bottom; 
    } 
    private struct Point 
    { 
     int X, Y; 
    } 
    private struct WindowPlacement 
    { 
     public int Length, Flags, ShowCmd; 
     public Point ptMinPosition, ptMaxPosition; 
     public Rect rcNormalPosition; 
    } 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 
    [DllImport("user32.dll", CharSet = CharSet.Ansi)] 
    private static extern bool SetWindowText(IntPtr hWnd, string strNewWindowName); 
    [DllImport("user32.dll")] 
    private static extern int EnumWindows(EnumWindowsProc ewp, int lParam); 
    private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam); 
    [DllImport("user32.dll")] 
    private static extern bool IsWindowVisible(IntPtr hWnd); 
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl); 
    [DllImport("user32.dll")] 
    private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 
    [DllImport("user32.Dll")] 
    private static extern int PostMessage(IntPtr hWnd, UInt32 msg, int wParam, int lParam); 
    [DllImport("user32.dll", SetLastError = true)] 
    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); 
} 
+0

非常感謝,解決了我的問題 –

+0

太好了。那麼請接受我的回答。 – bytecode77

+0

標記爲已回答 –