using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using UAS_MES.CustomControl.AccordionMenu;
using UAS_MES.DataOperate;
using UAS_MES.Entity;
using UAS_MES.Properties;
using UAS_MES.PublicMethod;
namespace UAS_MES
{
public partial class Main : Form
{
//所有用到了headBar的部分都需要这段代码
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
string SerialPort1 = "Make!ColorBoxWeigh#Make!CartonBoxWeigh";
Thread thread;
string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
public Main()
{
InitializeComponent();
DoubleBuffered = true;
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
}
///
/// 删除7天前的日志
///
private void DeleteLog()
{
DirectoryInfo dir = new DirectoryInfo(sysdisc + @":\Log");
List DeleteFileName = new List();
//保存6天前和今天的操作日志
for (int i = 1; i <= 30; i++)
{
DeleteFileName.Add(DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd") + ".txt");
}
DeleteFileName.Add(DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
if (dir.Exists)
{
FileInfo[] file = dir.GetFiles();
foreach (FileInfo f in file)
{
if (!DeleteFileName.Contains(f.Name))
f.Delete();
}
}
}
//tabPage标签图片尺寸
const int CLOSE_SIZE = 16;
//窗体加载的时候将headBar的事件委托给指定的函数
private void Main_Load(object sender, EventArgs e)
{
//杀死全部未关闭的打印进程
//Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
//for (int i = 0; i < processes.Length; i++)
//{
// processes[i].Kill();
//}
string[] lines = System.IO.File.ReadAllLines(sysdisc + @":\" + "lblprocess" + ".txt");
foreach (string line in lines)
{
if (line != "")
{
string processid = line.Split('|')[0];
string lblid = line.Split('|')[1];
try
{
if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName == System.Diagnostics.Process.GetCurrentProcess().ProcessName)
{
}
else
{
System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
}
}
catch (Exception)
{
try
{
System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
}
catch (Exception)
{
}
Console.WriteLine("删除");
FileStream fas = new FileStream(sysdisc + @":\" + "lblprocess" + ".txt", FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fas);
StreamReader sr = new StreamReader(fas);
String s = sr.ReadToEnd();
sw.Write(s.Replace(line+"\r\n",""));
sw.Flush();
sw.Close();
sr.Close();
fas.Close();
}
}
}
thread = new Thread(DeleteLog);
thread.Start();
this.Tag = "ShowDialogWindow";
this.headBar1.MouseDown += new MouseEventHandler(this.headBar_MouseDown);
this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
this.MainTabControl.Padding = new Point(CLOSE_SIZE + 10, CLOSE_SIZE);
this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
this.MainTabControl.MouseDown += new MouseEventHandler(this.MainTabControl_MouseDown);
//设置底部栏目的基础信息
inf_name.Text = User.UserName;
//inf_position.Text = User.UserPosition;
Inf_linecode.Text = User.UserLineCode;
inf_source.Text = User.UserSourceCode;
inf_db.Text = SystemInf.CurrentDB;
inf_currentstep.Text = User.CurrentStepName;
//设置获取当前屏幕大小自动全屏但是保留任务栏
Rectangle ScreenArea = Screen.GetWorkingArea(this);
Top = 0;
Left = 0;
Width = ScreenArea.Width;
Height = ScreenArea.Height;
SetForm();
//设置导航栏宽度
SystemInf.NavWidth = Menu.Width;
SystemInf.HeadBarHeight = headBar1.Height;
}
///
///设置窗体的最大化和最小化状态
///
public void SetForm()
{
int WS_SYSMENU = 0x00080000; // 系统菜单
int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮
int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));
SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
}
protected override CreateParams CreateParams
{
get
{
const int WS_MINIMIZEBOX = 0x00020000; // Winuser.h中定义
CreateParams cp = base.CreateParams;
cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作
return cp;
}
}
///
/// 重载创建文件句柄
///
protected override void CreateHandle()
{
if (!IsHandleCreated)
{
try { base.CreateHandle(); }
catch { }
finally
{
if (!IsHandleCreated)
base.RecreateHandle();
}
}
}
Bitmap image = Resources.close_button;
//鼠标点下的时候监听
private void headBar_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
GlobalEventsHandler.keycode = keyData;
return false;
}
private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
Brush HeadTextColor = new SolidBrush(Color.Green);
Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);
//先添加TabPage属性
if (e.State == DrawItemState.Selected)
e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), HeadTextColor, myTabRect.X + 2, myTabRect.Y + 2);
else
e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134), SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);
//再画一个矩形框
using (Pen p = new Pen(SystemBrushes.Control))
{
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
e.Graphics.DrawRectangle(p, myTabRect);
}
//画关闭符号
using (Pen objpen = new Pen(Color.Black))
{
//使用图片
Bitmap bt = new Bitmap(image);
Point p5 = new Point(myTabRect.X, 8);
e.Graphics.DrawImage(bt, p5);
}
e.Graphics.Dispose();
}
catch (Exception) { }
}
//关闭按钮功能
private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
//如果鼠标在区域内就关闭选项卡
bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
if (isClose == true)
{
this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
}
}
}
private void MainTabControl_ControlRemoved(object sender, ControlEventArgs e)
{
//移除已打开的Form中的Form
TabPage tb = e.Control as TabPage;
AccordionMenu.OpenedFormName.Remove(tb.Tag.ToString());
(tb.Controls[0] as Form).Close();
//掉用了串口的程序需要在这个步骤关闭串口
try
{
if (SerialPort1.Contains(tb.Controls[0].Tag.ToString()))
{
//在这里调用Form的Close方法来触发TabPage下的Form的FormClosing事件
(tb.Controls[0] as Form).Close();
}
}
catch (Exception) { }
}
private void LoginOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string logout_confirm = MessageBox.Show(this.ParentForm, "退出登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
if (logout_confirm == "Yes")
{
//注销的时候切换回默认数据库
SystemInf.ConnectionString = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
DataHelper.DBConnectionString = SystemInf.ConnectionString;
//清除上个用户的权限信息
SystemInf.Caller.Clear();
//清除已经打开过的窗口的信息
AccordionMenu.OpenedFormName.Clear();
//注销的时候将除了登陆窗口之外的全部窗口关闭
for (int i = 0; i < Application.OpenForms.Count; i++)
{
if (Application.OpenForms[i].Name != "Login")
Application.OpenForms[i].Close();
}
this.Hide();
Login login = new Login();
login.ShowDialog();
this.Close();
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
//杀死全部未关闭的打印进程
Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
for (int i = 0; i < processes.Length; i++)
{
processes[i].Kill();
}
System.Environment.Exit(0);
}
}
}