Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:31:00+00:00 2026-05-26T18:31:00+00:00

I am writing a windows forms app that can launch a console for debugging.

  • 0

I am writing a windows forms app that can launch a console for debugging. I want to disable the close button of the console so that the windows forms app cannot be shut down through the close button of the console. I have built the test code skeleton and it works. The code is below:

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.Diagnostics;
using System.Runtime.InteropServices;

namespace bsa_working
{
    public partial class Form1 : Form
    {
        static bool console_on = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (ViewConsole.Checked)
            {
                Win32.AllocConsole();
                ConsoleProperties.ConsoleMain();

                // Set console flag to true
                console_on = true;  // will be used later
            }
            else
                Win32.FreeConsole();
        }
    }

    public class Win32
    {
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
    }

    public class ConsoleProperties
    {
        [DllImport("user32.dll")]
        static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);

        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        [DllImport("user32.dll")]
        static extern IntPtr RemoveMenu(IntPtr hMenu, uint nPosition, uint wFlags);

        internal const uint SC_CLOSE = 0xF060;
        internal const uint MF_GRAYED = 0x00000001;
        internal const uint MF_BYCOMMAND = 0x00000000;

        public static void ConsoleMain()
        {
            IntPtr hMenu = Process.GetCurrentProcess().MainWindowHandle;
            IntPtr hSystemMenu = GetSystemMenu(hMenu, false);

            EnableMenuItem(hSystemMenu, SC_CLOSE, MF_GRAYED);
            RemoveMenu(hSystemMenu, SC_CLOSE, MF_BYCOMMAND);

            // Set console title
            Console.Title = "Test Console";

            // Set console surface foreground and background color
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.ForegroundColor = ConsoleColor.White;
            Console.Clear();
        }
    }
}

The code works fine EXCEPT:

  1. When the code is compiled and run the first time, the X on the console is NOT grayed out but it is grayed out on the Windows Forms app. However, when the code is closed down and run again, the code works as it should; that is, the X on the console is grayed out and the Windows Forms app is as it should be. Any ideas why and how this can be fixed?

  2. Sometimes the console comes up behind the win form. Any way to force the console to always come on top?

As an aside, is there any way the console can be pinned to a specific spot on the WinForm? app? I can set its size so if I could pin it in a specific spot I could create a spot for it on the form.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:31:01+00:00Added an answer on May 26, 2026 at 6:31 pm

    To make that work you need to change IntPtr hMenu = Process.GetCurrentProcess().MainWindowHandle; to use the Window Handle of the Console Window instead (which you can obtain by calling GetConsoleWindow()).

    To make it display on top you could use for example SetForegroundWindow with the Console Window Handle.

    About the pinning I am really not sure whether this is even possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am about to start on a journey writing a windows forms application that
I am writing a small .NET Windows Forms app, and I use the built-in
I'm writing an app that has a wizard type input section. Think MS Windows
I am writing a windows forms application that has a check box regarding the
I'm writing a Windows Form app that uses an API (the smartFOCUS API, to
We're working on a Windows App that periodically has to launch operations which may
I'm writing a Windows Forms app in C#, using Visual Studio 2010. It has
I'm writing a Windows Forms app, VS2010, NET Framework 4.0, coding in VB. I'm
I'm writing a console app in C#, using Mono on OS X. On Windows,
I'm writing a Windows Forms application which is supposed to play three sound files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.