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:
-
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?
-
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.
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 callingGetConsoleWindow()).To make it display on top you could use for example
SetForegroundWindowwith the Console Window Handle.About the pinning I am really not sure whether this is even possible.