Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. run .exe file in a panel

run .exe file in a panel

Scheduled Pinned Locked Moved C#
csharpquestionvisual-studiotutorial
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pancakeleh
    wrote on last edited by
    #1

    How to place a .exe file into the panel in visual studio windows form application c#. using this

    System.Diagnostics.Process.Start("calc.exe");

    the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?

    A OriginalGriffO D RaviBeeR P 5 Replies Last reply
    0
    • P pancakeleh

      How to place a .exe file into the panel in visual studio windows form application c#. using this

      System.Diagnostics.Process.Start("calc.exe");

      the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Using the SetParent API might help you achieve this.

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

      void LoadProcessInPanel
      {
      ...
      Process process = Process.Start("calc.exe");
      SetParent(process.MainWindowHandle, myPanel.Handle);
      ...
      }

      OriginalGriffO 1 Reply Last reply
      0
      • P pancakeleh

        How to place a .exe file into the panel in visual studio windows form application c#. using this

        System.Diagnostics.Process.Start("calc.exe");

        the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        The solution Abhinav gave you will not work as is: you need to wait for the window to be fully loaded before you set the new value:

                Process process = Process.Start("calc.exe");
                Thread.Sleep(500);
                SetParent(process.MainWindowHandle, myPanel.Handle);
        

        You will also need to move it to a "sensible" location within your panel after you display it.

        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);
        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
        private const short SWP_NOMOVE = 0X2;
        private const short SWP_NOSIZE = 1;
        private const short SWP_NOZORDER = 0X4;
        private const int SWP_SHOWWINDOW = 0x0040;

        private void myButton_Click(object sender, EventArgs e)
        {
        Process process = Process.Start("calc.exe");
        Thread.Sleep(500);
        IntPtr processHandle = process.MainWindowHandle;
        SetParent(processHandle, myPanel.Handle);
        SetWindowPos(processHandle, 0, 0, 0, myPanel.Bounds.Width, myPanel.Bounds.Height, SWP_NOZORDER | SWP_SHOWWINDOW);
        }

        You might want to look at this: Window Tabifier[^] - it's a bit more complex that you appear to need, but it does everything you want to!

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        D 1 Reply Last reply
        0
        • A Abhinav S

          Using the SetParent API might help you achieve this.

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

          void LoadProcessInPanel
          {
          ...
          Process process = Process.Start("calc.exe");
          SetParent(process.MainWindowHandle, myPanel.Handle);
          ...
          }

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Unless Calc.exe starts a lot quicker than it does on my PC, you need a delay before you change the parent - it won't work until the window is fully constructed. :laugh:

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • P pancakeleh

            How to place a .exe file into the panel in visual studio windows form application c#. using this

            System.Diagnostics.Process.Start("calc.exe");

            the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            I've just been playing around with this as your question interested me. There's a bit more to just setting the parent handle. This is what I came up with after a little research and testing, it will still need further work but it's a start:

            using System;
            using System.Runtime.InteropServices;

            internal static class NativeMethods
            {
            public const int GWL_STYLE = -16;
            public const int WS_VISIBLE = 0x10000000;

            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms633530(v=vs.85).aspx
            /\*
            

            BOOL WINAPI IsWindowVisible(
            __in HWND hWnd
            );
            */
            [DllImport("User32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool IsWindowVisible(IntPtr hWnd);

            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms633534(v=vs.85).aspx
            /\*
            

            BOOL WINAPI MoveWindow(
            __in HWND hWnd,
            __in int X,
            __in int Y,
            __in int nWidth,
            __in int nHeight,
            __in BOOL bRepaint
            );
            */
            [DllImport("User32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms633541(v=vs.85).aspx
            /\*
            

            HWND WINAPI SetParent(
            __in HWND hWndChild,
            __in_opt HWND hWndNewParent
            );
            */
            [DllImport("User32.dll")]
            public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx
            /\*
            

            LONG WINAPI SetWindowLong(
            __in HWND hWnd,
            __in int nIndex,
            __in LONG dwNewLong
            );
            */
            [DllImport("User32.dll")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
            }

            using System;
            using System.Diagnostics;
            using System.Drawing;
            using System.Windows.Forms;

            internal class ProcessWrapper
            {
            private Process process = null;

            public ProcessWrapper(Process process)
            {
                this.process = process;
            }
            
            public void Close()
            {
                if (process != null)
                    process.CloseMainWindow();
            }
            public void PlaceInControl(Control control)
            {
                if (control != null && process != null)
                {
                    while (
                        process.MainWindowHandle == IntPtr.Zero || 
                        !NativeMethods.IsWindowVisible(process.MainWindowHandle))
                    {
                        process.Refresh();
                    }
                    process.WaitForInputIdle();
            
            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              The solution Abhinav gave you will not work as is: you need to wait for the window to be fully loaded before you set the new value:

                      Process process = Process.Start("calc.exe");
                      Thread.Sleep(500);
                      SetParent(process.MainWindowHandle, myPanel.Handle);
              

              You will also need to move it to a "sensible" location within your panel after you display it.

              [DllImport("user32.dll")]
              static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);
              [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
              public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
              private const short SWP_NOMOVE = 0X2;
              private const short SWP_NOSIZE = 1;
              private const short SWP_NOZORDER = 0X4;
              private const int SWP_SHOWWINDOW = 0x0040;

              private void myButton_Click(object sender, EventArgs e)
              {
              Process process = Process.Start("calc.exe");
              Thread.Sleep(500);
              IntPtr processHandle = process.MainWindowHandle;
              SetParent(processHandle, myPanel.Handle);
              SetWindowPos(processHandle, 0, 0, 0, myPanel.Bounds.Width, myPanel.Bounds.Height, SWP_NOZORDER | SWP_SHOWWINDOW);
              }

              You might want to look at this: Window Tabifier[^] - it's a bit more complex that you appear to need, but it does everything you want to!

              Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              You can use the IsWindowVisible[^] function with a while loop instead of sleeping the thread

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              OriginalGriffO 1 Reply Last reply
              0
              • D DaveyM69

                You can use the IsWindowVisible[^] function with a while loop instead of sleeping the thread

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                :doh: I need another coffee...

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • P pancakeleh

                  How to place a .exe file into the panel in visual studio windows form application c#. using this

                  System.Diagnostics.Process.Start("calc.exe");

                  the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?

                  RaviBeeR Offline
                  RaviBeeR Offline
                  RaviBee
                  wrote on last edited by
                  #8

                  See this[^] article. /ravi

                  My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                  1 Reply Last reply
                  0
                  • P pancakeleh

                    How to place a .exe file into the panel in visual studio windows form application c#. using this

                    System.Diagnostics.Process.Start("calc.exe");

                    the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?

                    P Offline
                    P Offline
                    pancakeleh
                    wrote on last edited by
                    #9

                    i have tried out and it works! but right now i am facing another problem! how do i adjust the panel width and height? I fixed my panel on the top left hand side with a width of 400px and height 400px. however after the button is being clicked, the exe appear but the position of the panel shifted. how do i ensure that the panel stays where it was as of before? and also before the exe file is being displayed in the panel, it will pop up the exe first. how can i do it in such a way that it wont pop up and will be displayed immediately in the panel?

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups