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();
                }
  • 相关阅读:
    开源权限框架shiro 入门
    Struts1.2入门笔记
    memcache概述
    教你如何将中文转换成全拼
    WPF第一章(XAML前台标记语言(Chapter02代码讲解))
    WPF第一章(XAML前台标记语言)
    WPF简介
    Activity以singleTask模式启动,intent传值的解决办法
    linux下查看文件编码以及编码转换
    Fedora 17字体美化
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/9935730.html
Copyright © 2011-2022 走看看