| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- 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); // 双缓冲
- }
- /// <summary>
- /// 删除7天前的日志
- /// </summary>
- private void DeleteLog()
- {
- DirectoryInfo dir = new DirectoryInfo(sysdisc + @":\Log");
- List<string> DeleteFileName = new List<string>();
- //保存6天前和今天的操作日志
- for (int i = 1; i <= 6; 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)
- {
- thread = new Thread(DeleteLog);
- thread.Start();
- RefreshDBConnect.Interval = 600 * 1000;
- RefreshDBConnect.Tick += RefreshDB;
- RefreshDBConnect.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();
- }
- private void RefreshDB(object sender, EventArgs e)
- {
- DataHelper dh = new DataHelper();
- }
- /// <summary>
- ///设置窗体的最大化和最小化状态
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 重载创建文件句柄
- /// </summary>
- 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("UAS_MES." + tb.Text);
- (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 = ConfigurationManager.ConnectionStrings["MES"].ConnectionString;
- 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();
- RefreshDBConnect.Stop();
- this.Close();
- }
- }
- }
- }
|