How to disable the close button of the console window?

This is an old piece of code, and it is also a code that I used often before. Although it is basically not used now and in the future, it is very useful in certain scenarios.

scenes to be used

Sometimes, we need to write a console program with certain processing logic, which is simpler than writing a Windows service. However, we need to prevent accidentally clicking the close button in the upper right corner of the console window and causing the program to exit abnormally. So there is a simple requirement as stated in the title of this article.

Code

To find the Windows window and the button to disable the Windows window, you need to use Windows API FindWindow, GetSystemMenuand RemoveMenu, the specific code implementation is shown below, you can copy the code to the console project and run it directly:

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace Demo
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            string title = $"程序 {DateTime.Now} 启动";
            //修改控制台窗口标题
            Console.Title = title;
            //禁用控制台窗口关闭按钮
            DisableCloseButton(title);

            //检测指定 title 的控制台窗口是否存在
            bool isExist = IsExistsConsole(title);

            Console.WriteLine($"isExist = {isExist},窗口标题:{title}");

            Console.WriteLine("按回车键退出");

            Console.ReadLine();
        }

        #region 禁用控制台窗口关闭按钮
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

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

        [DllImport("user32.dll", EntryPoint = "RemoveMenu")]
        static extern IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);

        ///<summary>
        /// 禁用控制台窗口关闭按钮
        ///</summary>
        ///<param name="title">窗口标题</param>
        public static void DisableCloseButton(string title)
        {
    
    
            //线程休眠,确保能够正常 FindWindow,否则有时会 Find 失败。
            Thread.Sleep(100);

            IntPtr windowHandle = FindWindow(null, title);
            IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);
            const uint SC_CLOSE = 0xF060;
            RemoveMenu(closeMenu, SC_CLOSE, 0x0);
        }

        /// <summary>
        /// 检测指定 title 的控制台窗口是否存在
        /// </summary>
        /// <param name="title">windows 窗口标题</param>
        /// <returns></returns>
        public static bool IsExistsConsole(string title)
        {
    
    
            IntPtr windowHandle = FindWindow(null, title);
            if (windowHandle.Equals(IntPtr.Zero)) return false;

            return true;
        }
        #endregion
    }
}

The results of its operation are as follows:

disable close button

to sum up

As mentioned above, the code is very simple, and the functions implemented are also very simple. I just don’t think I will use it again in the future, so I’ll remember it in case I forget it forever. If it happens to be useful to you, it would be a great honor.


Author: Technical Zemin
Publisher: Technical Translation Station

Public Number: Technical Translation Station

Guess you like

Origin blog.csdn.net/weixin_47498376/article/details/112544984