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

    以前我们每一个设备有一个管理软件,客户总是为此抱怨,特别是做系统集成的大客户。现在我几乎把所有的管理软件软件用 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 的所有的处理与设计代码仍然独立的 .     文章知识点与官方知识档案匹配,可进一步学习相关知识C技能树首页概览114755 人正在系统学习中

来源:Akron

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2008年6月6日
下一篇 2008年6月7日

相关推荐