using FileManager;
using MeasureData;
using MeasureThread;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static MeasureThread.ThreadStatusEventArgs;
namespace HOZProject
{
public partial class FormHOZMain : Form
{
#region 成员变量
private BackgroundWorker m_BackgroundWorker;// 申明后台对象
///
/// 测量文件
///
public MeasureFile m_MeasureFile;
/// 测量线程
public Measure m_Ms;
///
/// 显示日志窗体
///
Form m_FormLog = null;
///
/// 显示实例化窗体
///
Form m_FormInit = null;
///
/// 初始化用户控件
///
UControl_Init uControl_Init = null;
#endregion
#region 构造函数
public FormHOZMain()
{
InitializeComponent();
//在线程操作过程中,可以设置控件属性
Control.CheckForIllegalCrossThreadCalls = false;
m_BackgroundWorker = new BackgroundWorker(); // 实例化后台对象
m_BackgroundWorker.WorkerReportsProgress = true; // 设置可以通告进度
m_BackgroundWorker.WorkerSupportsCancellation = true; // 设置可以取消
m_BackgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
m_BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(UpdateProgress);
m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork);
LogManager.InitManulLog();
}
#endregion
#region 测量线程
void DoWork(object sender, DoWorkEventArgs e)
{
m_Ms = new Measure();
m_Ms.InitMeas(m_MeasureFile);
m_Ms.SendThreadStatus += new ThreadStatusHandler(displayMessage); //注册事件
m_Ms.SendCutHolesStatus += new CutHolesStatusHandler(displayCutHoleMessage); //注册事件
//设置控件操作
SetWinControlMeasureState(false);
//自动测量的全过程
m_Ms.DoMeasure();
//定位
//切割
//分析位置
//截面
}
public void displayMessage(object sender, ThreadStatusEventArgs e)
{
//主界面显示内容
this.BeginInvoke((Action)delegate
{
//流程编号
string eCode = e.Step_Code;
//流程开始时间
DateTime eTime = e.Time;
//图片对象
PictureInformation ePictureInformation = e.Picture_Information;
//当前状态
string eState = e.State;
this.listmsg.Items.Add(e.Time.ToString() +"_"+ e.State);
ShowLogInfo();
});
}
public void displayCutHoleMessage(object sender, CutHolesStatusEventArgs e)
{
//主界面显示内容
this.BeginInvoke((Action)delegate
{
//this.listmsg.Items.Add(e.HoleName.ToString() + e.State);
ChangeCutHoleState(e.HoleName.ToString(), Convert.ToInt32(e.State));
});
}
void UpdateProgress(object sender, ProgressChangedEventArgs e)
{
}
void CompletedWork(object sender, RunWorkerCompletedEventArgs e)
{
//设置控件操作
SetWinControlMeasureState(true);
}
#endregion
private void FormHOZMain_Load(object sender, EventArgs e)
{
//加载控件的点击事件
this.Click += new EventHandler(FormHOZMain_Click);
plFill.Click += new EventHandler(FormHOZMain_Click);
plTop.Click += new EventHandler(FormHOZMain_Click);
plLeft.Click += new EventHandler(FormHOZMain_Click);
plLeftContent.Click += new EventHandler(FormHOZMain_Click);
pbImage.Click += new EventHandler(FormHOZMain_Click);
//加载窗口移动事件
this.MouseDown += new MouseEventHandler(FormHOZMain_MouseDown);
//plFill.MouseDown += new MouseEventHandler(FormHOZMain_MouseDown);
//plTop.MouseDown += new MouseEventHandler(FormHOZMain_MouseDown);
//plLeft.MouseDown += new MouseEventHandler(FormHOZMain_MouseDown);
//plLeftContent.MouseDown += new MouseEventHandler(FormHOZMain_MouseDown);
//新建
NewCreate();
}
#region 创建切孔列表信息
///
/// 创建切孔列表信息
///
///
public void CreateCutHoleList(List ListCutHole)
{
//清空左侧Panel中的切孔控件
plLeftContent.Controls.Clear();
plPrarInfo.Controls.Clear();
for (int i = ListCutHole.Count - 1; i >= 0; i--)
{
UControl_CutHole ucCutHole = new UControl_CutHole(this);
ucCutHole.Dock = DockStyle.Top;
ucCutHole.CutHoleName = ListCutHole[i].HoleName;
//显示切孔参数信息
if (ucCutHole.UControl_ParaInfo == null)
{
ucCutHole.UControl_ParaInfo = new UControl_ParaInfo(this);
}
plPrarInfo.Width = ucCutHole.UControl_ParaInfo.Width;
plPrarInfo.Height = ucCutHole.UControl_ParaInfo.Height;
//设置当前样品的参数信息
ucCutHole.UControl_ParaInfo.Name = ListCutHole[i].HoleName;
ucCutHole.UControl_ParaInfo.CutHoleName = ListCutHole[i].HoleName;
ucCutHole.UControl_ParaInfo.Position = ListCutHole[i].Position;
ucCutHole.UControl_ParaInfo.StartTime = ListCutHole[i].START.ToString();
ucCutHole.UControl_ParaInfo.EndTime = ListCutHole[i].END.ToString();
ucCutHole.UControl_ParaInfo.State = ListCutHole[i].STATE.ToString();
ucCutHole.UControl_ParaInfo.IsSwitch = ListCutHole[i].SWITCH;
ucCutHole.UControl_ParaInfo.Dock = DockStyle.Fill;
ucCutHole.UControl_ParaInfo.ShowParaInfo();
plPrarInfo.Controls.Add(ucCutHole.UControl_ParaInfo);
plLeftContent.Controls.Add(ucCutHole);
}
plPrarInfo.Visible = false;
}
#endregion
//隐藏处理层
private void FormHOZMain_Click(object sender, EventArgs e)
{
if (plPrarInfo.Visible)
{
plPrarInfo.Visible = false;
}
}
#region 设置控件在线程过程中的编辑状态
///
/// 设置控件在线程过程中的编辑状态
///
///
private void SetWinControlMeasureState(bool cState)
{
pbNew.Enabled = cState;
pbOpen.Enabled = cState;
pbSave.Enabled = cState;
pbInit.Enabled = cState;
pbImportTemplateFile.Enabled = cState;
pbLog.Enabled = cState;
}
#endregion
#region 窗体 最大化 最小化 关闭 按钮事件
private void pbMin_MouseEnter(object sender, EventArgs e)
{
this.pbMin.BackgroundImage = global::HOZProject.Properties.Resources.Min_Blue;
}
private void pbMin_MouseLeave(object sender, EventArgs e)
{
this.pbMin.BackgroundImage = global::HOZProject.Properties.Resources.Min_Gray;
}
private void pbMax_MouseEnter(object sender, EventArgs e)
{
this.pbMax.BackgroundImage = global::HOZProject.Properties.Resources.Max_Blue;
}
private void pbMax_MouseLeave(object sender, EventArgs e)
{
this.pbMax.BackgroundImage = global::HOZProject.Properties.Resources.Max_Gray;
}
private void pbClose_MouseEnter(object sender, EventArgs e)
{
this.pbClose.BackgroundImage = global::HOZProject.Properties.Resources.exit_Red;
}
private void pbClose_MouseLeave(object sender, EventArgs e)
{
this.pbClose.BackgroundImage = global::HOZProject.Properties.Resources.exit_Gray;
}
private void pbClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void pbMin_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void pbMax_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Maximized;
}
//窗体据中
this.StartPosition = FormStartPosition.CenterScreen;
}
#endregion
#region 新建、保存、打开、初始化、导入配置、查看日志
private void pbNew_Click(object sender, EventArgs e)
{
NewCreate();
}
private void NewCreate()
{
m_MeasureFile = new MeasureFile();
if (!m_MeasureFile.New())
{
return;
}
else
{
//清空切孔列表用户控件
plLeftContent.Controls.Clear();
//加载配置文件
}
}
private void pbSave_Click(object sender, EventArgs e)
{
if (m_MeasureFile == null)
{
MessageBox.Show("请新建一个测量文件");
}
else
{
m_MeasureFile.Save();
}
}
private void pbOpen_Click(object sender, EventArgs e)
{
}
private void pbInit_Click(object sender, EventArgs e)
{
if (uControl_Init == null)
{
uControl_Init = new UControl_Init(this);
uControl_Init.ReloadConfig();
}
if (m_FormInit == null)
{
m_FormInit = new Form();
}
m_FormInit.StartPosition = FormStartPosition.CenterScreen;
m_FormInit.FormBorderStyle = FormBorderStyle.None;
m_FormInit.Width = uControl_Init.Width;
m_FormInit.Height = uControl_Init.Height;
if (m_FormInit.Controls.Count == 0)
{
m_FormInit.Controls.Add(uControl_Init);
}
m_FormInit.ShowDialog();
}
private void pbImportTemplateFile_Click(object sender, EventArgs e)
{
//打开默认路径
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "样品参数文件|*.cfg";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (uControl_Init == null)
{
uControl_Init = new UControl_Init(this);
}
//设置选择的模板文件
uControl_Init.ReadConfigPath = openFileDialog.FileName;
//获取文件
uControl_Init.ReadConfigInfo();
}
}
private void pbLog_Click(object sender, EventArgs e)
{
m_FormLog = new Form();
m_FormLog.StartPosition = FormStartPosition.CenterScreen;
UControl_Log uControl_Log = new UControl_Log();
m_FormLog.FormBorderStyle = FormBorderStyle.None;
m_FormLog.Width = uControl_Log.Width;
m_FormLog.Height = uControl_Log.Height;
uControl_Log.Name = "UControl_Log";
//获取日志信息
if (listmsg.Items.Count>0)
{
string[] strLog = new string[listmsg.Items.Count];
for (int i = 0; i < listmsg.Items.Count; i++)
{
//赋值给数组
strLog[i] = listmsg.Items[i].ToString();
}
uControl_Log.ShowProcessLogInfo(strLog);
}
m_FormLog.Controls.Add(uControl_Log);
m_FormLog.ShowDialog();
}
//显示日志
private void ShowLogInfo()
{
if (m_FormLog != null)
{
foreach (Control item in m_FormLog.Controls)
{
if (item is UserControl)
{
if (item.Name == "UControl_Log")
{
UControl_Log ucLog = (UControl_Log)item;
//获取日志信息
if (listmsg.Items.Count > 0)
{
string[] strLog = new string[listmsg.Items.Count];
for (int i = 0; i < listmsg.Items.Count; i++)
{
//赋值给数组
strLog[i] = listmsg.Items[i].ToString();
}
ucLog.ShowProcessLogInfo(strLog);
}
}
}
}
}
}
#endregion
#region 修改切孔状态
///
/// 修改切孔状态
///
///
///
private void ChangeCutHoleState(string CutHoleName, int State)
{
foreach (Control item in plLeftContent.Controls)
{
if (item is UserControl)
{
UControl_CutHole cutHole = (UControl_CutHole)item;
if (cutHole.CutHoleName == CutHoleName)
{
Button btnCutHole = (Button)cutHole.Controls.Find("btnCutHole", false)[0];
switch (State)
{
//开始运行
case (int)ThreadState.InProcess:
btnCutHole.BackColor = Color.Yellow;
break;
//完成
case (int)ThreadState.Success:
btnCutHole.BackColor = Color.LawnGreen;
break;
//错误
case (int)ThreadState.Failed:
btnCutHole.BackColor = Color.Red;
break;
}
break;
}
}
}
}
#endregion
#region 拖动无窗体的控件
[DllImport("user32.dll")]//拖动无窗体的控件
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
private void FormHOZMain_MouseDown(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
//拖动窗体
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
}
#endregion
#region 开始、结束线程事件
private void pbStart_Click(object sender, EventArgs e)
{
if (m_MeasureFile == null)
{
MessageBox.Show("请新建一个测量文件");
//listmsg.Items.Clear();
}
else
{
if (m_BackgroundWorker.IsBusy)
{
MessageBox.Show("线程已经运行");
return;
}
m_BackgroundWorker.RunWorkerAsync(this);
}
}
private void pbStop_Click(object sender, EventArgs e)
{
if (m_BackgroundWorker.IsBusy)
{
m_BackgroundWorker.CancelAsync();
}
}
#endregion
#region 清空处理层中的控件
///
/// 清空处理层中的控件
///
private void ClearProcessControls()
{
//清空处理层中的控件
plPrarInfo.Controls.Clear();
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
if (plPrarInfo.Controls.Count > 0)
{
string name = textBox1.Text;
string code = textBox2.Text;
int state = Convert.ToInt32(comboBox1.Text);
foreach (Control item in plPrarInfo.Controls)
{
if (item is UserControl)
{
if (item.Name == name)
{
UControl_ParaInfo uControl_ParaInfo = (UControl_ParaInfo)item;
TimeLineItem[] ParaItem = uControl_ParaInfo.TlItem;
foreach (TimeLineItem tlItem in ParaItem)
{
if (tlItem.Code == code)
{
tlItem.State = state;
break;
}
}
uControl_ParaInfo.TimeLineInvalidate();
break;
}
}
}
}
}
}
}