题目:
这个题目要主要的就是各种控件在实现相同功能的时候不要重复的写,将一样的代码放入到一个函数中去,这样直接调用函数使用起来就会很方便。
还有就是补充了一下图像打开的方式。
对于多文档来说,还有一个需要注意的就是子窗体的控件如何与父窗体的控件融合在一起。
界面:
父窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2
{
public partial class MDIFATHER : Form
{
public MDIFATHER()
{
InitializeComponent();
}
//之前的笔记,这次的作业不涉及新建
//以下是文件菜单的新建、打开、退出
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
int countfile = 0;
MDIChildren mychild = new MDIChildren();
mychild.MdiParent = this;//建立关系
//建立第一个新建文件的时候,没有父窗口的子窗口,所以没有进行循环
//因此后面有countfile++,这时候建立了第一个子窗口,为了激活for循环内实现
//countfile的自动相加,于是设置了isnewfile为ture
for (int i=0;i<this.MdiChildren.Length;i++)
{
//表示当前,如果是新建的话就是ture,false的话再if中就没有作用
//这也就是为什么在打开文件部分里面设置了isnewfile=false
mychild = (MDIChildren)this.MdiChildren[i];
if (mychild.isnewfile) countfile++;
}
countfile++;
mychild.Text = "New File"+countfile;
mychild.isnewfile = true;//为了激活使得countfile进行自动加一
mychild.Show();
//目前新建还有一些问题,就是新建1,2,3后,关闭2,再新建一个,子窗体显示的名称是3
//按道理应该是4
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFile();
}
void OpenFile()
{
OpenFileDialog myfile = new OpenFileDialog();
myfile.Title = "打开图像框";
myfile.Filter = "图像文件|*.bmp;*.jpg;*.png;*.gif|所有文件|*.*";
if (myfile.ShowDialog() == DialogResult.OK)
{
MDIChildren mychild = new MDIChildren();
mychild.MdiParent = this;//建立关系
mychild.Text= myfile.FileName;
//图像打开方式
Bitmap myBitmap1, myBitmap2;
myBitmap1 = new Bitmap(myfile.FileName); //打开“文件名”对应的图像文件并将图像读取到该变量后,该图像文件并没有被关闭,导致在采用原文件名保存时无法保存
//(相当于一个文件被一个文件指针变量打开并读写时,在关闭之前无法用另外一个文件指针变量读写)
myBitmap2 = new Bitmap(myBitmap1); //再将 myBitmap1 中的图像读取到变量 myBitmap2 中
myBitmap1.Dispose(); //随后释放变量 myBitmap1 所占的内存资源,导致该文件不再被变量 myBitmap1 使用,即相当于该文件被关闭了,这样后面保存回原文件的时候才可以保存。
mychild.pictureBox1.Image = myBitmap2; //将变量 myBitmap2 中的图像赋给pictureBox1,
//和“文件名”对应的图像文件没有关系。
mychild.Show();
}
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
//以上是文件菜单的新建、打开、退出
//以下是窗口菜单的子菜单内容
private void 关闭所有子窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach(MDIChildren mychild in this.MdiChildren)
{
mychild.Close();
}
}
private void 排列图标ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.ArrangeIcons);
}
private void 层叠ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.Cascade);
}
private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileVertical);
}
//以下是关于菜单
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("这是一个基于RichTextBox的MDI文本编辑器例子。\n\n中国石油大学(华东)", "说明", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//以下是工具条
private void opentoolStripButton_Click(object sender, EventArgs e)
{
OpenFile();
}
private void savetoolStripButton_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
myActiveMdiChild.savepicture();
//这里没有问题,其实就是是否有子窗体文本框,如果有就可以保存,
//而不是文本内容为空才能保存
}
//以下是工具条的内容
private void othersavetoolStripButton_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
myActiveMdiChild.SaveAspicture();
}
private void abouttoolStripButton_Click(object sender, EventArgs e)
{
MessageBox.Show("这是一个基于RichTextBox的MDI文本编辑器例子。\n\n中国石油大学(华东)", "说明", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void nPToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.pictureBox1.BorderStyle = BorderStyle.None;
}
}
private void fixedSingleToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
}
}
private void fixes3DToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.pictureBox1.BorderStyle = BorderStyle.Fixed3D;
}
}
private void normalToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
}
private void strechImageToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
private void autoSizeToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
}
private void centerImageToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChildren myActiveMdiChild = (MDIChildren)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
}
//以上是主窗口工具条的按钮
}
}
子窗体的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2
{
public partial class MDIChildren : Form
{
public bool isnewfile = false; //区分新建和打开的
public MDIChildren()
{
InitializeComponent();
}
public string GetPictureBoxBorderStyle()
{
switch (pictureBox1.BorderStyle)
{
case BorderStyle.None:
return "None";
case BorderStyle.FixedSingle:
return "FixedSingle";
case BorderStyle.Fixed3D:
return "Fixed3D";
default:
return "";
}
}
public string GetPictureBoxSizeMode()
{
switch (pictureBox1.SizeMode)
{
case PictureBoxSizeMode.Normal:
return "Normal";
case PictureBoxSizeMode.StretchImage:
return "StretchImage";
case PictureBoxSizeMode.AutoSize:
return "AutoSize";
case PictureBoxSizeMode.CenterImage:
return "CenterImage";
case PictureBoxSizeMode.Zoom:
return "Zoom";
default:
return "";
}
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
savepicture();
}
public void savepicture() //直接保存分为两种情况,见下面的注释。
{
if (this.isnewfile)
SaveAspicture(); //若为“新建”菜单项打开的窗口,则保存时使用保存文件对话框,即调用SaveAsFile()方法。
else
this.pictureBox1.Image.Save(this.Text); //若为“打开”菜单项打开的窗口,则保存时直接保存。
}
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveAspicture();
}
public void SaveAspicture()
{
SaveFileDialog mySaveFileDialog = new SaveFileDialog();
mySaveFileDialog.Title = "保存图片";
mySaveFileDialog.Filter = "图像文件|*.bmp;*.jpg;*.gif;*.png|所有文件|*.*";
mySaveFileDialog.CreatePrompt = true;
mySaveFileDialog.OverwritePrompt = true;
string strFileFullName = this.Text, strPathName, strFileName;
int k = strFileFullName.LastIndexOf('\\'); //倒序匹配,返回匹配第一项的下标。
strPathName = strFileFullName.Substring(0, k + 1); //存放路径
strFileName = strFileFullName.Substring(k + 1); //存放文件名
mySaveFileDialog.FileName = strFileName;
if (mySaveFileDialog.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image.Save(mySaveFileDialog.FileName);
this.Text = mySaveFileDialog.FileName;
this.isnewfile = false; //只要保存,变量IsNewFile就变成false。
}
}
private void 文件FToolStripMenuItem_Click(object sender, EventArgs e)
{
}
//以下是设置中的图像边框
private void nOToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.None;
}
private void fixedSingleToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.FixedSingle;
}
private void fixed3DToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
}
//以下是设置中的显示方式
private void normalToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
private void strechImageToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void autoSizeToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void centerImageToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
//以下是状态栏
private void MDIChildren_Activated(object sender, EventArgs e)
{
MDIFATHER myfather = (MDIFATHER)this.MdiParent;
myfather.windowStatusStrip.Text = "当前子窗口文件名:" + this.Text;
myfather.TypeStatusStrip.Text = "当前图像边框类型:" + GetPictureBoxBorderStyle();
myfather.displayStatusStrip.Text = "当前图像显示方式:" + GetPictureBoxSizeMode();
}
private void MDIChildren_Deactivate(object sender, EventArgs e)
{
MDIFATHER myMdiMain = (MDIFATHER)this.MdiParent;
myMdiMain.windowStatusStrip.Text = "当前子窗口文件名:";
myMdiMain.TypeStatusStrip.Text = "当前图像边框类型:";
myMdiMain.displayStatusStrip.Text = "当前图像显示方式:";
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
MDIFATHER myMdiMain = (MDIFATHER)this.MdiParent;
myMdiMain.windowStatusStrip.Text = "当前子窗口文件名:" + this.Text;
myMdiMain.TypeStatusStrip.Text = "当前图像边框类型:" + GetPictureBoxBorderStyle();
myMdiMain.displayStatusStrip.Text = "当前图像显示方式:" + GetPictureBoxSizeMode();
}
//下面是右键快键菜单栏的按钮设置,这里觉得可以改进,不用重复写,而是用一下函数??
private void nOToolStripMenuItem1_Click(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.None;
}
private void fixedSingleToolStripMenuItem1_Click(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.FixedSingle;
}
private void fixed3DToolStripMenuItem1_Click(object sender, EventArgs e)
{
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
}
private void normalToolStripMenuItem1_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
private void stretchImageToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void autoSizeToolStripMenuItem1_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void centerImageToolStripMenuItem1_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void zoomToolStripMenuItem1_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
savepicture();
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveAspicture();
}
//以上是右键快捷键的内容
}
}
希望大家在学习多窗体的时候主要是看怎么实现的,而不是觉得代码能够运行就行,因为单分享代码出来,有一些细节是没有呈现的。
共勉!
标签:MDI,sender,C#,void,object,private,窗体,EventArgs,Click From: https://blog.csdn.net/weixin_74942631/article/details/144786234