65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# 创建 MDI 应用程序(演练)

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.20/5 (38投票s)

2004年6月30日

CPOL

6分钟阅读

viewsIcon

327639

downloadIcon

8945

如果你是初学者(或中级),并且想在 C# 中开发 MDI 应用程序,那么这个就是为你准备的!

Sample Image - myBestMDI.jpg

引言

MDI(Multiple Document Interface,多文档界面)是一种显示窗口表单的方式,其中至少有一个父窗口和多个子窗口。例如,Word、Excel、PowerPoint 等应用程序中的每个文档、工作表或幻灯片都充当父容器窗口下的子窗口。SDI(Single Document Interface,单文档界面)的例子包括 Windows 资源管理器、Internet Explorer、记事本等,其中只有一个窗口充当界面。如果您是初学者(或中级开发者),并希望使用强大的 C# 语言开发具有基本功能的 MDI 应用程序,请按照以下分步指南进行操作。即使是 VB6 背景的开发者,由于 C# 的纯面向对象用法,也会遇到很多问题。遵循这个简短的分步演练来创建一个小型 MDI 应用程序

分步演练

  1. 转到 文件->新建->空白解决方案
  2. 在项目类型中选择 Visual C# 项目
  3. 在模板中选择 Windows 应用程序
  4. 在“名称”文本框中输入,例如:myBestMDI
  5. 通过单击浏览按钮选择位置并单击“确定”

创建所有必要的表单并对其进行编码

创建主 MDI 表单 (frmMDIMain) 作为容器

  1. 在“视图”->“解决方案资源管理器”中单击 Form1 一次
  2. 找到 FileName 属性并键入 "frmMDIMain.cs"
  3. 在解决方案资源管理器中,双击 frmMDIMain
  4. 找到 (Name) 属性并键入 "frmMDIMain"
  5. 找到 Text 属性并键入 "This is the Parent"
  6. 找到 IsMDIContainer 属性并将其设置为 true
  7. 找到 WindowState 属性并将其设置为 Maximized

创建子表单 (frmMChild) 以演示多实例

  1. 转到项目菜单 -> 添加 Windows 窗体,然后键入 frmMChild
  2. 在解决方案资源管理器中,双击 frmMChild
  3. 找到 Text 属性并键入 "This is the Multi Instance Child"
  4. 找到 size 属性并将其设置为 568, 464

创建子表单 (frmSChild) 以演示单实例

  1. 转到项目菜单 -> 添加 Windows 窗体,然后键入 frmSChild
  2. 在解决方案资源管理器中,双击 frmSChild
  3. 找到 Text 属性并键入 "This is the Single Instance Child"
  4. 找到 size 属性并将其设置为 568, 464\

创建关于表单 (frmAbout) 以提供有关此产品的信息

  1. 转到项目菜单 -> 添加 Windows 窗体,然后键入 frmAbout
  2. 在解决方案资源管理器中,双击 frmAbout
  3. 找到 Text 属性并键入 "About My Best MDI"
  4. 找到 size 属性并将其设置为 448, 304
  5. 找到 FormBorderstyle 属性并将其设置为 FixedToolWindow

创建启动画面表单 (frmSplash) 以显示带公司徽标的欢迎信息

  1. 转到项目菜单 -> 添加 Windows 窗体,然后键入 frmSplash
  2. 在解决方案资源管理器中,双击 frmSplash
  3. 找到 size 属性并将其设置为 448, 320
  4. 找到 ControlBox 属性并将其设置为 False
  5. 找到 FormBoderStyle 属性并将其设置为 None
  6. 找到 StartupPosition 属性并将其设置为 CenterScreen
  7. 找到 BackGroundImage 属性并浏览一个漂亮的图像以及您的公司徽标
  8. 找到 cursor 属性并将其设置为 waitCursor

添加一个 Timer 控件到启动画面,以便停留几秒钟,方便用户阅读

  1. 转到“视图”->“工具箱”并双击 Timer 控件
  2. 找到 timer1 控件并单击它一次
  3. 将其 Interval 属性设置为 2000,即毫秒
  4. 将其 Enabled 属性设置为 true
  5. 双击以进入其 Tick 事件
type :- 
timer1.Enabled = false;
this.Close();

添加两个类:一个作为主启动类 (clsMain),一个用于保存全局对象 (clsGlobal)

  1. 在解决方案资源管理器中,右键单击项目名称 myBestMdi
  2. 单击 添加->新建文件夹,然后键入 classes
  3. 右键单击 classes 文件夹
  4. 单击 添加->添加新项,选择类,然后键入 clsGlobal 作为名称

    注意:由于文件夹级别,命名空间 myBestMDI.Classes 会被自动设置为默认命名空间。

  5. 将类的修饰符从
     public class clsGlobal 
    to
     public sealed class clsGlobal 

    (这是为了防止实例化此类的实例)

  6. 在类的大括号刚开始的地方粘贴以下行
    public static frmMDIMain g_objfrmMDIMain;
    //this is to declare a global static reference to our MDI parent so that the same 
    //instance is referred across all child with no extra line of code
  7. 再次在解决方案资源管理器中,右键单击 classes 文件夹
  8. 单击 添加->添加新项,选择类,然后键入 clsMain 作为名称
  9. 在 clsMain 类的构造函数 clsMain 函数正下方粘贴以下代码片段
    [STAThread]
      static void Main() 
      {
       try
       {
        frmSplash objfrmSplash = new frmSplash();
        objfrmSplash.ShowDialog();
        clsGlobal.g_objfrmMDIMain = new frmMDIMain();
        Application.Run(clsGlobal.g_objfrmMDIMain);
       }
       catch(Exception ex)
       {
        MessageBox.Show(ex.Message,"My Best MDI",
         MessageBoxButtons.OK,MessageBoxIcon.Stop);            
       }
      }
    //This is the Single Threaded Apartment Model(out of our scope)
    //of the application which will run the Main function
    //when the application starts
  10. 由于我们在上面的代码中使用了 Application 类和 MessageBox 函数,我们需要在顶部添加以下行:using System.Windows.Forms;
  11. 现在转到 frmMain,并在其代码窗口中删除以下代码片段
