views:

133

answers:

3

I'm trying to get my C# form to be parented correctly in a third party app, I have the handle to the control that I would like my form parented to but just can't seem to get it to work.

alt text

I would like to create my form so that it is part of the MDIClient, handle 005E0ED6. Just like Window 01D7157D.

Is this possible? If so can it be done in C#?

+2  A: 

How have you tried doing it? Did you try SetParent? See the following StackOverflow question to see if it helps. http://stackoverflow.com/questions/170800/embedding-hwnd-into-external-process-using-setparent

Josh Einstein
Yes I have tried set Parent, but my form is created outside of the MDIClient and doesn't seem to want to be part of it.
Nathan W
Can you show the code?
Josh Einstein
Ahh I haven't seen that question, I have a look into it.
Nathan W
Thanks, doing the AttachThreadInput worked.
Nathan W
A: 

This code seems to work:

    [DllImport("user32.dll")]
    private static extern
        IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

    [DllImport("user32.dll")]
    private static extern
        IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);

        WinAPI.SetParent(this.Handle, otherappshandle);

        IntPtr otherprocessID = GetWindowThreadProcessId(otherappshandle, new IntPtr(0));
        IntPtr threadID = new IntPtr(AppDomain.GetCurrentThreadId());

        AttachThreadInput(threadID , otherprocessID , 1);
Nathan W
A: 

Good luck. I've gone down that road, and found that there's enough little irritating gotchas that I eventually gave up on it.

SetParent() and the like will get you part of the way there, but there's a bunch of little gotchas to watch as far as the overall system (message pump blocking etc.) that just make it a time sink.

With WinForms, especially, I'd highly recommend just running your UI in the main process (if you can), and if you want to isolate your processing in another process do that instead.

kyoryu