zoukankan      html  css  js  c++  java
  • 控制台禁止操作

    首先是Windows API声明:

            #region windows api
    
            [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
            static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
            [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            [DllImport("user32.dll ", EntryPoint = "GetSystemMenu", SetLastError = true)]
            static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
            [DllImport("user32.dll ", EntryPoint = "RemoveMenu", SetLastError = true)]
            static extern int RemoveMenu(IntPtr hMenu, int nPos, int flags);
    
            #endregion

    禁止开启两个进程,并根据配置是否隐藏控制台,或者是显示控制台,但是禁止关闭按钮,且令Ctrl + C作为普通输入;只能输入exit退出。

                new Mutex(true, Config.Instance.ServiceNo, out bool ret);
                if (!ret) return;
                
                Console.Title = Config.Instance.ServiceNo;
                IntPtr intptr = FindWindow("ConsoleWindowClass", Config.Instance.ServiceNo);
                
                if (Config.Instance.ShowUi == 0 && intptr != IntPtr.Zero)
                {
                    ShowWindow(intptr, 0);//隐藏窗口
                }
                else
                {
                    IntPtr CLOSE_MENU = GetSystemMenu(intptr, IntPtr.Zero); //找关闭按钮
                    RemoveMenu(CLOSE_MENU, 0xF060, 0x0);//关闭按钮禁用
                    Console.TreatControlCAsInput = true;
                }
                string key = "";
                while (key != null && key.ToLower() != "exit")
                {
                    key = Console.ReadLine();
                }
  • 相关阅读:
    洛谷 P3366 【模板】最小生成树
    洛谷 P2820 局域网
    一本通【例4-10】最优布线问题
    洛谷 P1546 最短网络 Agri-Net
    图论模板
    洛谷 AT667 【天下一人力比較】
    刷题记录
    洛谷P1553 数字翻转(升级版)
    tornado硬件管理系统-网络与磁盘的实现(7)
    tornado硬件管理系统-内存与swap的实现(6)
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/9935730.html
Copyright © 2011-2022 走看看