| using System; using System.Collections.Generic; using System.Threading; using System.Windows.Forms; using MES.Core; using EES.Common; using EES.Controls.Win; using EES.Common.Data; using System.ComponentModel; using System.Runtime.InteropServices; using System.Text; using System.Linq; using WinControls; using DataCool_MES.framework; using DataCool_MES.config; using DataCool_MES.utils; using System.Drawing; namespace DataCool_MES {
public delegate void FeedInfoHandler(object sender, Exception ex, string message, MessageTipsLevel level); /// <summary> /// 业务窗体基类 /// </summary> public partial class BaseModuleForm : DockBaseWorkForm {
#region DllImport [DllImport("kernel32")] public static extern long WritePrivateProfileString(string section, string key, string val, string filepath); [DllImport("kernel32")] public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); [DllImport("imm32.dll")] public static extern IntPtr ImmGetContext(IntPtr hwnd); [DllImport("imm32.dll")] public static extern bool ImmGetOpenStatus(IntPtr himc); [DllImport("imm32.dll")] public static extern bool ImmSetOpenStatus(IntPtr himc, bool b); [DllImport("imm32.dll")] public static extern bool ImmGetConversionStatus(IntPtr himc, ref int lpdw, ref int lpdw2); [DllImport("imm32.dll")] #endregion public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey); private const int IME_CMODE_FULLSHAPE = 0x8; private const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11; private SynchronizationContext sc; protected string onceScanData = string.Empty; private BarCodeHooK hook; IModuleFlow coreFlowObj; // 获取屏幕分辨率 private Rectangle primaryScreen; private float scaleX; // 基准分辨率宽度 private float scaleY; [Browsable(false)] public IModuleFlow CoreFlowObj {
get { return coreFlowObj; } } [Browsable(false)] /// <summary> /// 日志记录处理异常信息显示处理对象 /// 此处用于各种日志和状态信息的记录,并进行对应的信息的显示 /// </summary> internal virtual AlertLog Log {
get {
return null; } } protected event Action CursorBegin; protected event Action CursorEnd; /// <summary> /// 实例化脚本对象 /// </summary> /// <returns></returns> protected virtual IModuleFlow CreateModuleFlow() {
return null; } public BaseModuleForm() {
DoubleBuffered = true; ImeMode = ImeMode.Off; AutoScaleMode = AutoScaleMode.None; InitializeComponent(); #region Cursor CursorBegin = () => {
Clear(); Cursor = Cursors.WaitCursor; }; CursorEnd = () => {
Cursor = Cursors.Default; }; #endregion Load += BaseModuleForm_Load; hook = new BarCodeHooK(); hook.BarCodeEvent += Hook_BarCodeEvent; hook.Start(); FormClosed += BaseModuleForm_FormClosed; } private void BaseModuleForm_FormClosed(object sender, FormClosedEventArgs e) {
try {
hook.Stop(); hook.BarCodeEvent -= Hook_BarCodeEvent; hook = null; } catch (Exception ex) {
FrameAppContext.GetInstance().AppLogger.Error(ex); } } private void Hook_BarCodeEvent(BarCodeHooK.BarCodes barCode) {
if (CoreFlowObj != null && FlowContext.Instance.WorkStatus == WorkStatus.Running && !string.IsNullOrEmpty(barCode.BarCode)) {
onceScanData = TrimSpecialChar(barCode.BarCode); CoreFlowObj.OnExecScanReceiving(onceScanData); } } private void BaseModuleForm_Load(object sender, EventArgs e) {
sc = SynchronizationContext.Current; if (!DesignMode) {
Input = FlowContext.Instance; FlowContext.Instance.FlowCustomEvent += OnRaiseFlowCustomEvent; } } private void OnRaiseFlowCustomEvent(object sender, TEventArgs<EventClass> e) {
sc.Post(new SendOrPostCallback(delegate (object obj) {
OnFlowCustomEvent((EventClass)obj); }), e.Data); } protected virtual void OnFlowCustomEvent(EventClass ec) {
if (ec.EventType == FlowViewCommand.ClearCurText) {
Clear(); } } /// <summary> /// 获取当前窗体绑定的上下文对象 /// </summary> /// <returns></returns> public virtual BindingType GetBindingType() {
Type type; if (bindingSource.DataSource is Type) {
type = (Type)bindingSource.DataSource; } else {
if (bindingSource.DataSource != null) {
type = bindingSource.DataSource.GetType(); } else {
type = null; } } if (type == null) {
throw new Exception("界面输入的数据源为空"); } BindingType bindingType = new BindingType(); Type type2; if (type.GUID == typeof(DataCollection<>).GUID) {
type2 = type.GetGenericArguments()[0]; bindingType.IsArray = true; } else {
if (type.GUID == typeof(List<>).GUID) {
type2 = type.GetGenericArguments()[0]; bindingType.IsArray = true; } else {
type2 = type; bindingType.IsArray = false; } } bindingType.Type = Factory.getRawType(type2); return bindingType; } public virtual void Start() {
if (coreFlowObj != null && FlowContext.Instance.WorkStatus == WorkStatus.Running) return; IModuleFlow core = null; try {
CursorBegin.Invoke(); if (CoreFlowObj != null) throw new Exception("正在作业,不能启动!"); core = CreateModuleFlow(); if (core != null) {
core.FeedInfo += OnFeedInfo; core.ExecBeginWork(); coreFlowObj = core; FlowContext.Instance.WorkStatus = WorkStatus.Running; } ImeMode = ImeMode.NoControl; Log.SetLogFocus(); } catch (Exception ex) {
FlowContext.Instance.WorkStatus = WorkStatus.NoAction; if (coreFlowObj != null) {
coreFlowObj.FeedInfo -= OnFeedInfo; coreFlowObj = null; } throw ex; } finally {
if (FlowContext.Instance.WorkStatus == WorkStatus.Running) {
onceScanData = string.Empty; } CursorEnd.Invoke(); } } public virtual void Stop() {
if (CoreFlowObj != null) {
try {
var configObj = SerializerHelper.LoadFromXML<AppContentConfig>(FrameAppContext.GetInstance().RunTimeConfigPath + "\\AppContentConfig.Config"); if (configObj != null && !string.IsNullOrEmpty(configObj.EntryModuleName) && configObj.EntryModuleName.Equals("成品装箱作业")) {
using (var dlgFrm = new FlowStopCheckForm()) {
var dlg = dlgFrm.ShowDialog(); if (dlg == DialogResult.OK && !FlowContext.Instance.IsEndPacking) {
CoreFlowObj.ExecEndWork(); coreFlowObj = null; FlowContext.Instance.WorkStatus = WorkStatus.Stopped; } if (dlg == DialogResult.OK && FlowContext.Instance.IsEndPacking) {
CoreFlowObj.StopWorkCheck(); } } } else {
CoreFlowObj.ExecEndWork(); coreFlowObj = null; FlowContext.Instance.WorkStatus = WorkStatus.Stopped; } } catch (Exception ex) {
Exception error = new Exception("流程执行异常停止," + ex.InnerException == null ? ex.Message : ex.InnerException.Message); FrameAppContext.GetInstance().AppLogger.Error(error); } finally {
Clear(); if (FlowContext.Instance.WorkStatus != WorkStatus.Running) {
Log.Write("作业已经停止!", "", MessageTipsLevel.Warning); FrameAppContext.GetInstance().AppLogger.Info("作业已经停止!"); FlowContext.Instance.ResetContext(); } Utility.GCFullCollect(); } } }<
|