![]()
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.IO;//因为下面有用到流文件StreamWriter需定义IO
10
11 namespace history
12 {
13 public partial class Form1 : Form
14 {
15 string address;
16 public Form1()
17 {
18 InitializeComponent();
19 address = System.Environment.CurrentDirectory;//对address定义
20 }
21
22 private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
23 {
24
25 openFileDialog1.FileName = ""; //设定打开文件对话框的初始内容为空
26
27 this.openFileDialog1.ShowDialog(); //显示打开文件对话框
28
29 StreamWriter s = new StreamWriter(address + "\\Menu.ini", true);//定义一个以一种特定的编码向流中写入数据对象
30
31 s.WriteLine(openFileDialog1.FileName); //写入INI文件
32
33 s.Flush(); //清理当前编写器的所有缓冲区,并使所有缓冲数据写入基础流
34
35 s.Close(); //关闭当前的StreamWriter对象和基础流
36
37 ShowWindows(openFileDialog1.FileName); //调用自定义方法ShowWindows
38
39 }
40
41 //读取INI文件并将信息加入菜单
42 private void Form1_Load(object sender, EventArgs e)
43 {
44
45 StreamReader sr = new StreamReader(address + "\\Menu.ini"); //声明一个以一种特定的编码从字节流中读取字符对象
46
47 int i = this.文件ToolStripMenuItem.DropDownItems.Count - 2; //定义一个int型变量i并为它赋值
48
49 while (sr.Peek() >= 0) //读取INI文件
50 {
51
52 ToolStripMenuItem menuitem = new ToolStripMenuItem(sr.ReadLine()); //声明一个ToolStripMenuItem对象
53
54 this.文件ToolStripMenuItem.DropDownItems.Insert(i, menuitem); //向菜单中添加内容
55
56 i++; //int变量i递增
57
58 menuitem.Click += new EventHandler(menuitem_Click); //为菜单中的可选项生成处理程序
59
60 }
61
62 sr.Close(); //关闭当前的StreamReader对象和基础流
63
64 }
65
66 //自定义方法ShowWindows()用来加载背景图片并显示窗体
67 private void menuitem_Click(object sender, EventArgs e)
68 {
69 ToolStripMenuItem menu = (ToolStripMenuItem)sender;
70 ShowWindows(menu.Text);
71 }
72 public void ShowWindows(string fileName)//
73 {
74
75 Image p = Image.FromFile(fileName); //定义一个Image型的变量p
76
77 Form f = new Form(); //定义一个Form型的变量f
78
79 f.MdiParent = this; //设定当前窗体为MDI父窗体
80
81 f.BackgroundImage = p; //为窗体f设置背景图片
82
83 f.Show(); //显示窗体
84
85 }
86
87
88 private void 退出ToolStripMenuItem_Click_1(object sender, EventArgs e)
89 {
90 Application.Exit();
91 }
92
93 private void 关闭所有ToolStripMenuItem_Click_1(object sender, EventArgs e)
94 {
95 foreach (Form frm in this.MdiChildren) //用遍历获取所有子窗体
96 {
97 frm.Close(); //关闭子窗体
98 }
99 }
100
101 }
102 }