2012-09-26 88 views
1

我有一個C#窗口應用程序,並最終從互操作組件中啓動對話框。 問題是,這個對話窗口有時出現在c#應用程序的主窗口後面,迫使用戶使用alt-tab來查找它。C#從窗口句柄獲取父進程

我已經把措施納入地方找到這個對話窗口,並把它向前......

private static extern bool SetForegroundWindow(IntPtr hWnd); 

public class SearchData 
{ 
    public string Wndclass; 
    public string Title; 
    public IntPtr hWnd; 
} 

private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ref SearchData data); 
private delegate bool EnumWindowsProc(IntPtr hWnd, ref SearchData data); 

public static bool EnumProc(IntPtr hWnd, ref SearchData data) 
{ 
    //Code to determine whether the window from handle hWnd is our target window. 
    //apply handle, title, class to data and halt the enumeration 
} 

...但「發現」對話是有問題的對話框的類名和窗體標題的變化。

但是,對話窗口的父進程(任務管理器>轉到進程)與當前進程相同。所以爲了正確'找到'這個對話窗口,我的目的是枚舉所有的窗口,找到父進程ID並與當前進程進行比較。

有沒有辦法從窗口句柄中獲得整個父進程?

回答

1

GetWindowThreadProccessId會做這項工作。

+0

非常好,謝謝。這個答案和這篇文章(http://stackoverflow.com/questions/2281429/how-to-enumerate-all-windows-within-a-process)導致我的解決方案。 – MoSlo

0

嘗試在打開對話框之前隱藏您的應用程序窗口。這樣它就不會出現在它後面。

+0

不是一個完整的解決方案,因爲對話框也可以出現在其他窗口的後面(例如Web瀏覽器等)。用戶會感到困惑,並從任務欄中選擇主應用程序,期望出現對話框。與此同時,它只埋在其他窗戶後面。這對於多屏幕顯示器尤其是一個問題。 – MoSlo