using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; using Lskj.Control; using Lskj.Core; using Lskj.Util; using Lskj.Business.Impl; using Lskj.Model; using Lskj.Data; using Lskj.Notice.Properties; using DevExpress.XtraGrid; using System.Net.Sockets; using System.Net; using System.Threading; using Lskj.Business; namespace Lskj.Notice { /// /// 通知界面 /// public partial class FrmMain : BaseForm { string mUserID; string mUserName; string mUserPassword; private bool isConnect = true; private bool blink = false; private bool isSocketflush = false; private Icon blank = Resources.blank; private Icon striped = Resources.logo; private bool mouseinform = false; private Socket clientSocket = null; /// ///json构造多线程 /// private Thread JosnThread; /// /// 水印 /// private MessageBoxWaterMark _waterMark = new MessageBoxWaterMark(); public FrmMain(string[] args) { InitializeComponent(); this.gridView.DoubleClick += new EventHandler(OnGridViewDoubleClick); if (args != null && args.Length > 1) { this.mUserID = args[0]; this.mUserName = args[1]; this.mUserPassword = args[2]; } this.tsmiShowWindow.Checked = DBConfig.Instance.NoticeShowWindow; this.tsmiFlicker.Checked = DBConfig.Instance.NoticeFlicker; } /// /// 说明:显示窗口 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// void ThreadShowFrom() { int x = Screen.PrimaryScreen.WorkingArea.Width - this.Width; int y = Screen.PrimaryScreen.WorkingArea.Height - this.Height; this.SetDesktopLocation(x, y); //Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height); //this.PointToClient(p); //this.Location = p; //this.Show(); //for (int i = 0; i < this.Height; i++) //{ // this.Location = new Point(p.X, p.Y - i); // System.Threading.Thread.Sleep(1); //} } /// /// 说明:显示普通窗口 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// void NormalShowForm() { if (this.WindowState == FormWindowState.Minimized) { this.Activate(); this.WindowState = FormWindowState.Normal; this.ShowInTaskbar = true; this.notifyIcon1.Visible = false; } } /// /// 说明:重新加载表格数据 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// void ReloadGridData() { try { DBConfig.Instance.ReadConfig(); DBConfig.Instance.CreateConnection(); ERPInfo.Instance.UserId = DBConfig.Instance.NoticeUserID; ERPInfo.Instance.UserName = DBConfig.Instance.NoticeUserName; ERPInfo.Instance.Password = mUserPassword; mUserName = DBConfig.Instance.NoticeUserName; } catch (Exception) { } //DataTable data = NoticeImpl.GetNoticeData2(ERPInfo.Instance.UserId); //int rowCount = data == null ? 0 : data.Rows.Count; //this.gridControlEx.DataSource = data; DataTable data = (this.gridControlEx.DataSource as DataTable).TrimDeleteRows(); int rowCount = data == null ? 0 : data.Rows.Count; this.Text = "您有" + rowCount + "条任务需要处理(提示:双击即可处理)."; if (rowCount > 0) { // 开启新消息弹窗 if (this.tsmiShowWindow.Checked) { this.NormalShowForm(); } // 开启、关闭新消息闪烁 this.blinkTimer.Enabled = this.tsmiFlicker.Checked; } else { //this.WindowState = FormWindowState.Minimized; this.blinkTimer.Enabled = false; } } /// /// 打开基础或单据模块审批模块 /// void OpenAuditControl(DataRow rowItem) { try { bool isCustomNotice = false; string BetweenDLLCoid = string.Empty; String[] AutoParame = rowItem["dllname"].ToString().Split('^'); if (AutoParame.Count() >= 4) { rowItem["dllname"] = AutoParame[0]; rowItem["keyvalue"] = AutoParame[1]; rowItem["modulename"] = AutoParame[2]; if (AutoParame[0].Equals("MiniBlink.dll", StringComparison.CurrentCultureIgnoreCase) || AutoParame[0].Equals("Lskj.PubBrower.dll", StringComparison.CurrentCultureIgnoreCase)) { BetweenDLLCoid = AutoParame[3]; } else { rowItem["stepcode"] = AutoParame[3]; } isCustomNotice = true; } string[] defaultArgs = string.Format(ModuleArgs.DefaultArgs, rowItem["modulename"] + "", ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, 1, rowItem["moduleid"] + "").Split('~'); string[] menuArgs = new string[] { "0", rowItem["moduleid"] + "", rowItem["stepcode"] + "", rowItem["keyvalue"] + "", "0", "0" }; string dllName = rowItem["dllname"] + ""; switch (rowItem["dllname"] + "") { case "Lskj.BaseInfo.dll": menuArgs = new string[] { "1", rowItem["moduleid"] + "", rowItem["keyvalue"] + "" }; break; case "Lskj.PubBillInfo.dll": menuArgs = new string[] { rowItem["moduleid"] + "", rowItem["keyvalue"] + "", rowItem["modulename"] + "", rowItem["stepcode"] + "" }; break; case "Lskj.PubBill.dll": menuArgs = new string[] { "", rowItem["moduleid"] + "" }; break; case "Lskj.PubModuleDetail.dll": menuArgs = new string[] { "", rowItem["moduleid"] + "" }; break; case "MiniBlink.dll": menuArgs = new string[] { "", rowItem["keyvalue"] + "", BetweenDLLCoid }; break; case "Lskj.PubBrower.dll": menuArgs = new string[] { "", rowItem["keyvalue"] + "", BetweenDLLCoid }; break; } if ("2".Equals(rowItem["messid"] + "")) { menuArgs = new string[] { "0", rowItem["moduleid"] + "", rowItem["stepcode"] + "", rowItem["keyvalue"] + "", "1", "0" }; } string[] args = defaultArgs.Concat(menuArgs).ToArray(); IForm form = FormHelper.LoadDllForm(dllName, args); if (dllName.Equals("MiniBlink.dll")) form.SubForm.WindowState = FormWindowState.Maximized; if (dllName.Equals("Lskj.PubBrower.dll")) form.SubForm.WindowState = FormWindowState.Maximized; DialogResult result = form.SubForm.ShowDialog(); if (result != DialogResult.OK && ("1".Equals(rowItem["messid"] + "") || "2".Equals(rowItem["messid"] + ""))) { //if ("2".Equals(rowItem["messid"] + "")) //{ // NoticeImpl.UpdRemindField(rowItem, dllName); //} NoticeImpl.UpeNoticeData(rowItem, isCustomNotice); string sqlStr = ""; DataTable data = NoticeImpl.GetNoticeData2(ERPInfo.Instance.UserId, out sqlStr); int rowCount = data == null ? 0 : data.Rows.Count; int topIndex = this.gridView.TopRowIndex; this.gridControlEx.DataSource = data; this.gridView.TopRowIndex = topIndex; this.Text = "您有" + rowCount + "条任务需要处理(提示:双击即可处理)."; } } catch (Exception ex) { MessageUtil.Show(ex); } } /// /// 说明:窗户加载 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnMainLoad(object sender, EventArgs e) { DBConfig.Instance.CreateConnection(); if (!BaseImpl.HasExistsTable("p_systemNoticeSheet")) { NoticeImpl.CreationNoticeTable(); } this.tsmiFlicker.Checked = DBConfig.Instance.NoticeFlicker; this.tsmiShowWindow.Checked = DBConfig.Instance.NoticeShowWindow; string sqlStr = ""; DataTable data = NoticeImpl.GetNoticeData2(ERPInfo.Instance.UserId, out sqlStr); int rowCount = data == null ? 0 : data.Rows.Count; this.gridControlEx.DataSource = data; this.Text = "您有" + rowCount + "条任务需要处理(提示:双击即可处理)."; this.ThreadShowFrom(); if (rowCount == 0) this.WindowState = FormWindowState.Minimized; OnSocketSendData(); this.ReloadGridData(); this.timer3.Start(); } /// /// 说明:通过Socket通讯更新数据 /// 创建人:王一帆 /// 创建日期:2021-05-20 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnSocketSendData() { //WaitForm.ShowForm(); //bool connected = false; string serverIp = string.Empty; string port = string.Empty; if (string.IsNullOrWhiteSpace(SystemInfo.Instance.MessageInteraction)) return; string[] serverIPPortS = SystemInfo.Instance.MessageInteraction.Split('^'); foreach (string serverIPPort in serverIPPortS) { serverIp = serverIPPort; port = serverIp.Substring(serverIp.LastIndexOf(':') + 1); serverIp = serverIp.Substring(0, serverIp.LastIndexOf(':')); try { JosnThread = null; clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //clientSocket.Blocking = false;//非阻塞模式,定时循环读取缓冲区的数据把它拼接到缓冲区数据队列 arrMsg //这里的ip地址,端口号都是服务端绑定的相关数据。 IPAddress ip = serverIp.Contains(',') ? IPAddress.Parse(serverIp.Substring(0, serverIp.LastIndexOf(','))) : IPAddress.Parse(serverIp); IPEndPoint endpoint = new IPEndPoint(ip, Convert.ToInt32(!string.IsNullOrEmpty(port) ? port : "8080")); clientSocket.BeginConnect(endpoint, null, null);//链接有端口号与IP地址确定服务端. //connected = true; //IAsyncResult result = clientSocket.BeginConnect(endpoint, null, null); //result.AsyncWaitHandle.WaitOne(500); string txtMsg_UserID = DBConfig.Instance.NoticeUserID; string txtMsg_UserName = DBConfig.Instance.NoticeUserName; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(txtMsg_UserID + ',' + txtMsg_UserName); byte[] newbuffer = new byte[buffer.Length + 1];//定义一个新数组 newbuffer[0] = 0;//设置标识,表示发送的是字符串 Buffer.BlockCopy(buffer, 0, newbuffer, 1, buffer.Length);//源数组中的数据拷贝到新数组中 clientSocket.Send(newbuffer);//发送新数组中的数据 //客户端在接受服务端发送过来的数据是通过Socket 中的Receive方法, //该方法会阻断线程,所以我们自己为该方法创建了一个线程 JosnThread = new Thread(ReceMsg); JosnThread.IsBackground = true;//设置后台线程 JosnThread.Start(); break; } catch (Exception ex) { isConnect = false; isSocketflush = false; this.gridControlEx.DataSource = null; } finally { // WaitForm.HideForm(); } } } /// /// 接收服务端发送信息 /// public void ReceMsg() { try { while (true) { isSocketflush = true; byte[] buffer = new byte[1024 * 1024 * 8]; int clientInt = clientSocket.Receive(buffer);//接收服务端发送过来的数据 if (clientInt != 0) { isConnect = true; } string ReceiveMsg = System.Text.Encoding.UTF8.GetString(buffer);//把接收到的字节数组转成字符串显示在文本框中。 DataTable data = JsonUtil.ToDataTable(ReceiveMsg); int rowCount = data == null ? 0 : data.Rows.Count; if (rowCount != 0) { this.Invoke(new EventHandler(delegate { int topIndex = this.gridView.TopRowIndex; this.gridControlEx.DataSource = data; this.gridView.TopRowIndex = topIndex; this.Text = "您有" + rowCount + "条任务需要处理(提示:双击即可处理)."; if (rowCount > 0) { // 开启新消息弹窗 if (this.tsmiShowWindow.Checked) { this.NormalShowForm(); } // 开启、关闭新消息闪烁 this.blinkTimer.Enabled = this.tsmiFlicker.Checked; } else { this.WindowState = FormWindowState.Minimized; this.blinkTimer.Enabled = false; } })); } else { isSocketflush = false; this.gridControlEx.DataSource = null; } } } catch (Exception ex) { isSocketflush = false; } } /// /// 说明:重新加载数据 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnTimerReloadTick(object sender, EventArgs e) { //if (isSocketflush) //{ // this.gridControlEx.Paint -= new System.Windows.Forms.PaintEventHandler(this.OnLabelTitlePaint); //} //else //{ // this.gridControlEx.Paint += new System.Windows.Forms.PaintEventHandler(this.OnLabelTitlePaint); //} if (!mouseinform && !isSocketflush) { this.ReloadGridData(); OnSocketSendData(); } } /// /// 说明:点击打开审批界面 /// 创建人:王一帆 /// 创建日期:2020-08-03 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnGridViewDoubleClick(object sender, EventArgs e) { DataRow rowItem = this.gridView.GetFocusedDataRow(); if (rowItem != null) { if (rowItem["messid"] + "" == "1" && string.IsNullOrWhiteSpace(rowItem["dllname"] + "")) { // 保存小类设置 FrmNoticePrompt frmNoticePrompt = new FrmNoticePrompt(); frmNoticePrompt.TextBoxObj.Text = BaseImpl.HasExistsTable("p_systemMessageTab") ? rowItem["keyvalue"] + "" : rowItem["Msg"] + ""; if (frmNoticePrompt.ShowDialog() == DialogResult.OK) { NoticeImpl.UpeNoticeData(rowItem); string sqlStr = ""; DataTable data = NoticeImpl.GetNoticeData2(ERPInfo.Instance.UserId, out sqlStr); int rowCount = data == null ? 0 : data.Rows.Count; this.gridControlEx.DataSource = data; this.Text = "您有" + rowCount + "条任务需要处理(提示:双击即可处理)."; } } else { OpenAuditControl(rowItem); } } } /// /// 说明:最小化到托盘 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnMainFormClosing(object sender, FormClosingEventArgs e) { this.WindowState = FormWindowState.Minimized; e.Cancel = true; } /// /// 说明:显示窗口 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnTsmiShowClick(object sender, EventArgs e) { this.NormalShowForm(); } /// /// 说明:退出系统 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnTsmiCloseClick(object sender, EventArgs e) { if (MessageUtil.Show("确定要退出任务监视工具?\r\n退后后将无法收到审批消息.", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { try { notifyIcon1.Dispose(); this.Dispose(); this.Close(); } catch (Exception) { } } } /// /// 说明:窗口最小化时显示图标 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnFrmMainSizeChanged(object sender, EventArgs e) { if (WindowState == FormWindowState.Minimized) { this.ShowInTaskbar = false; this.notifyIcon1.Visible = true; } } /// /// 说明:左键点击打开消息列表 /// 创建人:龚宇超 /// 创建日期:2019-09-10 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnNotifyIconMouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { this.NormalShowForm(); } } /// /// 说明:闪烁图标 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnBlinkTimerTick(object sender, EventArgs e) { try { if (!blink) { this.notifyIcon1.Icon = striped; } else { this.notifyIcon1.Icon = blank; } blink = !blink; } catch (Exception) { } } /// /// 说明: /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. void OnTsmiShowWindowClick(object sender, EventArgs e) { this.tsmiShowWindow.Checked = !this.tsmiShowWindow.Checked; DBConfig.Instance.NoticeShowWindow = this.tsmiShowWindow.Checked; DBConfig.Instance.WriteConfig(); } /// /// 说明: /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. void OnTsmiFlickerClick(object sender, EventArgs e) { this.tsmiFlicker.Checked = !this.tsmiFlicker.Checked; DBConfig.Instance.NoticeFlicker = this.tsmiFlicker.Checked; DBConfig.Instance.WriteConfig(); } /// /// 说明:随主系统退出 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnTsmiExitByMainClick(object sender, EventArgs e) { this.tsmiExitByMain.Checked = !this.tsmiExitByMain.Checked; DBConfig.Instance.NoticeExit = this.tsmiExitByMain.Checked; DBConfig.Instance.WriteConfig(); } /// /// 说明:清除所有标识为1的消息 /// 创建人:王一帆 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. private void OnTsmiCleanAllClick(object sender, EventArgs e) { NoticeImpl.UpeNoticeData("1"); string sqlStr = ""; DataTable data = NoticeImpl.GetNoticeData2(ERPInfo.Instance.UserId, out sqlStr); int rowCount = data == null ? 0 : data.Rows.Count; this.gridControlEx.DataSource = data; this.Text = "您有" + rowCount + "条任务需要处理(提示:双击即可处理)."; } /// /// 说明:鼠标进入窗体事件 /// 创建人:王一帆 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. private void OnTsmiMouseEnter(object sender, EventArgs e) { mouseinform = true; if (JosnThread != null) JosnThread.Suspend(); } /// /// 说明:鼠标离开窗体事件 /// 创建人:王一帆 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. private void OnTsmiMouseLeave(object sender, EventArgs e) { mouseinform = false; if (JosnThread != null) JosnThread.Resume(); } /// /// 说明:重绘单据水印 /// 创建人:龚宇超 /// 创建日期:2017-12-04 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void OnLabelTitlePaint(object sender, PaintEventArgs e) { if (this._waterMark != null) { //GraphicsText graphicsText = new GraphicsText(); //graphicsText.Graphics = e.Graphics; //绘制围绕点旋转的文本 //StringFormat format = new StringFormat(); //format.Alignment = StringAlignment.Center; //format.LineAlignment = StringAlignment.Center; // 绘制文本 DataTable data = (this.gridControlEx.DataSource as DataTable).TrimDeleteRows(); int rowCount = data == null ? 0 : data.Rows.Count; this._waterMark.Text = ""; progressPanel.Hide(); if (rowCount == 0) { this._waterMark.Text = "未连接到服务器,正在重连..."; } if (isConnect == false) { progressPanel.Show(); } //if (!string.IsNullOrEmpty(this._waterMark.Text)) //{ // //graphicsText.DrawString(this._waterMark.Text, new Font("宋体", 12, FontStyle.Bold), new SolidBrush(Color.Red), new PointF(this.Width / 2, this.Height / 2 - 10), format, 0); // //e.Graphics.TranslateTransform(0, 80); // //e.Graphics.RotateTransform(-15f); // //e.Graphics.DrawRectangle(new Pen(Color.Red, 2f), new Rectangle(14, 20, 55, 21)); // progressPanel.Show(); //} } } } }