using System; using System.IO; using System.Net; using System.Data; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Configuration; using System.Threading; using Core.Mes.IBaseInterface; using Core.Mes.ClientFrameWork; using System.Runtime.InteropServices; using System.Diagnostics; namespace Core.Mes.ClientManager { /// /// 登录窗口 /// public class FrmCover : System.Windows.Forms.Form { #region " 定义变量 " public MainForm _mainForm; public static int pot; public static string url; public static string messageFlag; public int _loginFlag = 0; //重新登录标志 public bool ReLoginFlag = false; public int ReLoginType = 0; private System.Windows.Forms.Timer timer_checkSem; private int index = 1; #endregion // #region " 窗体自动生成代码 " public FrmCover() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } public FrmCover(string sUserName) { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); textBox1.Text = sUserName; textBox1.Enabled = false; // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button btnLoad; private System.Windows.Forms.ToolTip toolTip1; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.PictureBox pictureBox1; private System.ComponentModel.IContainer components; private global::Infragistics.Win.UltraWinProgressBar.UltraProgressBar ultraProgressBar1; private System.Windows.Forms.Timer timerAutoRun; #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); Infragistics.Win.Appearance appearance4 = new Infragistics.Win.Appearance(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmCover)); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.btnLoad = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.ultraProgressBar1 = new Infragistics.Win.UltraWinProgressBar.UltraProgressBar();//new Infragistics.Win.UltraWinProgressBar.UltraProgressBar(); this.timerAutoRun = new System.Windows.Forms.Timer(this.components); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.timer_checkSem = new System.Windows.Forms.Timer(this.components); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // textBox1 // this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append; this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource; this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.textBox1.Location = new System.Drawing.Point(150, 133); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(120, 21); this.textBox1.TabIndex = 0; this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown); this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); // // textBox2 // this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.textBox2.Location = new System.Drawing.Point(150, 161); this.textBox2.Name = "textBox2"; this.textBox2.PasswordChar = '*'; this.textBox2.Size = new System.Drawing.Size(120, 21); this.textBox2.TabIndex = 1; this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress); // // btnLoad // this.btnLoad.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnLoad.ImageIndex = 0; this.btnLoad.Location = new System.Drawing.Point(282, 132); this.btnLoad.Name = "btnLoad"; this.btnLoad.Size = new System.Drawing.Size(56, 23); this.btnLoad.TabIndex = 2; this.btnLoad.Text = "登录"; this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click); // // btnExit // this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnExit.ImageIndex = 3; this.btnExit.Location = new System.Drawing.Point(282, 160); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(55, 23); this.btnExit.TabIndex = 3; this.btnExit.Text = "退出"; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // pictureBox1 // this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top; this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(450, 104); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pictureBox1.TabIndex = 6; this.pictureBox1.TabStop = false; // // ultraProgressBar1 // this.ultraProgressBar1.BorderStyle = Infragistics.Win.UIElementBorderStyle.None; this.ultraProgressBar1.Dock = System.Windows.Forms.DockStyle.Bottom; appearance4.ForeColor = System.Drawing.Color.Yellow; this.ultraProgressBar1.FillAppearance = appearance4; this.ultraProgressBar1.Location = new System.Drawing.Point(0, 213); this.ultraProgressBar1.Maximum = 300; this.ultraProgressBar1.Name = "ultraProgressBar1"; this.ultraProgressBar1.Size = new System.Drawing.Size(450, 14); this.ultraProgressBar1.Step = 1; this.ultraProgressBar1.SupportThemes = false; this.ultraProgressBar1.TabIndex = 8; this.ultraProgressBar1.Text = "新余钢铁有限责任公司技术中心"; // // timerAutoRun // this.timerAutoRun.Tick += new System.EventHandler(this.timerAutoRun_Tick); // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(105, 137); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(41, 12); this.label4.TabIndex = 12; this.label4.Text = "用户名"; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(117, 165); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(29, 12); this.label5.TabIndex = 13; this.label5.Text = "密码"; // // timer_checkSem // this.timer_checkSem.Interval = 200; this.timer_checkSem.Tick += new System.EventHandler(this.timer_checkSem_Tick); // // FrmCover // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(450, 227); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.ultraProgressBar1); this.Controls.Add(this.btnExit); this.Controls.Add(this.btnLoad); this.Controls.Add(this.pictureBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "FrmCover"; this.ShowInTaskbar = false; this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "系统用户登录"; this.Load += new System.EventHandler(this.FrmCover_Load); this.SizeChanged += new System.EventHandler(this.FrmCover_SizeChanged); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion #endregion #region " Init Form " DataSet dsetLoginUrl = new DataSet(); /// /// 窗体初始化 /// /// /// private void FrmCover_Load(object sender, System.EventArgs e) { try { if (ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["Corporation"].Value.ToString() != null) this.ultraProgressBar1.Text = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["Corporation"].Value.ToString() + @" & 湖南视拓科技发展有限责任公司 联合研发"; this.pictureBox1.Image = System.Drawing.Image.FromFile(@"LoginPic.jpg"); url = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["RemoteServerUrl"].Value.ToString(); if (File.Exists("LoginUrl.txt")) { dsetLoginUrl.ReadXml("LoginUrl.txt"); dsetLoginUrl.Tables[0].PrimaryKey = new DataColumn[] { dsetLoginUrl.Tables[0].Columns[0] }; foreach (DataRow arow in dsetLoginUrl.Tables[0].Rows) { textBox1.AutoCompleteCustomSource.Add(arow[0].ToString()); } } else { DataTable dtab = new DataTable(); dtab.Columns.Add("LogID", typeof(string)); dtab.PrimaryKey = new DataColumn[] { dtab.Columns[0] }; dsetLoginUrl.Tables.Add(dtab); dsetLoginUrl.WriteXml("LoginUrl.txt", XmlWriteMode.IgnoreSchema); } } catch (Exception ex) { Console.WriteLine(ex.Message); } try { //为锁定统一登录设置进程间信号量 if (!string.IsNullOrEmpty(textBox1.Text.Trim()) && ReLoginType == 2) { Semaphore sem = OpenSem(false, true); if (sem != null) timer_checkSem.Enabled = true; } } catch { } this.textBox1.Focus(); } #endregion #region " Button Event " private bool IsLoginThreadFlag = false; private string loginID = ""; private string passwd = ""; private bool IsException = false; private Thread ThLogin; /// /// 登录按钮事件 /// /// /// private void btnLoad_Click(object sender, System.EventArgs e) { loginID = this.textBox1.Text.Trim(); passwd = this.textBox2.Text.Trim(); this.timerAutoRun.Interval = 100; index = 1; IsException = false; this.ultraProgressBar1.Value = this.ultraProgressBar1.Minimum; this.btnLoad.Enabled = false; if (!ReLoginFlag) { //this.ultraProgressBar1.Visible = true; this.timerAutoRun.Start(); ThreadStart ts = new ThreadStart(LoginHandler); ThLogin = new Thread(ts); ThLogin.Name = "LoginThread"; ThLogin.Start(); } else { Relogin(); } } private void Relogin() { try { string ipaddress = ClientCommon.GetIp(); SimpleReturnObject _out = new SimpleReturnObject(0, ""); ClientCommon._RemotingHelp.InitServerUrlList("ServerCommon", url); object obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetPurviewInfo", new object[] { this.loginID, this.passwd, Dns.GetHostName(), ipaddress }, out _out); if (_out.ErrCode != 0) throw new Exception(_out.ErrMessage); if (!string.IsNullOrEmpty(_out.ErrMessage)) { this._mainForm.ShowPopUpMsg(_out.ErrMessage, "密码过于简单,请尽快修改密码!"); } ClientCommon._UserInfo = (UserInfo)obj; Semaphore sem = OpenSem(true, true); timer_checkSem.Enabled = false; AfterLogin(); //调用服务器与客户端时间同步函数 SetTime.SetClientTime(); } catch (Exception ex) { MessageBox.Show("连接服务器失败!\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } this.btnLoad.Enabled = true; } private void AfterLogin() { try { _loginFlag = 1; try { if (dsetLoginUrl.Tables[0].Rows.Find(textBox1.Text.Trim()) == null) { if (dsetLoginUrl.Tables[0].Rows.Count == 10) dsetLoginUrl.Tables[0].Rows[0].Delete(); dsetLoginUrl.Tables[0].Rows.Add(new object[] { textBox1.Text.Trim() }); dsetLoginUrl.Tables[0].AcceptChanges(); dsetLoginUrl.WriteXml("LoginUrl.txt", XmlWriteMode.IgnoreSchema); } } catch { } this.Close(); } catch (Exception ex) { this.btnLoad.Enabled = true; _loginFlag = 0; MessageBox.Show("连接服务器失败!\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void LoginHandler() { string ipaddress = ClientCommon.GetIp(); try { IsLoginThreadFlag = false; SimpleReturnObject outInfo = new SimpleReturnObject(0, ""); ClientCommon._RemotingHelp.InitServerUrlList("ServerCommon", url); object obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetPurviewInfo", new object[] { this.loginID, this.passwd, Dns.GetHostName(), ipaddress }, out outInfo); if (outInfo.ErrCode != 0) throw new Exception(outInfo.ErrMessage); if (!string.IsNullOrEmpty(outInfo.ErrMessage)) { this._mainForm.ShowPopUpMsg(outInfo.ErrMessage, "密码过于简单,请尽快修改密码!"); } ClientCommon._UserInfo = (UserInfo)obj; ClientCommon._RemotingHelp.ServerUrlList = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetServerList", null, out outInfo); _loginFlag = 1; IsLoginThreadFlag = true; } catch (Exception ex) { //this.btnLoad.Enabled =true; IsException = true; _loginFlag = 0; IsLoginThreadFlag = true; if (ex.Message == "线程正被中止。") return; MessageBox.Show("连接服务器失败!\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnExit_Click(object sender, System.EventArgs e) { _loginFlag = -1; try { this.timerAutoRun.Stop(); if (ThLogin != null) { ThLogin.Abort(); } } catch { } this.Close(); } private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar == (char)13) { } if (e.KeyChar == (char)27) { this.btnExit_Click(sender, new EventArgs()); } } private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar == (char)13) { this.btnLoad_Click(sender, new EventArgs()); } if (e.KeyChar == (char)27) { this.btnExit_Click(sender, new EventArgs()); } } private void button1_Click(object sender, System.EventArgs e) { FrmPasswd frm = new FrmPasswd(); frm.strUserID = this.textBox1.Text.Trim(); frm.ShowDialog(); } #endregion #region " Monuse Events " #endregion private void timerAutoRun_Tick(object sender, System.EventArgs e) { int current = this.ultraProgressBar1.Value; int max = this.ultraProgressBar1.Maximum; int min = this.ultraProgressBar1.Minimum; int step = this.ultraProgressBar1.Step; if (index++ > 300) { string strInfo = ""; try { this.timerAutoRun.Stop(); if (ThLogin != null) { ThLogin.Abort(); } } catch (Exception ex) { strInfo = ex.Message; } MessageBox.Show("连接超时!\n" + strInfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (current == max && step > 0) this.ultraProgressBar1.Value = min; this.ultraProgressBar1.PerformStep(); if (IsLoginThreadFlag) { if (IsException) { this.timerAutoRun.Stop(); //this.ultraProgressBar1.Visible = false; } if (this.ultraProgressBar1.Value != max) { this.timerAutoRun.Interval = 10; this.ultraProgressBar1.Step = 5; } else { this.timerAutoRun.Stop(); Semaphore sem = OpenSem(true, false); this.AfterLogin(); } } else { } } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == System.Windows.Forms.Keys.Return) { System.Windows.Forms.SendKeys.Send("{TAB}"); } } [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("User32.dll")] public static extern int SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); //ShowWindow参数 private const int SW_SHOWMINIMIZED = 2; private const int SWP_SHOWWINDOW = 0x40; private void FrmCover_SizeChanged(object sender, EventArgs e) { //base.OnSizeChanged(e); if (this.WindowState == FormWindowState.Minimized) { var owner = this.Owner; if (owner != null) { ShowWindow(owner.Handle, SW_SHOWMINIMIZED); this.Visible = true; //移到屏幕外面,模拟最小化效果 SetWindowPos(this.Handle, IntPtr.Zero, owner.Left, owner.Top, 0, 0, SWP_SHOWWINDOW); //Trace.WriteLine(string.Format("x: {0}, y: {1}", Program.MainForm.Left, Program.MainForm.Top)); } } } private void timer_checkSem_Tick(object sender, EventArgs e) { try { string loginid = textBox1.Text.Trim().ToUpper(); string sem_name = "CORE_MES." + loginid; if (string.IsNullOrEmpty(loginid)) return; bool CreateNewSem = false; Semaphore ProcessSem = new Semaphore(1, 1, sem_name, out CreateNewSem); if (ProcessSem.WaitOne(0, false)) { try { ProcessSem.Release(1); } catch { } timer_checkSem.Enabled = false; SomeOneLogined(); } } catch { } } private Semaphore OpenSem(bool ReleaseIt, bool ForceCreate) { bool CreateNewSem = false; Semaphore sem = null; string loginid = textBox1.Text.Trim().ToUpper(); string sem_name = "CORE_MES." + loginid; if (string.IsNullOrEmpty(loginid)) return sem; if (!ForceCreate) { try { sem = Semaphore.OpenExisting(sem_name); } catch { sem = null; } } else { sem = new Semaphore(1, 1, sem_name, out CreateNewSem); } try { if (sem != null) { if (ReleaseIt) { sem.Release(1); Debug.Print("释放信号量!"); } else { if (sem.WaitOne(1, false)) Debug.Print("获取信号量!"); } } } catch (Exception ex) { } return sem; } private void SomeOneLogined() { timer_checkSem.Enabled = false; loginID = this.textBox1.Text.Trim(); this.timerAutoRun.Interval = 100; index = 1; IsException = false; this.ultraProgressBar1.Value = this.ultraProgressBar1.Minimum; this.btnLoad.Enabled = false; try { AfterLogin(); //调用服务器与客户端时间同步函数 SetTime.SetClientTime(); } catch (Exception ex) { MessageBox.Show("连接服务器失败!\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } this.btnLoad.Enabled = true; } } }