#region
|
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Drawing;
|
using System.Windows.Forms;
|
using CSFrameworkV5.Business;
|
using CSFrameworkV5.Common;
|
using CSFrameworkV5.Core;
|
using CSFrameworkV5.Language;
|
using CSFrameworkV5.Library.CommonClass;
|
using CSFrameworkV5.Library.Entry;
|
using DevExpress.XtraBars;
|
using DevExpress.XtraEditors;
|
|
#endregion
|
|
namespace CSFrameworkV5.Library
|
{
|
/// <summary>
|
/// 所有MDI子窗体基类.
|
/// </summary>
|
public partial class frmBaseChild : frmBase, IMdiChildForm,
|
IPurviewControllable, ISystemButtons
|
{
|
/// <summary>
|
/// 打开当前窗体传入的预设参数, 界面需要根据此参数做特殊处理
|
/// </summary>
|
protected object _CurrentParameter;
|
|
public frmBaseChild()
|
{
|
InitializeComponent();
|
}
|
|
/// <summary>
|
/// 系统是否只保留一个子窗体实例
|
/// </summary>
|
public bool AllowMultiInstatnce
|
{
|
get => _AllowMultiInstatnce;
|
set => _AllowMultiInstatnce = value;
|
}
|
|
/// <summary>
|
/// 给窗体设置参数,SetParameter方法在在Form.Load事件之前运行的, 因此需要在Form.Load事件内处理传入的参数。
|
/// 参考:MdiTools.OpenChildForm(IMdiForm mdi, Type formType, ToolStripMenuItem sender, object param)方法
|
/// </summary>
|
/// <param name="param"></param>
|
public virtual void SetParameter(object param)
|
{
|
_CurrentParameter = param;
|
}
|
|
/// <summary>
|
/// 清空容器内输入框.
|
/// </summary>
|
public void ClearContainerEditorText(Control container)
|
{
|
for (var i = 0; i < container.Controls.Count; i++)
|
if (container.Controls[i] is TextEdit)
|
((TextEdit)container.Controls[i]).EditValue = null;
|
else if (container.Controls[i] is TextBoxBase)
|
((TextBoxBase)container.Controls[i]).Clear();
|
}
|
|
//当子窗体获得焦点时在主界面注册本窗体的按钮。
|
//通过Form Activated事件可以看到主窗体的ToolBar状态变化。
|
private void frmBaseChild_Activated(object sender, EventArgs e)
|
{
|
ToolbarRegister.RegisteButton(Buttons.ToList());
|
|
NotifyObserver(); //通过其它观察者
|
|
//显示系统操作页面(数据操作按钮页面) //new 20200202
|
(MdiParent as frmMain1).ShowDataOperatePage();
|
}
|
|
private void frmBaseChild_FormClosed(object sender,
|
FormClosedEventArgs e)
|
{
|
NotifyObserver(); //当关闭窗体,要通知所有子窗体观察者
|
}
|
|
private void frmBaseChild_FormClosing(object sender,
|
FormClosingEventArgs e)
|
{
|
IsClosing = true; //正在关闭状态
|
}
|
|
//通知观察者进行更新
|
private void NotifyObserver()
|
{
|
foreach (IObserver o in _observers)
|
if (o != null)
|
o.Notify();
|
}
|
|
#region 成员变量定义
|
|
/// <summary>
|
/// 父窗体的Toolbar组件
|
/// </summary>
|
protected IToolbarRegister _ToolbarRegister;
|
|
/// <summary>
|
/// 初始化子窗体的按钮数组
|
/// </summary>
|
protected IButtonList _buttons = new ButtonList();
|
|
/// <summary>
|
/// 子窗体的观察者
|
/// </summary>
|
protected IList _observers = new ArrayList();
|
|
/// <summary>
|
/// 子窗体的系统按钮
|
/// </summary>
|
protected IButtonInfo[] _SystemButtons = null;
|
|
/// <summary>
|
/// 窗体是否正在关闭状态
|
/// </summary>
|
protected bool _IsClosing;
|
|
/// <summary>
|
/// 否只保留一个子窗体实例
|
/// </summary>
|
protected bool _AllowMultiInstatnce;
|
|
/// <summary>
|
/// 窗体的可用权限
|
/// </summary>
|
protected int _FormAuthorities;
|
|
/// <summary>
|
/// 打开窗体的菜单名
|
/// </summary>
|
protected string _FormMenuName = "";
|
|
#endregion
|
|
#region IPurviewControllable 接口实现
|
|
/// <summary>
|
/// 窗体可用权限.为2^n方:1,2,4,8,16.....n^2
|
/// 打开窗体时从数据库获取权限值保存在该变量
|
/// </summary>
|
public int FormAuthorities
|
{
|
get => _FormAuthorities;
|
set => _FormAuthorities = value;
|
}
|
|
/// <summary>
|
/// 打开窗体的菜单名
|
/// </summary>
|
public string FormMenuName
|
{
|
get => _FormMenuName;
|
set => _FormMenuName = value;
|
}
|
|
/// <summary>
|
/// 派生类通过重写该虚方法自定义每个按钮可用状态
|
/// </summary>
|
public virtual bool ButtonAuthorized(int authorityValue)
|
{
|
if (Loginer.CurrentUser.IsAdmin()) return true;
|
|
return (authorityValue & FormAuthorities) == authorityValue;
|
}
|
|
/// <summary>
|
/// 检查当前用户是否拥有本窗体的某个权限
|
/// </summary>
|
/// <param name="authorityValue">需要检查的权限值,ButtonAuthority类定义功能点</param>
|
/// <returns></returns>
|
public bool HasPurview(int value)
|
{
|
return (value & _FormAuthorities) == value;
|
}
|
|
#endregion
|
|
#region IMdiChildForm 接口实现
|
|
/// <summary>
|
/// 主窗体的Toolbar按钮注册器
|
/// </summary>
|
public IToolbarRegister ToolbarRegister
|
{
|
get => _ToolbarRegister;
|
set => _ToolbarRegister = value;
|
}
|
|
public virtual void RegisterToolBar(IToolbarRegister toolBarRegister)
|
{
|
//this.Buttons是当前窗体的按钮数组。
|
toolBarRegister.RegisteButton(Buttons.ToList());
|
}
|
|
/// <summary>
|
/// 当前窗体是否正在关闭状态
|
/// </summary>
|
public bool IsClosing
|
{
|
get => _IsClosing;
|
set => _IsClosing = value;
|
}
|
|
/// <summary>
|
/// 注册子窗体观察者
|
/// </summary>
|
public void RegisterObserver(IObserver[] observers)
|
{
|
foreach (var o in observers) _observers.Add(o);
|
}
|
|
/// <summary>
|
/// 子窗体的按钮数组
|
/// </summary>
|
public IButtonList Buttons => _buttons;
|
|
/// <summary>
|
/// 模板方法.初始化本窗体的按钮.
|
/// </summary>
|
public virtual void InitButtons()
|
{
|
//关闭按钮
|
_buttons.AddButton(ToolbarRegister.CreateButton("btnClose",
|
LanLib.Get("关闭"), ToolBarGroup.关闭窗体,
|
Globals.LoadBitmap("32_Exit.png"), new Size(57, 28), true, true,
|
DoClose));
|
|
UpdateButtonCaption();
|
}
|
|
/// <summary>
|
/// 系统按钮列表。注:子窗体享用系统按钮,如帮助/关闭窗体常用功能。
|
/// </summary>
|
public virtual IButtonInfo[] GetSystemButtons()
|
{
|
return _SystemButtons;
|
}
|
|
#region 处理快捷方式按钮
|
|
/// <summary>
|
/// 更新快捷方式按钮的标题
|
/// </summary>
|
public virtual void UpdateButtonCaption()
|
{
|
if (string.IsNullOrEmpty(FormButtonName))
|
{
|
var btn =
|
(BarItem)(MdiParent as IMdiForm).GetToolBarButton(
|
ButtonNameList.ribbon_btnShortCut);
|
if (btn != null) btn.Caption = LanLib.Get("设为常用");
|
|
return;
|
}
|
|
var FormFullName = GetType().FullName;
|
var MenuName = FormMenuName;
|
BarItem button = null;
|
|
if (IsExistsShortCutButton(FormFullName, MenuName))
|
{
|
_ShortCutButtonCaption = LanLib.Get("取消常用");
|
button =
|
(BarItem)(MdiParent as IMdiForm).GetToolBarButton(
|
ButtonNameList.ribbon_btnShortCut);
|
if (button != null) button.Caption = LanLib.Get("取消常用");
|
}
|
else
|
{
|
_ShortCutButtonCaption = LanLib.Get("设为常用");
|
button =
|
(BarItem)(MdiParent as IMdiForm).GetToolBarButton(
|
ButtonNameList.ribbon_btnShortCut);
|
if (button != null) button.Caption = LanLib.Get("设为常用");
|
}
|
|
if (string.IsNullOrEmpty(MenuName) ||
|
string.IsNullOrEmpty(FormFullName))
|
{
|
button =
|
(BarItem)(MdiParent as IMdiForm).GetToolBarButton(
|
ButtonNameList.ribbon_btnShortCut);
|
if (button != null) button.Enabled = false;
|
}
|
}
|
|
private static bool IsExistsShortCutButton(string FormFullName,
|
string MenuName)
|
{
|
var exists =
|
new bllUserCustomAction().Exist(FormFullName, MenuName);
|
return exists;
|
}
|
|
private bool _ButtonNameReady = false;
|
private string _FormButtonName;
|
|
/// <summary>
|
/// 获取当前窗体对应的按钮
|
/// </summary>
|
public string FormButtonName
|
{
|
get
|
{
|
if (!_ButtonNameReady && string.IsNullOrEmpty(_FormButtonName))
|
{
|
var FormFullName = GetType().FullName;
|
var MenuName = FormMenuName;
|
_FormButtonName =
|
(MdiTools.MainForm as IMdiForm)
|
.GetModuleMainFormButtonNameByMenuName(FormFullName,
|
MenuName);
|
}
|
|
return _FormButtonName;
|
}
|
}
|
|
protected string _ShortCutButtonCaption = "";
|
|
/// <summary>
|
/// 设为/取消 常用功能
|
/// </summary>
|
/// <param name="sender"></param>
|
public virtual void DoCreateShortCut()
|
{
|
var txt = _ShortCutButtonCaption;
|
var FormFullName = GetType().FullName;
|
var MenuName = FormMenuName;
|
|
var Success = false;
|
if (txt == LanLib.Get("设为常用"))
|
{
|
if (!Msg.AskQuestion("是否要建立 <" + Text + "> 窗体的快捷方式?")) return;
|
|
//数据库添加
|
Success = new bllUserCustomAction().InsertData(FormFullName,
|
MenuName, FormButtonName);
|
}
|
|
if (txt == LanLib.Get("取消常用"))
|
{
|
if (!Msg.AskQuestion("是否要取消 <" + Text + "> 窗体的快捷方式?")) return;
|
|
//数据库删除
|
Success =
|
new bllUserCustomAction().Delete(FormFullName, MenuName);
|
}
|
|
if (Success)
|
{
|
(MdiTools.MainForm as IMdiForm).UpdateModuleButtonLocal(
|
Globals.DEF_CUSTOM_MODULE_MAIN);
|
UpdateButtonCaption();
|
if (Success) Msg.ShowInformation(LanLib.Get("設置快捷按钮操作成功!"));
|
}
|
}
|
|
#endregion
|
|
public virtual void DoHelp(IButtonInfo button) //打开帮助
|
{
|
DoHelp();
|
}
|
|
public virtual void DoHelp()
|
{
|
HelpDoc.HelpAllFile(HelpDoc.CSHelp);
|
//Msg.ShowInformation("已打开帮助文档,如派生的窗体需要打开指定帮助文件,请重写DoHelp方法。");
|
}
|
|
public virtual void DoClose(IButtonInfo sender)
|
{
|
Close();
|
}
|
|
public virtual void DoSetLanguageCHS(IButtonInfo sender)
|
{
|
if (LanLib.Current != LanguageType.CHS)
|
{
|
LanLib.Current = LanguageType.CHS;
|
SetLanguageAllForm();
|
}
|
}
|
|
public virtual void DoSetLanguageCHT(IButtonInfo sender)
|
{
|
if (LanLib.Current != LanguageType.CHT)
|
{
|
LanLib.Current = LanguageType.CHT;
|
SetLanguageAllForm();
|
}
|
}
|
|
public virtual void DoSetLanguageENG(IButtonInfo sender)
|
{
|
if (LanLib.Current != LanguageType.ENG)
|
{
|
LanLib.Current = LanguageType.ENG;
|
SetLanguageAllForm();
|
}
|
}
|
|
private void SetLanguageAllForm()
|
{
|
//设置主窗体的语言
|
(MdiTools.MainForm as ILanguageSupport).SetLanguage();
|
|
foreach (Form form in Application.OpenForms)
|
if (form is ILanguageSupport && form != MdiTools.MainForm)
|
(form as ILanguageSupport).SetLanguage();
|
}
|
|
public override void SetLanguage()
|
{
|
base.SetLanguage();
|
SetDynimcButtonLanguage();
|
|
Buttons.GetButtonByName("btnHelp").Caption = LanLib.Get("帮助");
|
Buttons.GetButtonByName("btnClose").Caption = LanLib.Get("关闭");
|
Buttons.GetButtonByName("btnLanguage").Caption = LanLib.Get("选择语言");
|
|
UpdateButtonCaption();
|
}
|
|
private IDictionary<string, string> DefaultButtonCaption =
|
new Dictionary<string, string>();
|
|
private void SetDynimcButtonLanguage()
|
{
|
foreach (var btn in _buttons.ToArray())
|
{
|
if (LanLib.Current == LanLib.DefautLanguage)
|
if (!DefaultButtonCaption.ContainsKey(btn.Name))
|
DefaultButtonCaption.Add(btn.Name, btn.Caption);
|
|
//默认标题作为ObjectID,获取对应的语言
|
if (DefaultButtonCaption.ContainsKey(btn.Name))
|
btn.Caption = LanLib.Get(DefaultButtonCaption[btn.Name]);
|
}
|
}
|
|
#endregion
|
}
|
}
|