1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.1 KiB
C#

using HighWayIot.Log4net;
using HighWayIot.Winform.UserControlPages;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace HighWayIot.Winform.MainForm
{
public partial class BaseForm : Form
{
private static LogHelper logger = LogHelper.Instance;
List<UserControl> UserControls = new List<UserControl>();
public BaseForm()
{
InitializeComponent();
Init();
}
/// <summary>
/// 初始化类
/// </summary>
public void Init()
{
logger.Info("主页面已启动");
UserPanelSwitch(typeof(MonitorMainPage), "监控主页面");
}
/// <summary>
/// 系统退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SysQuitStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 注销
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LogoutStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
/// <summary>
/// 菜单事件
/// 菜单事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StripMenuItemClick(object sender, EventArgs e)
{
if (sender is ToolStripMenuItem && sender != null)
{
ToolStripMenuItem button = (ToolStripMenuItem)sender;
switch (button.Text)
{
case "角色管理":
UserPanelSwitch(typeof(RoleConfigPage), button.Text);
break;
case "用户管理":
UserPanelSwitch(typeof(UserConfigPage), button.Text);
break;
case "操作日志":
UserPanelSwitch(typeof(OperateConfigPage), button.Text);
break;
case "报警日志":
UserPanelSwitch(typeof(AlarmConfigPage), button.Text);
break;
case "监控主页面":
UserPanelSwitch(typeof(MonitorMainPage), button.Text);
break;
case "":
break;
default:
MessageBox.Show("找不到按钮!");
break;
}
}
}
/// <summary>
/// 切换前端页面,动态创建
/// </summary>
/// <param name="userControlType">Usercontrol类型</param>
/// <param name="tag">名称标签</param>
private void UserPanelSwitch(Type userControlType, string tag)
{
bool flag = false;
foreach(UserControl us in UserControls)
{
if(us.GetType() == userControlType)
{
flag = true;
//如果在标签页里,切换选项卡
if (UserControlTabs.Contains(us) || us.Tag.ToString() == tag)
{
try
{
foreach(Control control in this.UserControlTabs.Controls)
{
if(control is TabPage page)
{
if(page.Text == tag)
{
UserControlTabs.SelectedTab = page;
}
}
}
}
catch(ArgumentNullException ex)
{
logger.Error("找不到相关标签页!请重启程序!", ex);
}
catch(Exception ex)
{
logger.Error("标签页未知错误!", ex);
}
}
//如果不在标签页里
else
{
TabPage tabPage = new TabPage(tag);
us.Parent = tabPage;
UserControlTabs.TabPages.Add(tabPage);
}
break;
}
}
if (!flag)
{
try
{
// 动态创建新的 UserControl 实例
object obj = Activator.CreateInstance(userControlType);
if (obj is UserControl newControl)
{
newControl.Tag = tag;
UserControls.Add(newControl); // 将新实例添加到列表
TabPage tabPage = new TabPage(tag);
newControl.Parent = tabPage;
newControl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
newControl.Dock = DockStyle.Fill;
UserControlTabs.TabPages.Add(tabPage);
UserControlTabs.SelectedTab = tabPage;
}
else
{
throw new InvalidOperationException($"类型 {userControlType.Name} 不是有效的 UserControl 类型");
}
}
catch (Exception ex)
{
logger.Error("无法创建用户控件实例", ex);
MessageBox.Show($"无法创建用户控件实例:{ex.Message}", "错误");
}
}
}
}
}