Loumos
Prevent the exit button from closing console application
So here is the situation i am dealing with. I have a Windows forms application that has the Application-Output type: set to Console Application. This allows the console and the windows forms app to be shown at execution. Now if the user presses the exit button on the console it will close the application. I am trying to change the behavior of this to only hide the console using the imported ShowWindow(IntPtr hWnd, int nCmdShow); method from "user23.dll". Everything i have tried just closes the application. See code below.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Security.Policy;
using System.Threading;
using System.CodeDom;
namespace Loumker
{
public static class cc
{
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow(); // gets console window
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); // shows the console window note: can also hide it.
[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate handler, bool add); //sets the control handler for the console.
private delegate bool ConsoleCtrlDelegate(CtrlType ctrlType);
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); //gets the systemMenu of the console window.
[DllImport("user32.dll")]
private static extern int GetMenuItemCount(IntPtr hMenu); //get the count of menu items.
[DllImport("user32.dll")]
private static extern int RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags); //removes the system menu from the window.
private const uint MF_BYPOSITION = 0x400; // constant button position.
private enum CtrlType : uint
{
CTRL_CLOSE_EVENT = 0x0002
}
public static List<string> Processes = new List<string>();
private static Random r = new Random();
public static void Open()
{
IntPtr consoleHandle = GetConsoleWindow();
DisableCloseButton(consoleHandle);
SetConsoleCtrlHandler(new ConsoleCtrlDelegate(ConsoleCtrlHandler), true);
ShowWindow(consoleHandle, 5);
}
public static void Close()
{
IntPtr conoleHandle = GetConsoleWindow();
ShowWindow(conoleHandle, 0);
}
private static bool ConsoleCtrlHandler(CtrlType ctrlType)
{
return true;
}
private static void DisableCloseButton(IntPtr cwindow)
{
IntPtr sysMenu = GetSystemMenu(cwindow, false);
int closeMenuIndex = GetMenuItemCount(sysMenu) - 1;
RemoveMenu(sysMenu, (uint)closeMenuIndex, MF_BYPOSITION);
}
}
}
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Security.Policy;
using System.Threading;
using System.CodeDom;
namespace Loumker
{
public static class cc
{
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow(); // gets console window
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); // shows the console window note: can also hide it.
[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate handler, bool add); //sets the control handler for the console.
private delegate bool ConsoleCtrlDelegate(CtrlType ctrlType);
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); //gets the systemMenu of the console window.
[DllImport("user32.dll")]
private static extern int GetMenuItemCount(IntPtr hMenu); //get the count of menu items.
[DllImport("user32.dll")]
private static extern int RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags); //removes the system menu from the window.
private const uint MF_BYPOSITION = 0x400; // constant button position.
private enum CtrlType : uint
{
CTRL_CLOSE_EVENT = 0x0002
}
public static List<string> Processes = new List<string>();
private static Random r = new Random();
public static void Open()
{
IntPtr consoleHandle = GetConsoleWindow();
DisableCloseButton(consoleHandle);
SetConsoleCtrlHandler(new ConsoleCtrlDelegate(ConsoleCtrlHandler), true);
ShowWindow(consoleHandle, 5);
}
public static void Close()
{
IntPtr conoleHandle = GetConsoleWindow();
ShowWindow(conoleHandle, 0);
}
private static bool ConsoleCtrlHandler(CtrlType ctrlType)
{
return true;
}
private static void DisableCloseButton(IntPtr cwindow)
{
IntPtr sysMenu = GetSystemMenu(cwindow, false);
int closeMenuIndex = GetMenuItemCount(sysMenu) - 1;
RemoveMenu(sysMenu, (uint)closeMenuIndex, MF_BYPOSITION);
}
}
}
5 replies