C# get parent process from window handle

家住魔仙堡 提交于 2019-12-11 10:37:12

问题


I have a C# windows application and ultimately launches a dialog from an interop component. The problem is that this dialog window sometimes appears behind the c# application's main window, forcing a user to alt-tab to find it.

I've put measures into place to find this dialog window and bring it forward...

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
}

...but 'finding' the dialog is problematic as the dialog's className and form title changes.

However, the dialog window's parent process (Task Manager>Go To Process) is the same as the current process. So to correctly 'find' this dialog window, my aim is to enumerate through all the windows, find the parent processID and compare against CurrentProcess.

Is there a way to obtain the overall parent process from a window handle?


回答1:


GetWindowThreadProccessId will do the work.




回答2:


Try hiding your application window prior to opening the dialog. That way it won't appear behind it.



来源:https://stackoverflow.com/questions/12597599/c-sharp-get-parent-process-from-window-handle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!