Skip to content

Commit

Permalink
fix batchcoretest
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinYellow committed Dec 27, 2017
1 parent 5e69945 commit cd08042
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions SCADA/Program/BatchCoreTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
using System.ServiceModel;

namespace BatchCoreService
Expand All @@ -11,6 +12,8 @@ static class Program
static void Main()
{
BatchCoreTest srv = new BatchCoreTest();
ConsoleUtil.RegisterCloseConsoleHandle();//注册控制台关闭事件,注意,只有执行该该操作,事件
ConsoleUtil.ClosedConsole += (sender, e) => srv.Dispose();
Console.ReadLine();
}
}
Expand Down Expand Up @@ -47,4 +50,130 @@ public void Dispose()
}
}
}

public static class ConsoleUtil
{
#region 禁用控制台关闭按钮

///
/// 禁用关闭按钮
///
public static void DisableCloseButton()
{
DisableCloseButton(Console.Title);
}

///
/// 禁用关闭按钮
///
/// 控制台名字
public static void DisableCloseButton(string consoleName)
{

IntPtr windowHandle = FindWindow(null, consoleName);
IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);
uint scClose = 0xF060;
RemoveMenu(closeMenu, scClose, 0x0);
}

#region API
[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);

#endregion

#endregion

#region 捕捉控制台关闭事件

public delegate bool ConsoleCtrlDelegate(int ctrlType);

[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate handlerRoutine, bool add);



///
/// 注册控制台关闭事件,通过事件进行捕捉.
///
public static void RegisterCloseConsoleHandle()
{
SetConsoleCtrlHandler(OnClosedConsole, true);
}

///
/// 当控制台被关闭时,引发事件.
///
public static event EventHandler ClosedConsole;

private static bool OnClosedConsole(int ctrlType)
{
if (ClosedConsole != null)
{
var e = new CloseConsoleEventArgs((CloseConsoleCategory)ctrlType);
ClosedConsole("Console", e);
return e.IsCancel;
}
return false; //忽略处理,让系统进行默认操作
}

#endregion

}

///
/// 控制台关闭事件.
///
public class CloseConsoleEventArgs : EventArgs
{
public CloseConsoleEventArgs()
{

}

public CloseConsoleEventArgs(CloseConsoleCategory category)
{
Category = category;
}

public CloseConsoleCategory Category { get; set; }

///
/// 是否取消操作.
///
public bool IsCancel { get; set; }
}

///
/// 关闭控制台的类型.
///
public enum CloseConsoleCategory
{
///
/// 当用户关闭Console
///
CloseEvent = 2,
///
/// Ctrl+C
///
CtrlCEvent = 0,
///
/// 用户退出(注销)
///
LogoffEvent = 5,
///
/// Ctrl+break
///
CtrlBreakEvent = 1,
///
/// 系统关闭
///
ShutdownEvent = 6,
}
}
Binary file modified SCADA/dll/OPCDriver.dll
Binary file not shown.

0 comments on commit cd08042

Please sign in to comment.