- 浏览: 10488 次
最新评论
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();
}
}
}
发表评论
-
多线程加速图像模板匹配
2012-01-20 10:13 677多线程加速图像模板匹 ... -
VC 打开外部文件
2012-01-20 10:13 633VC 打开外部文件 2010年0 ... -
Windows消息机制VC
2012-01-20 10:13 600Windows消息机制VC 2011年0 ... -
使用 C++ 和 MFC 进行多线程编程
2012-01-20 10:13 1088使用 C++ 和 MFC 进行多线程编程 2010年11月0 ... -
windows程序设计相关思想
2012-01-20 10:13 509windows程序设计相关思想 ... -
WinCE驱动开发问题精华集锦
2012-01-19 15:14 466WinCE驱动开发问题精华 ... -
android 各系统区别(android 1.5- android 3.0)
2012-01-19 15:14 352android 各系统区别(android ... -
WinCE驱动开发问题精华集锦(一)
2012-01-19 15:14 290WinCE驱动开发问题精华 ... -
《电脑十万个为什么》 第四卷 第一章
2012-01-19 15:14 669《电脑十万个为什么》 第四卷 第一章 2010年06月07日 ... -
C#面试题集锦(你值得拥有!)
2012-01-17 05:01 561C#面试题集锦(你值得拥有!) 2011年04月22日 ... -
windows编程---在窗口上建立控件
2012-01-17 05:01 582windows编程---在窗口上建立控件 2011年05月3 ... -
Flex开源项目网址列表
2012-01-16 03:52 599Flex开源项目网址列表 2011年06月19日 1. ... -
使用ArcGIS API for Flex开发的地图应用网站
2012-01-16 03:52 705使用ArcGIS API for Flex开发的地图应用网站 ... -
flex类网站收集
2012-01-16 03:52 592flex类网站收集 2010年10月22日 1. Fle ... -
用flex开发 google map应用程式
2012-01-16 03:51 444用flex开发 google map应用程式 2009年09 ... -
N多的好资源链接
2012-01-16 03:51 911N多的好资源链接 2009年07月22日 http:// ...
相关推荐
全书分6篇共25章,主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的使用、面向对象编程技术、数据结构与算法、Windows窗体基础、特色窗体界面、窗体控制技术、MDI窗体和继承窗体、Windows...
《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用...
│ ├─实例80 如何获取CPU信息 │ │ ├─实例81 如何浏览注册表信息 │ │ ├─实例82 如何隐藏系统驱动器 │ │ ├─实例83 如何清理系统回收站 │ │ ├─实例84 如何制作日志查看器 │ │ ├─...
C#编程经验技巧宝典源代码,目录如下: 第1章 开发环境 1 <br>1.1 Visual Studio开发环境安装与配置 2 <br>0001 安装Visual Studio 2005开发环境须知 2 <br>0002 配置合适的Visual Studio 2005...
第1章 Visual C#2008与窗体界面 案例1 飘动动画窗体 案例2 透明动画窗体 案例3 利用API函数实现动画窗体 案例4 闪烁动画窗体 案例5 滚动字幕动画窗体 案例6 超女卡通窗体 案例7 总在最前的登录窗体 案例8 在屏幕...
│ │ ├─实例84 如何制作日志查看器 │ │ ├─实例85 如何禁止关闭操作系统 │ │ ├─实例86 如何禁止屏幕保护程序 │ │ └─实例87 如何禁止系统菜单按钮命令 │ ├─第5章 图像处理 │ │ ├─实例100 如何以...
│ ├─实例80 如何获取CPU信息 │ │ ├─实例81 如何浏览注册表信息 │ │ ├─实例82 如何隐藏系统驱动器 │ │ ├─实例83 如何清理系统回收站 │ │ ├─实例84 如何制作日志查看器 │ │ ├─...
45 实例044 在关闭窗口前加入确认对话框 45 实例045 使用任意组件拖动窗体 46 实例046 修改提示字体及颜色 47 1.14 其他技术 48 实例047 如何为MDI类型窗体设置背景图片 48 实例048 向...
案例10 多文档MDI窗体 案例11 渐变色窗体 案例12 笑脸窗体 案例13 八边形图形窗体 本章小结 第2章 Visual C#2008与图形图像 案例1 动态绘制直线和曲线 案例2 动态绘制验证码 案例3 椭圓及椭圆弧的绘制 案例...
Visual+C#+2008程序设计经典案例设计与实现 第1章 Visual C#2008与窗体界面 案例1 飘动动画窗体 案例2 透明动画窗体 案例3 利用API函数实现动画窗体 案例4 闪烁动画窗体 案例5 滚动字幕动画窗体 案例6 超女卡通...
第1章 Visual C#2008与窗体界面 案例1 飘动动画窗体 案例2 透明动画窗体 案例3 利用API函数实现动画窗体 案例4 闪烁动画窗体 案例5 滚动字幕动画窗体 案例6 超女卡通窗体 案例7 总在最前的登录窗体 案例8 在屏幕...
案例1.1 多文档MDI应用程序 案例1.2 QQ窗体 案例1.3 卡通窗体, 案例1.4 带有分隔栏并更换主界面背景窗体 案例1.5 半透明渐显动画和渐变窗体 案例1.6 浮动的窗体 案例1.7 在屏幕中央并总在最前窗体 案例1.8 ...
随着经济的飞速发展人们的收入越来越高,相应的娱乐设施也越来越多,电影是人们的最佳选择,那么影院的售票及管理系统是一个供观众直观查看影片信息的系统,可以为观众提供影片的播放时间、播放的地点、影片的风格...
支持多种窗口模式(MDI,选项卡式,浮动式) 保存并加载整个窗口会话。 作为命令行参数给定时,可以在启动时加载会话文件。 句子突出显示,颜色根据关键字匹配而定(包括正则表达式支持) 使用键盘快捷键在突出...
2.14 查看C++/CLI类型 96 2.15 小结 97 2.16 练习 97 2.17 本章主要内容 98 第3章 判断和循环 101 3.1 比较数据值 101 3.1.1 if语句 102 3.1.2 嵌套的if语句 104 3.1.3 嵌套的if-else语句 107 3.1.4 逻辑...