这是一段古老的代码,也是我以前经常用到的代码。虽然现在和以后基本上都不会再用到它了,但是在特定的场景中,它很好用。

使用场景
有时候,我们需要编写一个具有一定处理逻辑的控制台程序,这比编写 Windows 服务要简单一些。但是,我们要防止不小心点击到控制台窗口右上角的关闭按钮而导致程序非正常退出。于是就有了如这篇文章标题所述的一个简单的需求。

代码实现
查找 Windows 窗口和禁用 Windows 窗口的按钮,需要用到 Windows API FindWindow 、GetSystemMenu 和 RemoveMenu,具体的代码实现如下所示,可以将代码复制到控制台项目中直接运行:

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
}

}
它的运行结果如下:

disable close button

总结
如上所述,代码很简单,实现的功能也很简单。只是觉得以后基本上不会再用到它了,聊以记之,以防永久遗忘。如若恰好对您有用,不胜荣幸。

[url]http://groups.tianya.cn/post-90242-b836d71804e74e74a10d9aec88b444e4-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-a7032cf5e16446369b34e5c21274ccfa-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-b02e6241a2e4464592bb7532d56a6e2f-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-a8cb6acd16e54bf580560a16123ee071-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-7933431c66a549aa8282ee1000ab4b3a-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-2af22906f8ff498bacd662908acb0c44-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-a126999cbdf844be83c11e1d4ee79ecc-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-580e63138f6f45d89b221aabf0768b43-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-d8f6c197d22a4633ab01a13f81cbcefe-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-ef2b11bc65aa4821b9dafab704b79faa-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-d896a5b30a2746f0aa1069e1d9ad3f88-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-cadb9e48bcfd4a17908c383dcf712e31-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-a25e6b18de154039a2092167d63f96e5-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-74d3dd1cec0842d2a13ee110c74d3d69-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-7ad81391fbe14d1abd659ca9a4939504-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-77285ad941d74b2391303751efbc164a-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-dc6e12517c1745a58656f2e6bbb83c5c-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-982b70e99b964d278877ab1ff0a98118-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-5641ca76eb13492ab8030cc6001f77f9-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-b56b32c5e6bd4b4c851992b9eac72421-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-26b519ba7bd04f4590b42d5e407e206f-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-ce4cf904dae546eaa369951a6734886f-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-fdc61e6393a64b52a050122d7142aad5-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-0e9a5afe8af54cfca0e888568cdb5f77-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-7cb455282d4a43fbb4acb62fa5431dc5-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-1c59fa5369f9460bb3af42ccfbfa527a-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-d11f2af7aebc412c8e1215d27b3c6fe8-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-43eedb0d51e34a75a83d447d28f6bfff-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-cca8a9a8db274aa3802cebc468c7828f-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-a9b90b6cce644b0dbcaf57624f639bb7-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-2ede07c19b8144859e7c94103e2de525-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-38818f57eb2b403eb9ec78aeb850e751-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-d1bbf407304048b4bd6e304e3b99e0fe-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-e0616e2fddec40bfa50db1e32fd6394e-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-9aa92134f923473f9ee97f7093beccd6-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-002178043c7f4a50a8e37e5bb1c2eace-1.shtml[/url]
[url]http://groups.tianya.cn/post-90242-f734fdf88e184d64b223b6d1d31e660e-1.shtml[/url]

作者 : 技术译民
出品 : 技术译站

©欢迎转载,转载请标明出处 https://www.cnblogs.com/ittranslator

不做标题党,只分享技术干货

文章均首发公众号『技术译站』,欢迎扫码关注

分类: DotNet
标签: dotnet, csharp