//
you're reading...
Programming Issues/Tips

Minimizing the Console Window in C#.

1. Introduction.

1.1 A few days ago, I was developing a console application written in C#.

1.2 One of the requirements of this application was that it must immediately minimize the console window on startup.

1.3 For the benefit of readers, I decided to post here the code for minimizing the console window of a managed application.

2. Example Code.

2.1 The code below demonstrates how this can be done :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CSConsoleApp01
{
    class Program
    {
        const Int32 SW_MINIMIZE = 6;

        [DllImport("Kernel32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        private static extern IntPtr GetConsoleWindow();

        [DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool ShowWindow([In] IntPtr hWnd, [In] Int32 nCmdShow);

        private static void MinimizeConsoleWindow()
        {
            IntPtr hWndConsole = GetConsoleWindow();
            ShowWindow(hWndConsole, SW_MINIMIZE);
        }

        static void Main(string[] args)
        {
            MinimizeConsoleWindow();
            Console.ReadKey();
        }
    }
}

2.2 I basically used the GetConsoleWindow() Windows API to obtain the window handle used by the console associated with the calling process.

2.3 I then use the ShowWindow() API using the window handle returned from GetConsoleWindow() and with the second parameter set to SW_MINIMIZE (value 6) to minimize the console window.

 

 

Advertisements

About Lim Bio Liong

I've been in software development for nearly 20 years specializing in C , COM and C#. It's truly an exicting time we live in, with so much resources at our disposal to gain and share knowledge. I hope my blog will serve a small part in this global knowledge sharing network. For many years now I've been deeply involved with C development work. However since circa 2010, my current work has required me to use more and more on C# with a particular focus on COM interop. I've also written several articles for CodeProject. However, in recent years I've concentrated my time more on helping others in the MSDN forums. Please feel free to leave a comment whenever you have any constructive criticism over any of my blog posts.

Discussion

One thought on “Minimizing the Console Window in C#.

  1. Thanks a lot!
    Your solution is much more simplier (and with much less lines of code) than the ones i found on other sites.
    Regards

    Posted by Konto | February 27, 2016, 6:19 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: