需求
项目使用WCF
作为服务提供API
调用,没有寄宿到其他地方,所以有个控制台窗口,怕实施人员或其他操作人员不小心激活窗口,误操作把服务关了,所以才找了这个功能!
后期会开发服务端的综合管理工具,这也是临时方案
代码
using System;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
class Program
{
#region Win32 Api
[DllImport("User32.dll ", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll ", EntryPoint = "GetSystemMenu")]
extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
[DllImport("user32.dll ", EntryPoint = "RemoveMenu")]
extern static int RemoveMenu(IntPtr hMenu, int nPos, int flags);
#endregion
static void Main(string[] args)
{
#region 禁用关闭按钮
//与控制台标题名一样的路径
string fullPath = System.Environment.CurrentDirectory + "\\ConsoleApp1.exe";
//根据控制台标题找控制台
int WINDOW_HANDLER = FindWindow(null, fullPath);
//找关闭按钮
IntPtr CLOSE_MENU = GetSystemMenu((IntPtr)WINDOW_HANDLER, IntPtr.Zero);
int SC_CLOSE = 0xF060;
//关闭按钮禁用
RemoveMenu(CLOSE_MENU, SC_CLOSE, 0x0);
#endregion
while(true)
{
if(Console.ReadLine().ToUpper()== "EXIT")
{
return;
}
}
}
}
}