[STAThread]
  static void Main() 
  {
   Application.Run(new Form1());
  }
//because we cannot have two Main function.We are invoking everything from clsMain

在主 MDI 表单中创建菜单

  1. 单击“视图”->“frmMdiMain 的设计器”
  2. 转到工具箱并双击 MainMenu 控件
  3. 单击 typehere 菜单一次
  4. 找到 Name 属性并将其设置为 mnuFile
  5. 找到 Text 属性并将其设置为 &File

    注意:& 用于下划线,这将启用快捷键 'F'

  6. 同样,单击子菜单的 TypeHere 框
  7. 找到 Name 属性并将其设置为 mnuFileNewMultiple
  8. 找到 Text 属性并将其设置为 &New Multiple
  9. 找到 ShortCut 属性并选择 Ctrl+N
  10. 同样,单击子菜单的 TypeHere 框
  11. 找到 Name 属性并将其设置为 mnuFileNewSingle
  12. 找到 Text 属性并将其设置为 New &Single
  13. 单击子菜单的 TypeHere 框
  14. 找到 Name 属性并将其设置为 mnuFileCloseChild
  15. 找到 Text 属性并将其设置为 &Close Child
  16. 单击子菜单的 TypeHere 框
  17. 找到 Name 属性并将其设置为 mnuFileSep1
  18. 找到 Text 属性并将其设置为 -

    注意:- 是一个连字符,它会自动添加一个分隔符

  19. 单击子菜单的 TypeHere 框
  20. 找到 Name 属性并将其设置为 mnuFileExit
  21. 找到 Text 属性并将其设置为 E&xit

    现在我相信您应该能够创建另一个菜单了

  22. 创建一个名为 mnuWindow、文本为 &Window 的顶层菜单,并将其 MDIList 属性设置为 true(将显示打开窗口的名称),同时带有 3 个名为
    mnuWindowCascade and text &Cascade
    mnuWindowTileVertical and text Tile&Vertical
    mnuWindowTileHorizontal and text Tile&Horizontal
  23. 创建一个名为 mnuHelp、文本为 &Help 的顶层菜单,并带有一个名为 mnuHelpAbout、文本为 &About... 的子菜单。 "..." 作为后缀是 Microsoft 的一种约定,表示该命令将显示一个对话框。

通过为创建的菜单添加代码来赋予生命

  1. 现在,在 frmSChild 代码中添加以下行
      private static frmSChild m_SChildform;
      public static frmSChild GetChildInstance()
      {
       if (m_SChildform ==null) //if not created yet, Create an instance
        m_SChildform = new frmSChild();
       return m_SChildform;  //just created or created earlier.Return it
      }
    
    //This is to make sure that when we Click on a 'New Single' menu on Parent form twice 
    //it should not open two instance of the same child form
  2. 再次单击“视图”->“frmMdiMain 的设计器”
  3. 双击 MDI 主表单上的“新建单个菜单”

    添加以下代码

    frmSChild objfrmSChild = frmSChild.GetChildInstance();
    objfrmSChild.MdiParent = this;
    objfrmSChild.Show();
    objfrmSChild.BringToFront();
  4. 再次单击“视图”->“frmMdiMain 的设计器”
  5. 双击 MDI 主表单上的“新建多个菜单”

    添加以下代码

    frmMChild objfrmMChild = new frmMChild();
    objfrmMChild.MdiParent = this;
    objfrmMChild.Show();
  6. 双击 MDI 主表单上的“关闭子菜单”,添加以下代码
    try
    {
     if(this.ActiveMdiChild.Name =="frmMChild")
     {
      frmMChild objfrmMChild = (frmMChild)this.ActiveMdiChild;
      objfrmMChild.Close();
     }
     else
     {
      frmSChild objfrmSChild = (frmSChild)this.ActiveMdiChild;
      objfrmSChild.Close();
    
     }
    }
    catch
    {
    }
  7. 双击 MDI 主表单上的“退出菜单”,添加以下代码
    Application.Exit(); 
  8. 双击“窗口”下的“层叠”菜单,添加以下代码
    this.LayoutMdi(MdiLayout.Cascade);
  9. 同样,对于“垂直平铺”,添加
    this.LayoutMdi(MdiLayout.TileVertical);
  10. 对于“水平平铺”,添加
    this.LayoutMdi(MdiLayout.TileHorizontal);
  11. 最后,双击“帮助”菜单中的“关于”以添加
    frmAbout objfrmAbout = new frmAbout();
    objfrmAbout.ShowDialog();
  12. 按 F5 开始调试,或按 Ctrl+F5 开始不调试

Sample screenshot

享受编程吧!!

© . All rights reserved.