c# Esecuzione di un programma interno alla finestra

Stato
Discussione chiusa ad ulteriori risposte.

InVeRsIoNe

Nuovo Utente
25
1
Salve grazie per l'aiuto ho questo problem il programma che ho creato mi funziona perfettamente ma non come vorrei , vorrei aprire in contemporanea più programmi pero appena apro un altro mi scompare l'altro , esempio apro paint , poi click per apri il notepad e mi scompare paint

host è un WindowsFormsHost

C#:
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;

namespace Arca
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

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

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

        private Process pDocked;
        private IntPtr hWndOriginalParent;
        private IntPtr hWndDocked;
        public System.Windows.Forms.Panel pannel;
        public System.Windows.Forms.Panel pannel_2;

        public MainWindow()
        {
            InitializeComponent();

            MediaElement_1.Height = Screen.PrimaryScreen.Bounds.Height;
            MediaElement_1.Play();

                /*
                Bitmap myBitmap = new Bitmap();
                Graphics g = Graphics.FromImage(myBitmap);

                System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);

                g.DrawRectangle(myPen, new System.Drawing.Rectangle(0, 300, 400, 400));
                g.DrawImage(myBitmap, 1, 1);*/

        }
       
        private void Window_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            Window window = (Window)sender;
            window.Topmost = true;
        }


        private void Button_Home_Click(object sender, RoutedEventArgs e)
        {
            Cambia_Pagina(0);
        }

        private void Button_Dash_Click(object sender, RoutedEventArgs e)
        {
            Cambia_Pagina(1);
        }

        private void Button_Mondo_Click(object sender, RoutedEventArgs e)
        {
            Cambia_Pagina(2);
        }

        private void Button_Manuale_Click(object sender, RoutedEventArgs e)
        {
            Cambia_Pagina(4);
        }

        private void Button_Mappa_Click(object sender, RoutedEventArgs e)
        {
            Cambia_Pagina(3);
        }

        private void Button_Play_Click(object sender, RoutedEventArgs e)
        {

            pannel = new System.Windows.Forms.Panel();
            host.Child = pannel;

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "notepad.exe";
            //proc.StartInfo.Arguments = "D:/(C)/neruale.py";
            proc.Start();

         
            proc.WaitForInputIdle();
            while (proc.MainWindowHandle == IntPtr.Zero)
            {
                Thread.Sleep(100); // Don't hog the CPU
                proc.Refresh(); // You need this since `MainWindowHandle` is cached
            }
            SetParent(proc.MainWindowHandle, pannel.Handle);

            /*
            Process p = Process.Start("notepad.exe");
            p.WaitForInputIdle();
            while (p.MainWindowHandle == IntPtr.Zero)
            {
                Thread.Sleep(100); // Don't hog the CPU
                p.Refresh(); // You need this since `MainWindowHandle` is cached
            }
            SetParent(p.MainWindowHandle, pannel.Handle);
            */

        }
       
        private void Button_Image_Click(object sender, RoutedEventArgs e)
        {
            //pannelo = new System.Windows.Forms.Panel();
            host.Child = pannel;

            System.Diagnostics.Process proco = new System.Diagnostics.Process();
            proco.EnableRaisingEvents = false;
            proco.StartInfo.FileName = "C:/Program Files/Internet Explorer/iexplore.exe";
            proco.StartInfo.Arguments = "www.google.it";
            proco.Start();


            proco.WaitForInputIdle();
            while (proco.MainWindowHandle == IntPtr.Zero)
            {
                Thread.Sleep(100); // Don't hog the CPU
                proco.Refresh(); // You need this since `MainWindowHandle` is cached
            }
            SetParent(proco.MainWindowHandle, pannel.Handle);


            /*
            pannel = new System.Windows.Forms.Panel();
            host.Child = pannel;
            dockIt("mspaint.exe");*/
        }
       
        private void Button_Paint_Click(object sender, RoutedEventArgs e)
        {

            pannel = new System.Windows.Forms.Panel();
            host.Child = pannel;

            System.Diagnostics.Process proc_1 = new System.Diagnostics.Process();
            proc_1.EnableRaisingEvents = false;
            proc_1.StartInfo.FileName = "mspaint.exe";
            proc_1.Start();


            proc_1.WaitForInputIdle();
            while (proc_1.MainWindowHandle == IntPtr.Zero)
            {
                Thread.Sleep(100); // Don't hog the CPU
                proc_1.Refresh(); // You need this since `MainWindowHandle` is cached
            }
            SetParent(proc_1.MainWindowHandle, pannel.Handle);

            /*System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
            pProcess.StartInfo.FileName = "C:/Users/Salvatore/AppData/Local/Programs/Python/Python37/python.exe";
            pProcess.StartInfo.Arguments = "D:/(C)/neruale.py"; //argument
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
            pProcess.Start();
           
            pProcess.WaitForExit();*/

        }

        private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Info_Galleria.Source = null;
        }

        private void ListBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox.Items.Clear();
            if (ListBox.SelectedIndex == 1)
                ListBox.Items.Add("1111");
            if (ListBox.SelectedIndex == 8)
                ListBox.Items.Add("Crediti");
        }

        private void Button_CMD_Click(object sender, RoutedEventArgs e)
        {

            pannel = new System.Windows.Forms.Panel();
            host.Child = pannel;

            Process pp = Process.Start("cmd.exe");
            //pp.WaitForInputIdle();
            SetParent(pp.MainWindowHandle, pannel.Handle);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            pannel_2 = new System.Windows.Forms.Panel();
            host.Child = pannel_2;

            System.Diagnostics.Process proc_2 = new System.Diagnostics.Process();
            proc_2.EnableRaisingEvents = false;
            proc_2.StartInfo.FileName = "notepad.exe";
            proc_2.Start();


            proc_2.WaitForInputIdle();
            while (proc_2.MainWindowHandle == IntPtr.Zero)
            {
                Thread.Sleep(100); // Don't hog the CPU
                proc_2.Refresh(); // You need this since `MainWindowHandle` is cached
            }
            SetParent(proc_2.MainWindowHandle, pannel_2.Handle);
        }
        // ----- END -----
    }
}
341913
 
