Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8608229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:33:15+00:00 2026-06-12T03:33:15+00:00

Intention Using the following code, I managed to load some applications in my windows

  • 0

Intention

Using the following code, I managed to load some applications in my windows form.

Code

What this function does is…

  • stating a process
  • embedding the process into a panel of my form
  • maximizing the embedded process
  • adding a resize event handler to the panel to update the size of the embedded process on panel resize
  • adding a closed event handler to the form to terminate the embedded process on form close

Usings

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

Constants

const int   GWL_STYLE   = -16;
const long  WS_VISIBLE  = 0x10000000,
            WS_MAXIMIZE = 0x01000000,
            WS_BORDER   = 0x00800000,
            WS_CHILD    = 0x40000000;

Function

IntPtr LoadExtern(Control Panel, string Path)
{
    try
    {
        Process Process = Process.Start(Path);
        Process.WaitForInputIdle();
        IntPtr Handle = Process.MainWindowHandle;
        SetParent(Handle, Panel.Handle);
        SetWindowLong(Handle, GWL_STYLE, (int)(WS_VISIBLE+(WS_MAXIMIZE|WS_BORDER)));

        MoveWindow(Handle, 0, 0, Panel.Width, Panel.Height, true);

        Panel.Resize += new EventHandler(
             delegate(object sender, EventArgs e)
             {
                  MoveWindow(Handle, 0, 0, Panel.Width, Panel.Height, true);
             }
        );

        this.FormClosed += new FormClosedEventHandler(
             delegate(object sender, FormClosedEventArgs e) {
                  SendMessage(Handle, 83, 0, 0);
                  Thread.Sleep(1000);
                  Handle = IntPtr.Zero;
             }
        );

        return Handle;
    }
    catch (Exception e) { MessageBox.Show(this, e.Message, "Error"); }
    return new IntPtr();
}

DLL Imports

[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr Handle, int x, int y, int w, int h, bool repaint);

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr Handle, int Msg, int wParam, int lParam);

Result

This code works nice with some applications, like the windows notepad. Notepad is started and included in the panel of my form. There is no caption and the are no borders, as it should be.

LoadExtern(panel1, "notepad.exe");

After closing the form the embedded process gets terminated like expected.

Problem

Unfortunately my code doesn’t work for some other (bigger) applications like firefox or sublimetext.

LoadExtern(panel2, @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe");

What happens is that my form starts and firefox starts, but in its own window. Could you help me to include sublimetext or firefox in my applications?

Part of the solution

Thanks to Sheng Jiang’s answers, I got it working for some more applications. What I did is to wait for a main window handle.

Process.WaitForInputIdle();
IntPtr Handle = new IntPtr();
for (int i = 0; Handle == IntPtr.Zero && i < 300; i++)
{
     Handle = Process.MainWindowHandle;
     Thread.Sleep(10);
}

But I still can’t embed applications like the windows explorer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:33:16+00:00Added an answer on June 12, 2026 at 3:33 am

    This code works for most applcations. I embedded the file explorer simply using a webbrowser control on my form and set its url to a file location. The internet explorer control magically turns into a file explorer then.

    This is my final code, feel free to use this for you own projects.

    IntPtr EmbedProcess(Control Panel, string Path)
    {
        string Name = NameFromPath(Path);
    
        foreach (Process Task in Process.GetProcesses())
        {
            if (NameFromPath(Task.ProcessName).Contains(Name))
            {
                try { Task.Kill(); }
                catch (Exception e) {  }
            }
        }
    
        try
        {
            Process Task = Process.Start(Path);
            Task.WaitForInputIdle();
            IntPtr Handle = new IntPtr();
            for (int i = 0; Handle == IntPtr.Zero && i < 10; i++) { Handle = Task.MainWindowHandle; Thread.Sleep(100); }
            SetParent(Handle, Panel.Handle);
            SetWindowLong(Handle, GWL_STYLE, (int)(WS_VISIBLE + (WS_MAXIMIZE | WS_BORDER)));
    
            MoveWindow(Handle, 0, 0, Panel.Width, Panel.Height, true);
    
            Panel.Resize += new EventHandler(delegate(object sender, EventArgs e) { MoveWindow(Handle, 0, 0, Panel.Width, Panel.Height, true); });
    
            this.FormClosed += new FormClosedEventHandler(delegate(object sender, FormClosedEventArgs e)
            {
                SendMessage(Handle, 83, 0, 0);
                Thread.Sleep(100);
                Handle = IntPtr.Zero;
            });
    
            return Handle;
        }
        catch (Exception e) { MessageBox.Show(this, e.Message, "Error"); }
        return new IntPtr();
    }
    

    I somebody is interested in the hole C# classes for embedding window processes and console processes into your form, check out this github repository.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: initialize: function() { _.bindAll(this); var callBack = function(res) {
Given the following code: QList<Vertex*> _vertices; //this gets filled //at some other point i
I have the following code. My intention that this custom object can be cast
I have the following code, with the intention of defining and using a list
I am using the following code to try and upload a NSManagedObject in the
I'm analysing a deadlock that's occurring when using a native library alongside managed code.
I'm playing around with the following code. The intention is to open a new
Can someone kindly enlighten me why the following code of mine does not work?
I am using following code to add tabs on a tabHost but crashes on
In my Python code I often find myself doing the following (using DB-API): yValues

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.