Loading application w/in MDI form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BC3Tech

    #1

    Loading application w/in MDI form

    I've searched around for this and have found some VB examples but 1)
    I'm not sure that they're VB.NET and 2) If they are VB.NET, then
    they're not working once ported to C#. I'll be doing this in .NET 2.0
    w/ VS.NET 2k5

    Below is my code:

    #region WinAPI Functions
    [DllImport("user 32.dll")]
    public static extern IntPtr GetParent(IntPt r hwnd);

    [DllImport("user 32.dll")]
    public static extern IntPtr SetParent(IntPt r hWndChild, IntPtr
    hWndNewParent);

    [DllImport("user 32.dll")]
    public static extern IntPtr FindWindow(stri ng lpClassName, string
    lpWindowName);

    [DllImport("user 32.dll")]
    public static extern IntPtr GetWindow(IntPt r hwnd, int wCmd);

    [DllImport("user 32.dll")]
    public static extern int GetWindowText(I ntPtr hwnd, string
    lpString, int cch);

    [DllImport("user 32.dll")]
    public static extern long GetWindowThread ProcessId(IntPt r hwnd,
    long lpdwProcessId);

    [DllImport("user 32.dll")]
    public static extern bool LockWindowUpdat e(IntPtr hwndLock);

    [DllImport("user 32.dll")]
    public static extern IntPtr GetDesktopWindo w();

    [DllImport("user 32.dll")]
    public static extern bool DestroyWindow(I ntPtr hwnd);

    [DllImport("user 32.dll", EntryPoint = "SetFocus")]
    public static extern IntPtr ApiSetFocus(Int Ptr hwnd);

    [DllImport("kern el32.dll")]
    public static extern bool TerminateProces s(IntPtr hProcess, int
    uExitCode);

    [DllImport("kern el32.dll")]
    public static extern IntPtr GetCurrentProce ss();
    #endregion

    Those functions were listed in a VB solution as the ones to use. Then
    for a btnLaunch in my MDI form:

    private void btnLaunch_Click (object sender, EventArgs e)
    {
    if (ofdApp.ShowDia log(this) == DialogResult.OK )
    {
    string appCommand = ofdApp.FileName ;

    m_process = new System.Diagnost ics.Process();
    m_process.Start Info = new
    System.Diagnost ics.ProcessStar tInfo(appComman d);
    Program.LockWin dowUpdate(Progr am.GetDesktopWi ndow());
    if (m_process.Star t())
    {
    Program.SetPare nt(m_process.Ha ndle, this.Handle);
    }
    Program.LockWin dowUpdate(this. Handle);
    }
    }


    If anybody's got an idea as to how to do this in C# 2.0 or has an
    example please let me know. Thanks!

  • BC3Tech

    #2
    Re: Loading application w/in MDI form

    I've been hacking away at this today and have made a little bit of
    progress...

    Here is what I have so far:

    private void btnLaunch_Click (object sender, EventArgs e)
    {
    if (ofdApp.ShowDia log(this) == DialogResult.OK )
    {
    Process m_proc = new Process();
    m_proc.StartInf o = new ProcessStartInf o(ofdApp.FileNa me);
    m_proc.StartInf o.Domain = AppDomain.Curre ntDomain.Friend lyName;
    m_proc.StartInf o.ErrorDialogPa rentHandle = this.Handle;
    m_proc.StartInf o.UseShellExecu te = true;
    m_proc.StartInf o.WindowStyle = ProcessWindowSt yle.Normal;

    if (m_proc.Start() )
    {
    m_proc.EnableRa isingEvents = true;
    m_proc.Exited += new EventHandler(pr oc_Exited);
    m_subProcesses. Add(m_proc);
    }

    btnSetParents.P erformClick();
    }
    }

    private void btnSetParents_C lick(object sender, EventArgs e)
    {
    Program.LockWin dowUpdate(Progr am.GetDesktopWi ndow());
    foreach (Process p in m_subProcesses)
    {
    Program.SetPare nt(p.MainWindow Handle, this.Handle);
    }

    Program.LockWin dowUpdate(IntPt r.Zero);
    }


    Now here's the weird part. This works just fine for putting cmd.exe and
    notepad.exe within my MDI form. However, when I try to run more
    "intesive" programs like a custom app we have (100k) or internet
    explorer, this won't function. But as soon as I put a thread.sleep as
    the first line in the btnSetParents, I can then load my custom app but
    still not internet explorer. If i set the sleep for even longer,
    eventually IE will get grabbed by this call. If this call is made
    automatically, and I then click the SetParents button on my form,
    nothing happens (I guess because the parent has already been set to my
    MDI form, but just didn't reflect the change).

    If there's a way to "know" when a System.Process is done loading and
    ready for the parent to be changed, that's essentially what I'm looking
    for.

    Thanks for any help!

    Comment

    Working...