CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2005
    Posts
    1

    Hiding console window?

    how can i hide the console window for the current app? (ie not opened thru System.Diagnostics.Process)

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Hiding console window?

    There might be other/easier solutions to this, but anyway:
    Code:
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    namespace HideConsole
    {
        class Program
        {
            [DllImport("user32.dll")]
            public static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
    
            public const int  SW_HIDE		= 0;
            public const int SW_SHOWNORMAL		= 1;
            public const int SW_NORMAL		= 1;
            public const int SW_SHOWMINIMIZED	= 2;
            public const int SW_SHOWMAXIMIZED	= 3;
            public const int SW_MAXIMIZE		= 3;
            public const int SW_SHOWNOACTIVATE	= 4;
            public const int SW_SHOW		= 5;
            public const int SW_MINIMIZE		= 6;
            public const int SW_SHOWMINNOACTIVE	= 7;
            public const int SW_SHOWNA		= 8;
            public const int SW_RESTORE		= 9;
            public const int SW_SHOWDEFAULT		= 10;
            public const int SW_FORCEMINIMIZE	= 11;
            public const int SW_MAX			= 11;
    
    	
            static void Main(string[] args)
            {
                // get current process
                Process p = Process.GetCurrentProcess();
    
                // get main windows handle (console)
                IntPtr pHandle = p.MainWindowHandle;
    
                // hide it
                if (ShowWindow(pHandle, SW_HIDE) == 0)
                {
                    // ShowWindow failed
                }
    
                // show it
                if (ShowWindow(pHandle, SW_SHOW) == 0)
                {
                    // ShowWindow failed
                }
            }
        }
    }
    - petter

  3. #3
    Join Date
    May 2002
    Posts
    511

    Re: Hiding console window?

    Someone else posted this solution which worked for me.

    "Create a console application and set in the project properties that it's a windows application. This will make it run in background. "

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured