Files
ClientServerProject/软件系统客户端模版/FormMainWindow.cs

320 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 BasicFramework;
using IndustryEthernet;
using Newtonsoft.Json.Linq;
using CommonLibrary;
using System.Threading;
//============================================================================
// 模版日期 2017-02-21
// 创建人 胡少林
// 版权所有 胡少林
// 授权说明 模版仅授权个人使用如需商用请联系hsl200909@163.com洽谈
// 说明 JSON组件引用自james newton-king遵循MIT授权协议
//============================================================================
//============================================================================
//
// 注意:本代码的相关操作未作密码验证,如有需要,请自行完成
// 示例具体示例参照Form1_FormClosing(object sender, FormClosingEventArgs e)方法
// 如果遇到启动调试就退出了请注释掉Program.cs文件中的指允许启动一个实例的代码
//
//============================================================================
namespace
{
public partial class FormMainWindow : Form
{
public FormMainWindow()
{
InitializeComponent();
}
#region
/// <summary>
/// 指示窗口是否显示的标志
/// </summary>
private bool IsWindowShow { get; set; } = false;
private void FormMainWindow_Load(object sender, EventArgs e)
{
//窗口载入
label_userName.Text = UserClient.UserAccount.UserName;
label_grade.Text = AccountGrade.GetDescription(UserClient.UserAccount.Grade);
label_factory.Text = UserClient.UserAccount.Factory;
label_register.Text = UserClient.UserAccount.RegisterTime.ToString();
label_last.Text = UserClient.UserAccount.LastLoginTime.ToString();
label_times.Text = UserClient.UserAccount.LoginFrequency.ToString();
label_address.Text = UserClient.UserAccount.LastLoginIpAddress;
//绑定事件仅执行一次不能放到show方法里
net_socket_client.MessageAlerts += Net_socket_client_MessageAlerts;
net_socket_client.LoginFailed += Net_socket_client_LoginFailed;
net_socket_client.LoginSuccess += Net_socket_client_LoginSuccess;
net_socket_client.AcceptByte += Net_socket_client_AcceptByte;
net_socket_client.AcceptString += Net_socket_client_AcceptString;
//启动网络服务
Net_Socket_Client_Initialization();
label_Announcement.Text = UserClient.Announcement;
toolStripStatusLabel_Version.Text = UserClient.CurrentVersion.ToString();
}
private void FormMainWindow_Shown(object sender, EventArgs e)
{
//窗口显示
IsWindowShow = true;
//是否显示更新日志
if(UserClient.JsonSettings.IsNewVersionRunning)
{
UserClient.JsonSettings.IsNewVersionRunning = false;
UserClient.JsonSettings.SaveSettings();
ToolStripMenuItem_Click(null, new EventArgs());
}
//根据权限使能菜单
if(UserClient.UserAccount.Grade<AccountGrade.SuperAdministrator)
{
ToolStripMenuItem.Enabled = false;
ToolStripMenuItem.Enabled = false;
ToolStripMenuItem.Enabled = false;
}
}
private void FormMainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
//窗口关闭
IsWindowShow = false;
net_socket_client.ClientClose();
//等待一秒退出
FormWaitInfomation fwm = new FormWaitInfomation("正在退出程序...", 1000);
fwm.ShowDialog();
fwm.Dispose();
}
#endregion
#region
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
//实例化一个密码修改的窗口,并指定了实现修改的具体方法,指定了密码长度
FormPasswordModify fpm = new FormPasswordModify(UserClient.UserAccount.Password,
p => {
JObject json = new JObject();json.Add(UserAccount.UserNameText, UserClient.UserAccount.UserName);
json.Add(UserAccount.PasswordText, p);
return net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode. + json.ToString()).IsSuccess;
}, 6, 8);
fpm.ShowDialog();
fpm.Dispose();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormAbout fa = new FormAbout(Resource.StringResouce.SoftName,
UserClient.CurrentVersion, 2017, Resource.StringResouce.SoftCopyRight);
fa.ShowDialog();
fa.Dispose();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
//更新情况复位
if (UserClient.JsonSettings.IsNewVersionRunning)
{
UserClient.JsonSettings.IsNewVersionRunning = false;
UserClient.JsonSettings.SaveSettings();
}
FormUpdateLog ful = new FormUpdateLog(UserClient.HistoryVersions);
ful.ShowDialog();
ful.Dispose();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormAboutVersion fav = new FormAboutVersion(UserClient.CurrentVersion);
fav.ShowDialog();
fav.Dispose();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormInputAndAction fiaa = new FormInputAndAction(str => net_simplify_client.ReadFromServer(
CommonHeadCode.SimplifyHeadCode. + str).IsSuccess,UserClient.Announcement);
fiaa.ShowDialog();
fiaa.Dispose();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormLog flg = new FormLog(net_simplify_client);
flg.ShowDialog();
flg.Dispose();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
using (FormRegisterAccount fra = new FormRegisterAccount(net_simplify_client))
{
fra.ShowDialog();
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormAccountManage fam = new FormAccountManage(() =>
{
OperateResultString result = net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode.);
if (result.IsSuccess) return result.Content;
else return result.ToMessageShowString();
},m => net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode. + m).IsSuccess);
fam.ShowDialog();
fam.Dispose();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
//暂不支持,期待下个版本
MessageBox.Show("暂不支持该特性");
}
#endregion
#region
private Net_Socket_Client net_socket_client = new Net_Socket_Client();
private void Net_Socket_Client_Initialization()
{
try
{
net_socket_client.EndPointServer = new System.Net.IPEndPoint(
System.Net.IPAddress.Parse(UserClient.ServerIp),
CommonLibrary.CommonLibrary.Port_Main_Net);
net_socket_client.ClientAlias = UserClient.UserAccount.UserName;
net_socket_client.ClientStart();
}
catch(Exception ex)
{
SoftBasic.ShowExceptionMessage(ex);
}
}
private void Net_socket_client_AcceptString(HuTcpState object1, string object2)
{
//接收到服务器发来的字符串数据
string head_code = object2.Substring(0, 4);
if (head_code == CommonHeadCode.MultiNetHeadCode.)
{
if (IsHandleCreated) Invoke(new Action(() =>
{
FormPopup fpp = new FormPopup(object2.Substring(4), Color.DodgerBlue, 10000);
fpp.Show();
}));
}
else if (head_code == CommonHeadCode.MultiNetHeadCode.线)
{
if (IsHandleCreated) Invoke(new Action(() =>
{
listBox1.DataSource = object2.Substring(4).Split('#');
}));
}
else if (head_code == CommonHeadCode.MultiNetHeadCode.)
{
if (IsHandleCreated) Invoke(new Action(() =>
{
Close();
}));
}
else if(head_code==CommonHeadCode.SimplifyHeadCode.)
{
//此处应用到了同步类的指令头
if (IsHandleCreated) Invoke(new Action(() =>
{
UserClient.Announcement = object2.Substring(4);
label_Announcement.Text= object2.Substring(4);
FormPopup fpp = new FormPopup(object2.Substring(4), Color.DodgerBlue, 10000);
fpp.Show();
}));
}
}
private void Net_socket_client_AcceptByte(HuTcpState object1, byte[] object2)
{
//接收到服务器发来的字节数据
if (IsHandleCreated) Invoke(new Action(() =>
{
MessageBox.Show(BitConverter.ToInt32(object2, 0).ToString());
}));
}
private void Net_socket_client_LoginSuccess()
{
//登录成功,或重新登录成功的事件,有些数据的初始化可以放在此处
if (IsHandleCreated) Invoke(new Action(() =>
{
toolStripStatusLabel_status.Text = "客户端启动成功";
}));
}
private void Net_socket_client_LoginFailed(int object1)
{
//登录失败的情况,如果连续三次连接失败,请考虑退出系统
if (object1 > 3)
{
if (IsHandleCreated) Invoke(new Action(() =>
{
Close();
}));
}
}
private void Net_socket_client_MessageAlerts(string object1)
{
//信息提示
if (IsHandleCreated) Invoke(new Action(() =>
{
toolStripStatusLabel_status.Text = object1;
}));
}
#endregion
#region
//=========================================================================================
//
// 在本界面任意地方调用net_simplify_client.ReadFromServer("[指令头]")即可获取服务器数据
// 处理结果之前先进行判定,具体参照示例
//
//=========================================================================================
/// <summary>
/// 用于界面请求访问服务器数据所用
/// </summary>
private Net_Simplify_Client net_simplify_client = new Net_Simplify_Client(
new System.Net.IPEndPoint(System.Net.IPAddress.Parse(UserClient.ServerIp),
CommonLibrary.CommonLibrary.Port_Second_Net));
#endregion
}
}