• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

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.
0
jazzIIIlove
Asked:
jazzIIIlove
  • 7
  • 4
  • 3
  • +4
1 Solution
 
AndyAinscowCommented:
this.Visible = false;

where 'this' is the main window you want to hide.
0
 
RickCommented:
Set the form's opacity to 0
0
 
Naman GoelCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
NavneetCommented:
0
 
NavneetCommented:
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
 
NavneetCommented:
        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
 
AndyAinscowCommented:
>>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
 
AndyAinscowCommented:
>>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
 
AndyAinscowCommented:
>>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
 
AndyAinscowCommented:
>>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
 
AndyAinscowCommented:
Ignore most of my last post, I was typing as you were supplying more info.
0
 
AndyAinscowCommented:
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 4
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now