偷梁换柱 --- 管理多个应用程序的壳程序

    以前我们每一个设备有一个管理软件,客户总是为此抱怨,特别是做系统集成的大客户。现在我几乎把所有的管理软件软件用 c#.net 重新写一遍,目的是让系统更强壮,更容易维护,更容易扩展。所有的文件采用 xml 格式,包括设备设置文件,每个设备生成一个 dll, 由一个与设备无关的shell 查询设备设置文件调入。如果每个dll 产生一个 Mdi Container, 同时把 shell 关闭,这是 window 会闪动,而且设备关闭时重新产生 shell 也会闪动。

    这里采用了仅使用一个shell Mdi Container, 切换设备时产生一个设备的Mdi Container,并把其中的 menu, toolbar, statusbar 等载入 shell window, 然后 close 设备的Mdi Container.,设备的Mdi Container 虽然关闭了,但所有事件均能使用,只是要注意,事件中所用的Mdi Container 变成了 shell window.

              protected Form CreateDeviceContext(string deviceID, string firmwareVersion)
             {
                    try {
                             string v = (string) ((CCollectionBase) ((CCollectionBase) Devices["FIRMWARE"])[deviceID])[firmwareVersion];
                             CCollectionBase c = (CCollectionBase) ((CCollectionBase) Devices["CONTEXT"])[v];
                             return (Form) CreateObject((string) c["CLASS"], (string) c["ASSEMBLY"]);
                     }
                     catch {}
                     return null;
              }
 
            private object CreateObject(string objectName, string assemblyName)
            {
                    string dirpath = Application.StartupPath + "//" + assemblyName + ".dll";
                    Assembly assembly = Assembly.LoadFrom(dirpath);
                    return assembly.CreateInstance(objectName);
            }
 
上面的 CreateDeviceContext 是从 devices ( 从设备管理 xml 文件中载入 ) 中根据设备 ID version 找到相应的 dll 设备 mdi window 名称 , 产生设备 mdi Form 同时在 Form 得构造函数中完成对设备的初始化。
 
       public FormDev ic e()
        {
                  InitializeComponent();
                 CDataSet.DataSet.Device = new CDevice();    //create a device
                 CArchive . LoadObjectsFromString(Device.Resources.Resource. Device Config, CDataSet.DataSet.Device.Config, "CONFIG", " DEVICE " );      //load device configuration
                 CDataSet.DataSet.SetpointsSet = CDataSet.DataSet.Device.CreateSetpointsSetFromString(Device.Resources.Resource.Setpoints ) ;    //load setpoints file from a resource file.
                 CDataSet.DataSet.Device.OnDeviceInitialized += OnDeviceInitialized;
         }
 
SwitchContextFromMain 设备 mdi Form 中的所有控件加入到 shell window:
 
        public virtual void SwitchContextFromMain(Form form)
        {
            MainForm.SuspendLayout();
                     form.SuspendLayout();
                     foreach (Control c in MainForm.Controls) {
                            if (!(c is MdiClient))
                                   c.Visible = false;
                     }
                     foreach (Control c in form.Controls) {
                            if (c is MdiClient) continue;
                            m_MainFormCollection.Add(c.Name, c);
                     }
 
                     foreach (object c in m_MainFormCollection) {
                            MainForm.Controls.Add((Control) c);
                            if (c is MenuStrip)
                                   MainForm.MainMenuStrip = (MenuStrip) c;
                     }
                     MainForm.ResumeLayout();
            form.Close();
        }
 
 
这样 shell mdi window 变成了设备的 mdi window, devie 的所有的处理与设计代码仍然独立的 .
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值