C#下MDI形式的图片查看器

C#下MDI形式的图片查看器
2011年08月27日
  C#下MDI形式的图片查看器
  所谓MDI,就是多文档界面,我们平常在用Word、Excel时碰到的就是MDI。用MDI可以在一个应用程序同时打开多个视图窗口对应不同的文档类,所以就大大提高了程序的工作效率。
  Visual C#是微软公司推出的下一代程序开发语言,是微软.Net 框架中的的一个重要组成部分,在推出Visual C#的过程中,微软公司还推出了与之相对应的一个软件开发包--.Net FrameWork SDK。此软件开发包里面封装了许多类、对象。Visual C#就是通过调用这些类、对象来实现许多
  比较强大的功能。但是在Visual C#下没有像MFC直接提供SDI、MDI和具有对话框的工程向导,所以只能自己动手完成所需要的工作了。然而,正因为C#里封装了许多功能强大的类和对象,所以在C#下进行MDI编程是件相当容易的事。
  本文就通过用C#编写一个MDI形式的图片查看器向大家介绍C#下进行MDI编程的方法和技巧。
  系统要求:
  (1)微软公司视窗2000服务器版或视窗 XP 版
  (2).Net FrameWrok SDK Beta 2版
  实现方法:
  首先,在VS.net下建议一个C#的工程,取名为“ImageViewer”,图示如下:
  接着,开始界面的设计:
  将主窗口的IsMdiContainer属性设置为True,Text属性设置为“图片查看器”。往主窗口上添加一个MainMenu和一个TabControl控件。
  编辑MainMenu控件如下图(1)、图(2):
  图(1)
  图(2)
  为了使“窗口”菜单下能显示所有的子窗口,要将“窗口”菜单的MdiList属性设置为True。
  设计TabControl:将它的Dock属性设置为Bottom,Appearance属性设置为FlatButtons,HotTrack、ShowToolTips属性均设置为True。
  好,主窗口已经设计好了,现在要设计MDI子窗口了。为此,我们先为程序添加另一个窗体作为子窗口。选择菜单:项目->添加Windows窗体,建立一个名为Form2的新窗体。如图:
  为了能在Form2上显示图片,我们需要添加一个pictureBox控件。(如果将显示的图片设置为Form2的背景的话,运行时将会非常慢!)将pictureBox控件的Dock属性设置为Fill(使它占据整个Form2的客户区),Modifiers属性设置为Public(使它能被主窗口访问到),SizeMode属性设置为
  StretchImage。
  现在我们开始编写代码。
  添加Form1类的数据成员如下:
  private Int32 filterIndex = -1; //打开文件对话框的文件过滤器
  private int FormCount; //记录之窗口的数目
  在构造函数里添加:
  FormCount=0;//将子窗口数目设置为0
  添加各个主菜单的消息响应函数如下:
  private void menuItem2_Click(object sender, System.EventArgs e)
  {
  OpenFileDialog ofd = new OpenFileDialog();//新建一个打开文件对话框
  ofd.Filter = "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png"
  + "|JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg"
  + "|GIF files (*.gif)|*.gif"
  + "|BMP files (*.bmp)|*.bmp"
  + "|Tiff files (*.tif;*.tiff)|*.tif;*.tiff"
  + "|Png files (*.png)|*.png"
  + "|All files (*.*)|*.*";//文件类型过滤器
  if (this.filterIndex != -1) ofd.FilterIndex = this.filterIndex;
  if (ofd.ShowDialog() == DialogResult.OK)
  {
  string strFileName = ofd.FileName;//获得文件路径
  string strShortName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);//获得文件名
  if (strFileName.Length != 0)
  {
  this.filterIndex = ofd.FilterIndex;
  try
  {
  Bitmap img = new Bitmap(strFileName);
  Form2 frmTemp = new Form2();//新建一个窗体
  frmTemp.MdiParent = this;
  //定义此窗体的父窗体,从而此窗体成为一个MDI窗体
  frmTemp.WindowState = FormWindowState.Maximized;
  //使窗体一开始就最大
  frmTemp.pictureBox1.Image = img;//设置图片
  frmTemp.AutoScroll = true;
  frmTemp.Text = strShortName;//设定MDI窗体的标题
  FormCount++ ;//窗体数目增加一
  frmTemp.Show ();//把此MDI窗体显示出来
  TabPage ctlPage = new TabPage();
  ctlPage.ToolTipText = strFileName;
  ctlPage.Text = strShortName + " ";
  ctlPage.ImageIndex = 1;
  ctlPage.Tag = frmTemp;
  frmTemp.Tag = ctlPage;
  tabControl1.TabPages.Add(ctlPage);//添加一项Tab
  }
  catch
  {
  MessageBox.Show(String.Format("{0} is not a valid image file", strFileName),"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  }
  }
  }
  }
  private void menuItem3_Click(object sender, System.EventArgs e)
  {
  this.Close();//退出程序
  }
  private void menuItem5_Click(object sender, System.EventArgs e)
  {
  this.LayoutMdi(MdiLayout.Cascade);//层叠窗口
  }
  private void menuItem6_Click(object sender, System.EventArgs e)
  {
  this.LayoutMdi(MdiLayout.TileHorizontal);//横向平铺窗口
  }
  private void menuItem7_Click(object sender, System.EventArgs e)
  {
  this.LayoutMdi(MdiLayout.TileVertical);//纵向平铺窗口
  }
  最后,添加tabControl的MouseUp消息函数(注意,只能是MouseUp,MouseDown是不行的),使之能够在各个子窗口之间进行切换。
  private void tabControl1_MouseUp
  (object sender, System.Windows.Forms.MouseEventArgs e)
  {
  Form2 frm = (Form2)tabControl1.SelectedTab.Tag;//激活新的子窗口
  frm.Activate();
  }
  现在已经完成了所有的工作了,按Ctrl+F5试试效果吧!
   
  完整代码:
  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  namespace ImageViewer
  {
  ///
  /// Summary description for Form1.
  ///
  public class Form1 : System.Windows.Forms.Form
  {
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.TabControl tabControl1;
  private System.Windows.Forms.MenuItem menuItem1;
  private System.Windows.Forms.MenuItem menuItem2;
  private System.Windows.Forms.MenuItem menuItem3;
  private System.Windows.Forms.MenuItem menuItem4;
  private System.Windows.Forms.MenuItem menuItem5;
  private System.Windows.Forms.MenuItem menuItem6;
  private System.Windows.Forms.MenuItem menuItem7;
  private Int32 filterIndex = -1;
  private int FormCount;
  private System.Windows.Forms.MdiClient mdiClient1;
  ///
  /// Required designer variable.
  ///
  private System.ComponentModel.Container components = null;
  public Form1()
  {
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();
  FormCount=0;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
  }
  ///
  /// Clean up any resources being used.
  ///
  protected override void Dispose( bool disposing )
  {
  if( disposing )
  {
  if (components != null)
  {
  components.Dispose();
  }
  }
  base.Dispose( disposing );
  }
  #region Windows Form Designer generated code
  ///
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  ///
  private void InitializeComponent()
  {
  this.mdiClient1 = new System.Windows.Forms.MdiClient();
  this.tabControl1 = new System.Windows.Forms.TabControl();
  this.mainMenu1 = new System.Windows.Forms.MainMenu();
  this.menuItem1 = new System.Windows.Forms.MenuItem();
  this.menuItem2 = new System.Windows.Forms.MenuItem();
  this.menuItem3 = new System.Windows.Forms.MenuItem();
  this.menuItem4 = new System.Windows.Forms.MenuItem();
  this.menuItem5 = new System.Windows.Forms.MenuItem();
  this.menuItem6 = new System.Windows.Forms.MenuItem();
  this.menuItem7 = new System.Windows.Forms.MenuItem();
  this.SuspendLayout();
  //
  // mdiClient1
  //
  this.mdiClient1.Dock = System.Windows.Forms.DockStyle.Fill;
  this.mdiClient1.Name = "mdiClient1";
  this.mdiClient1.TabIndex = 2;
  //
  // tabControl1
  //
  this.tabControl1.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
  this.tabControl1.Dock = System.Windows.Forms.DockStyle.Bottom;
  this.tabControl1.HotTrack = true;
  this.tabControl1.Location = new System.Drawing.Point(0, 305);
  this.tabControl1.Name = "tabControl1";
  this.tabControl1.SelectedIndex = 0;
  this.tabControl1.ShowToolTips = true;
  this.tabControl1.Size = new System.Drawing.Size(432, 24);
  this.tabControl1.TabIndex = 1;
  this.tabControl1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseUp);
  //
  // mainMenu1
  //
  this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  this.menuItem1,
  this.menuItem4});
  //
  // menuItem1
  //
  this.menuItem1.Index = 0;
  this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  this.menuItem2,
  this.menuItem3});
  this.menuItem1.Text = "文件(&F)";
  //
  // menuItem2
  //
  this.menuItem2.Index = 0;
  this.menuItem2.Text = "打开(&O)";
  this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
  //
  // menuItem3
  //
  this.menuItem3.Index = 1;
  this.menuItem3.Text = "退出(&X)";
  this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
  //
  // menuItem4
  //
  this.menuItem4.Index = 1;
  this.menuItem4.MdiList = true;
  this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  this.menuItem5,
  this.menuItem6,
  this.menuItem7});
  this.menuItem4.Text = "窗口(&W)";
  //
  // menuItem5
  //
  this.menuItem5.Index = 0;
  this.menuItem5.Text = "层叠窗口(&C)";
  this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
  //
  // menuItem6
  //
  this.menuItem6.Index = 1;
  this.menuItem6.Text = "横向平铺窗口(&H)";
  this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
  //
  // menuItem7
  //
  this.menuItem7.Index = 2;
  this.menuItem7.Text = "纵向平铺窗口(&V)";
  this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
  //
  // Form1
  //
  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  this.ClientSize = new System.Drawing.Size(432, 329);
  this.Controls.AddRange(new System.Windows.Forms.Control[] {
  this.tabControl1,
  this.mdiClient1});
  this.IsMdiContainer = true;
  this.Menu = this.mainMenu1;
  this.Name = "Form1";
  this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  this.Text = "图片查看器";
  this.ResumeLayout(false);
  }
  #endregion
  ///
  /// The main entry point for the application.
  ///
  [STAThread]
  static void Main()
  {
  Application.Run(new Form1());
  }
  private void menuItem2_Click(object sender, System.EventArgs e)
  {
  OpenFileDialog ofd = new OpenFileDialog();//新建一个打开文件对话框
  ofd.Filter = "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png"
  + "|JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg"
  + "|GIF files (*.gif)|*.gif"
  + "|BMP files (*.bmp)|*.bmp"
  + "|Tiff files (*.tif;*.tiff)|*.tif;*.tiff"
  + "|Png files (*.png)|*.png"
  + "|All files (*.*)|*.*";//文件类型过滤器
  if (this.filterIndex != -1) ofd.FilterIndex = this.filterIndex;
  if (ofd.ShowDialog() == DialogResult.OK)
  {
  string strFileName = ofd.FileName;//获得文件路径
  string strShortName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);//获得文件名
  if (strFileName.Length != 0)
  {
  this.filterIndex = ofd.FilterIndex;
  try
  {
  Bitmap img = new Bitmap(strFileName);
  Form2 frmTemp = new Form2();//新建一个窗体
  frmTemp.MdiParent = this;
  //定义此窗体的父窗体,从而此窗体成为一个MDI窗体
  frmTemp.WindowState = FormWindowState.Maximized;
  //使窗体一开始就最大
  frmTemp.pictureBox1.Image = img;//设置图片
  frmTemp.AutoScroll = true;
  frmTemp.Text = strShortName;//设定MDI窗体的标题
  FormCount++ ;//窗体数目增加一
  frmTemp.Show ( ) ;//把此MDI窗体显示出来
  TabPage ctlPage = new TabPage();
  ctlPage.ToolTipText = strFileName;
  ctlPage.Text = strShortName + " ";
  ctlPage.ImageIndex = 1;
  ctlPage.Tag = frmTemp;
  frmTemp.Tag = ctlPage;
  tabControl1.TabPages.Add(ctlPage);//添加一项Tab
  }
  catch
  {
  MessageBox.Show(String.Format("{0} is not a valid image file",strFileName),"Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  }
  }
  }
  }
  private void menuItem3_Click(object sender, System.EventArgs e)
  {
  this.Close();//退出程序
  }
  private void menuItem5_Click(object sender, System.EventArgs e)
  {
  this.LayoutMdi(MdiLayout.Cascade);//层叠窗口
  }
  private void menuItem6_Click(object sender, System.EventArgs e)
  {
  this.LayoutMdi(MdiLayout.TileHorizontal);//横向平铺窗口
  }
  private void menuItem7_Click(object sender, System.EventArgs e)
  {
  this.LayoutMdi(MdiLayout.TileVertical);//纵向平铺窗口
  }
  private void tabControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
  Form2 frm = (Form2)tabControl1.SelectedTab.Tag;//激活新的子窗口
  frm.Activate();
  }
  }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值