• 带历史信息的菜单


    注:要建立一个带历史信息的菜单,将主窗体设置为父窗体。并将在菜单中最近打开文件的文件名和路径保存到事先建立的*.ini文件中。

     

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

    namespace MenuHistory1
    {
        public partial class frmMenuHistory : Form
        {
            string adress;
            public frmMenuHistory()
            {
                InitializeComponent();

                //获取或设置当前工作目录的完全限定路径

                adress = System.Environment.CurrentDirectory;        

            }

            /// <summary>
            /// 将打开的文件路径写入INI文件       

            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void tsmiOpen_Click(object sender, EventArgs e)
            {
                this.openFileDialog1.ShowDialog();
                StreamWriter sw = new StreamWriter(adress + "\\
    menu.ini", true);
                sw.WriteLine(openFileDialog1.FileName); //写入INI文件
                sw.Flush();
                sw.Close();
                ShowWindows(openFileDialog1.FileName);
            }

            /// <summary>
            /// 读取INI文件并将信息加入菜单的实现代码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void frmMenuHistory_Load(object sender, EventArgs e)
            {
                StreamReader sr = new StreamReader(adress + "\\menu.ini"
    );
                int i = this.tsmiFile.DropDownItems.Count - 2;
                while (sr.Peek() > 0)
                {
                    ToolStripMenuItem menuitem = new ToolStripMenuItem(sr.ReadLine());
                    this.tsmiFile.DropDownItems.Insert(i, menuitem);
                    i++;
                    menuitem.Click += new EventHandler(menuitem_Click); //表示将处理不包含事件数据的事件的方法
                }
            }

            private void menuitem_Click(object sender, EventArgs e)
            {
                ToolStripMenuItem menu = (ToolStripMenuItem)sender;
                ShowWindows(menu.Text);
            }

            /// <summary>
            /// 用来加载背景图片并显示窗体的自定义方法。
            /// </summary>
            /// <param name="fileName"></param>
            private void ShowWindows(string fileName)
            {
                Image pic = Image.FromFile(fileName);
                Form f = new Form();
                f.MdiParent = this;
                f.BackgroundImage = pic;
                f.Show();
            }

            private void tsmiExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

        }
    }

  • 相关阅读:
    Revit API改变风管及管件尺寸
    Revit API注册事件
    Revit API创建标高,单位转换
    Revit API判断直线相交关系移动风管
    Revit MEP API找到连接器连接的连接器
    Revit MEP API连接器类别
    AngularJS如何编译和呈现页面
    AngularJS自定义Directive初体验
    Webpack基本用法
    对一个前端AngularJS,后端OData,ASP.NET Web API案例的理解
  • 原文地址:https://www.cnblogs.com/sdustyuleyi/p/2384635.html
Copyright © 2020-2023  润新知