作者在 2008-06-25 21:36:41 发布以下内容
要实现保存最近打开过的文件。可以将菜单中最近打开过的文件名和路径事先保存到事先建立好的*.ini文件中。系统启动时读取ini文件中 读数据建立数组菜单,即可以实现这种效果
界面效果如图:
关键代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication3
{
public partial class Form6 : Form
{
string address;
public Form6()
{
InitializeComponent();
address = System.Environment.CurrentDirectory;
}
{
public partial class Form6 : Form
{
string address;
public Form6()
{
InitializeComponent();
address = System.Environment.CurrentDirectory;
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
this.openFileDialog1.ShowDialog();
StreamWriter s = new StreamWriter(address + "\\Menu.ini", true);
s.WriteLine(openFileDialog1.FileName);//写入ini文件
s.Flush();
s.Close();
//ShowWithoutActivation(openFileDialog1.FileName);
ShowWindows(openFileDialog1.FileName);
}
{
openFileDialog1.FileName = "";
this.openFileDialog1.ShowDialog();
StreamWriter s = new StreamWriter(address + "\\Menu.ini", true);
s.WriteLine(openFileDialog1.FileName);//写入ini文件
s.Flush();
s.Close();
//ShowWithoutActivation(openFileDialog1.FileName);
ShowWindows(openFileDialog1.FileName);
}
private void Form6_Load(object sender, EventArgs e)//将ini文件的内容加入到菜单
{
StreamReader sr = new StreamReader(address + "\\Menu.ini");
int i = this.文件ToolStripMenuItem.DropDownItems.Count - 2;
while (sr.Peek() >= 0)//读取ini文件
{
ToolStripMenuItem menuitem = new ToolStripMenuItem(sr.ReadLine());
this.文件ToolStripMenuItem.DropDownItems.Insert(i, menuitem);
i++;
menuitem.Click+=new EventHandler(menuitem_Click);
}
sr.Close();
}
{
StreamReader sr = new StreamReader(address + "\\Menu.ini");
int i = this.文件ToolStripMenuItem.DropDownItems.Count - 2;
while (sr.Peek() >= 0)//读取ini文件
{
ToolStripMenuItem menuitem = new ToolStripMenuItem(sr.ReadLine());
this.文件ToolStripMenuItem.DropDownItems.Insert(i, menuitem);
i++;
menuitem.Click+=new EventHandler(menuitem_Click);
}
sr.Close();
}
public void ShowWindows(string fileName)
{
Image p = Image.FromFile(fileName);
Form f = new Form();
f.MdiParent = this;
f.BackgroundImage = p;
f.Show();
}
{
Image p = Image.FromFile(fileName);
Form f = new Form();
f.MdiParent = this;
f.BackgroundImage = p;
f.Show();
}
private void menuitem_Click(object sender, EventArgs e)
{
ToolStripMenuItem mune = (ToolStripMenuItem)sender;
ShowWindows(mune.Text);
}
}
}
{
ToolStripMenuItem mune = (ToolStripMenuItem)sender;
ShowWindows(mune.Text);
}
}
}
效果图:
———————————————————————————————————————————————————
提供代码测试、视频培训等功能 最新推出ASP.NET快速入门系列视频讲座、张孝祥java视频系列教程等更多的教程不用下载直接在线观看