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

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

具体代码如下(图片路径啥的根据自己的位置写)
效果如下图所示
在这里插入图片描述
步骤
(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)
        {

        }
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薪薪代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值