C# hiding an executable's window when that executable starts to process. How to?

Hi there;

I am writing an C# application, and want to hide that executable's windows when that executable starts to process. How to do in C#?

Regards.
LVL 12
jazzIIIloveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
this.Visible = false;

where 'this' is the main window you want to hide.
0
RickCommented:
Set the form's opacity to 0
0
Naman GoelPrinciple Software engineerCommented:
Do something like this:
 
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form1 = new Form1();
            form1.Opacity = 0;
            
            Application.Run(form1);
            
        }
    }

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

NavneetSoftware EngineerCommented:
0
NavneetSoftware EngineerCommented:
Hi!

This worked for me

        public Form1()
        {
            InitializeComponent();
           
            Process[] processRunning = Process.GetProcesses();
            foreach (Process pr in processRunning)
            {
                if (pr.ProcessName == "WindowsFormsApplication1.vshost") // My ProcessName
                {
                    pr.WaitForExit();
                }
            }
        }

Open in new window


Thanks!
0
NavneetSoftware EngineerCommented:
        public Form1()
        {
            InitializeComponent();
           
            Process[] processRunning = Process.GetProcesses();
            foreach (Process pr in processRunning)
            {
                if (pr.ProcessName == "WindowsFormsApplication1.vshost")
                {
                    for (int i = 0; i < 5; i++)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    MessageBox.Show("hi");
                    pr.WaitForExit();
                }
            }
        }

Open in new window

0
jazzIIIloveAuthor Commented:
Ok with my C# code; want to hide an executable's window and that executable has no relation with C#. It has a window that i want to hide with my C# code.

I have seen that go4expert page but couldn't do it.

Kind regards.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Something like...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private const int SW_HIDE = 0;

        [DllImport("User32")] 
        private static extern int ShowWindow(IntPtr handle, int nCmdShow);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HideProcessWindows("Notepad"); // <-- No Path & No Extension!
        }

        private void HideProcessWindows(string ProcessName)
        {
            foreach (Process p in Process.GetProcessesByName(ProcessName)) 
            {
                ShowWindow(p.MainWindowHandle, SW_HIDE);
            } 
        }

    }
}

Open in new window

0
AndyAinscowFreelance programmer / ConsultantCommented:
>>Ok with my C# code; want to hide an executable's window and that executable has no relation with C#. It has a window that i want to hide with my C# code.

So why didn't you ask that initially - a number of experts have told you what you asked for, now you say you want something very different.  Remember the experts here are NOT paid, we give our time and experience for free.
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>and want to hide that executable's windows when that executable starts to process.

Is that still a requirement ?
Are you writing the code for this other application ?  (If yes then what programming language?)
0
jazzIIIloveAuthor Commented:
Isn't question obvious;

C# hiding an executable's window when that executable starts to process. How to?

I want to hide an executable's window with my C# code.

I have no influence on that executable but it's implemented in C++.

Kind regards.
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>I am writing an C# application, and want to hide that executable's windows when that executable starts to process. How to do in C#?

>>Isn't question obvious;


Yes - this.Visible = false; will do it.

BUT you then later asked about hiding the window of a DIFFERENT executable to your C# app.


Anyway:
>>and want to hide that executable's windows when that executable starts to process.

Is that still a requirement ?
0
jazzIIIloveAuthor Commented:
Sorry for the misunderstanding AndyAinscow, you may be right, on a second thought, you may be right.

Ah, yes, I mean that executable should be hidden during its process and no window should be visible, my c# program should do that.

Kind regards.
0
jazzIIIloveAuthor Commented:
The issue is that I try to call the exe as this:

 using (Process exeProcess = Process.Start(startInfo))
                {
                    Console.WriteLine("Executed successfully!\n");                    
                }

but I don't know when I call the following function;

    public void HideProcessWindows(string ProcessName)
        {
            foreach (Process p in Process.GetProcessesByName(ProcessName))
            {
                ShowWindow(p.MainWindowHandle, SW_HIDE);
            }
        }  

Because if I call above function, HideProcessWindows; before Process.Start(), there is no process and no effect...
and if I call above function, HideProcessWindows; after Process. Start(), the process already started...

What should I do?

Kind regards.
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>during its process

So what does that mean?

Does it mean your C# app is starting this other app, letting it perform a task then closing it - all without the user seeing any window ?

Does it mean the user clicks a button (selects a menu item) ON THE OTHER APP and at that instant your C# app will hide this other app until it finishes the process then shows it again ?

Does it mean .....
0
AndyAinscowFreelance programmer / ConsultantCommented:
Ignore most of my last post, I was typing as you were supplying more info.
0
AndyAinscowFreelance programmer / ConsultantCommented:
The following is crude but probably not far off what you require.

        [DllImport("User32")] 
        private static extern int ShowWindow(IntPtr handle, int nCmdShow);
        private const int SW_HIDE = 0;

        private void button1_Click(object sender, EventArgs e)
        {

            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = "notepad.exe";

            Process p = Process.Start(psi);
            p.WaitForInputIdle() ;  //Wait until app is running - if one doesn't wait here then there may be no main window to hide
            ShowWindow(p.MainWindowHandle, SW_HIDE);
            
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nffvrxqgrcfqvvcCommented:
It takes some time for MainWindowHandle to update.. It's different on each system also sometimes 50ms, 100ms, even 300ms after WaitForInputIdle. You can make a timeout routine though if the MainWindowHandle doesn't become 0 after 5 seconds abort. Either that or make repeated calls to ShowWindow() in a timer until it returns TRUE then disable timer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.