Ultima modifica:

InVeRsIoNe

Nuovo Utente
25
1
ho letto ma non ho capito come potrei integrarlo nel mio codice , potresti farmi un esempio ? grazie :D ho ascoltato il tuo consiglio del immagine ;D
 

Mursey

Super Moderatore
Staff Forum
Utente Èlite
8,219
5,655
ho letto ma non ho capito come potrei integrarlo nel mio codice , potresti farmi un esempio ? grazie :D ho ascoltato il tuo consiglio del immagine ;D
Ci sarebbe da fare qualche prova con panel.Controls.Add(<control>);
Vedo che tu crei un processo e poi associ come parent l'handle del panel, se invece del panel semplice associassi un control figlio aggiunto con l'istruzione sopra potrebbe funzionare? E' solo un ipotesi.
Altrimenti dobbiamo trovare un'alternativa al host che stai usando...
 

Mursey

Super Moderatore
Staff Forum
Utente Èlite
8,219
5,655
Ho provato a ricostruire il tuo progetto, non sarà proprio la stessa cosa ma qualcosa ho combinato.
Prova a instanziare il Panel solo una volta, quando lo dichiari
C#:
public System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
Poi in ogni handler (Button_Play_Click, Button_Image_Click e Button_Paint_Click) assegnarlo al child e basta
C#:
host.Child = panel;

Spero possa essere un buon spunto di aiuto!
Ovviamente è solo un suggerimento, se volessi seguire questa strada devi gestire i child e non aggiungerli sempre ad ogni click...
 
Ultima modifica:

InVeRsIoNe

Nuovo Utente
25
1
come faccio a gestirli e aggiungerli ? , voglio semplicemente che rimango entrane le finestre accese se mi consigli qualche altro codice che funziona mi fai un esempio perché seno non capisco scusa il disturbo
 
Ultima modifica:

Mursey

Super Moderatore
Staff Forum
Utente Èlite
8,219
5,655
come faccio a gestirli e aggiungerli ? , voglio semplicemente che rimango entrane le finestre accese se mi consigli qualche altro codice che funziona mi fai un esempio perché seno non capisco scusa il disturbo
Hai provato la mia modifica?
Funziona?

Poi vediamo come procedere ma almeno verifichiamo che può essere una strada.
 
Stato
Discussione chiusa ad ulteriori risposte.

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!