Yana Tovpienetz
Yana Tovpienetz

Reputation: 125

run another application within a C# program with his childs

I use a sample code to hunt process to my windows form application from specific List

void processStartEvent_EventArrived(object sender, EventArrivedEventArgs e)
{
    string processName = e.NewEvent.Properties["ProcessName"].Value.ToString();
    int processID = Convert.ToInt32(e.NewEvent.Properties["ProcessID"].Value);

    if (_processNames.Contains(processName))
    {
        Process proc = Process.GetProcessById(processID);
        if (GlobalVar.SourceWinForm.InvokeRequired)
        {

            GlobalVar.SourceWinForm.Invoke(new MethodInvoker(delegate { ProcessHandler.SetParent(proc.MainWindowHandle, GlobalVar.SourceWinForm.Handle); }));
        }
        else
        {
            ProcessHandler.SetParent(proc.MainWindowHandle, GlobalVar.SourceWinForm.Handle);
        }
    }

}

as you can see i use the function :

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hwc, IntPtr hwp);

everything work good except one thing. for example i hunted notepad application into my app. so it really give me the notepad into my app window but the problem start when i press for example in the notepad "Format -> Font" it open a new sub window of notepad , this sub window , my apllication is not father of this sub window.

how i can hunt the full process ? include his child (subs) windows ?

Upvotes: 1

Views: 183

Answers (2)

user2831449
user2831449

Reputation:

you can use:

    [DllImport("user32.dll")]
    private static extern
        bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern
        bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    private static extern
        bool IsIconic(IntPtr hWnd);

and then

            // bring it to the foreground
            if (IsIconic(proc.MainWindowHandle))
                ShowWindowAsync(proc.MainWindowHandle, SW_RESTORE);
            SetForegroundWindow(proc.MainWindowHandle);

Upvotes: 1

Yana Tovpienetz
Yana Tovpienetz

Reputation: 125

Solution: first of all i want to say the comments are correct and can help to someone else so please read the comments to my main question first.

but if you have specific scenario and you don't have any other option and you must use SetParent or other function like that. ( make sure you read first about SetParent and you understand what it dose )

the solution is to take the entire forms to foreground:

bool SetForegroundWindow(IntPtr hWnd);

this make all the other forms that inherit from the main form you loaded to see it on your main application ( this in case you application block and streach entire screen )

Upvotes: 0

Related Questions