C# winform窗体程序-带历史信息的菜单

带历史信息的菜单窗体(附源码)

具体代码如下(图片路径啥的根据自己的位置写)
效果如下图所示
C# winform窗体程序-带历史信息的菜单_第1张图片
步骤
(1)创建一个项目,将其命名为 Ex01_01,默认窗体为 Form1。

(2)从工具箱中向 Form1 窗体添加 MenuStrip 控件,同时向窗体添加 Open
FileDialog 控件。创建一个“文件”主菜单,在其下面创建打开、关闭所有、
退出等菜单选项。
(3)主要程序代码。
将打开文件路径写入 INI 文件的实现代码如下:

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();
ShowWindows(openFileDialog1.FileName);
}

读取 INI 文件并将信息加入菜单的实现代码如下:

private void Form1_Load(object sender, EventArgs e)
{
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();

}

自定义方法 ShowWindows()用来加载背景图片并显
示窗体,实现代码如下:

public void ShowWindows(string fileName)
{
Image p = Image.FromFile(fileName);
Form f = new Form();
f.MdiParent = this;
f.BackgroundImage = p;
f.Show();
}

项目完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ex01_01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            this.openFileDialog1.ShowDialog();
            StreamWriter s = new StreamWriter("D:" + "\\Menu.ini", true);
            s.WriteLine(openFileDialog1.FileName);
            s.Flush();
            s.Close();
            ShowWindows(openFileDialog1.FileName);        }

        private void ShowWindows(string fileName)
        {
            Image p = Image.FromFile("C:\\Users\\Administrator\\Pictures\\5.png");
            Form f = new Form();
            f.MdiParent = this;
            f.BackgroundImage = p;
            f.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader("D:" + "\\Menu.ini");
            int i = this.文件ToolStripMenuItem.DropDownItems.Count - 2;
            while (sr.Peek() >= 0)
            {
                ToolStripMenuItem menuitem = new ToolStripMenuItem(sr.ReadLine());
                this.文件ToolStripMenuItem.DropDownItems.Insert(i, menuitem);
                i++;

                menuitem.Click += new EventHandler(menuitem_Click);
            }
            sr.Close();
        }

        private void menuitem_Click(object sender, EventArgs e)
        {

        }
    }
}

你可能感兴趣的:(C#,c#,开发语言,ui)