程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#小程序—帶歷史的菜單

C#小程序—帶歷史的菜單

編輯:C#入門知識

 前幾天寫了個帶歷史菜單功能的程序,寫的不是很好,太過於復雜。昨天我發現一種新方法,於是從寫一遍。動態添加菜單的代碼如下:
[csharp] 
//Menu.ini 是配置文件,來存儲歷史。 
if (File.Exists("Menu.ini")) 

    StreamReader sr = new StreamReader("Menu.ini"); 
    int i = this.文件ToolStripMenuItem.DropDownItems.Count - 2; 
    while (sr.Peek() > 0) 
    { 
        ToolStripMenuItem tlomenu = new ToolStripMenuItem(sr.ReadLine()); 
        this.文件ToolStripMenuItem.DropDownItems.Insert(i, tlomenu); 
        i++; 
    } 
    sr.Close(); 

              我還把程序的流程用Visio簡單的畫了一下。

        
         下面是幫助窗口的代碼:
        
[csharp] 
/調用API函數 
        [DllImport("user32")] 
        private static extern long FlashWindow(IntPtr handle, bool bInvert); 
 
        private void btnClose_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
 
        private void timer1_Tick(object sender, EventArgs e) 
        { 
            FlashWindow(this.Handle, true); 
        } 
        
          選擇字體窗口代碼:
   
[csharp]
public FrmFont(Frm frm) 
        { 
            InitializeComponent(); 
            mianFrm = frm; 
        } 
 
        Frm mianFrm = new Frm(); 
        //定義中間變量. 
        public Font newFont; 
        public bool getBlod; 
        public bool getItalic; 
 
         
        //向程序中添加字體和大小. 
        private void FrmFont_Load(object sender, EventArgs e) 
        { 
            InstalledFontCollection iof = new InstalledFontCollection(); 
            foreach (FontFamily font in iof.Families) 
            { 
                cboFont.Items.Add(font.Name); 
            } 
            for (int i = 1; i < 100; i++) 
                cboSize.Items.Add(i); 
        } 
 
        public void UpdateFrmMain() 
        { 
            if (cboFont.SelectedIndex > 0) 
            { 
                try 
                { 
                    newFont = new Font(cboFont.SelectedItem.ToString(), newFont.Size); 
                } 
                catch 
                { } 
            } 
            if (cboSize.SelectedIndex > 0) 
            { 
                newFont = new Font(newFont.FontFamily, float.Parse(cboSize.SelectedItem.ToString())); 
            } 
            mianFrm.rtbNotePad.Font = newFont; 
        } 
        //字體選擇變化 
        private void cboFont_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            UpdateFrmMain(); 
        } 
        //字體大小下拉欄變化 
        private void cboSize_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            UpdateFrmMain(); 
        } 
 
           程序運行的圖片如下:


         
  主窗體代碼如下:
[csharp] 
//定義變量 
        StreamWriter str=null; 
        string strTemp; 
        string path = System.IO.Directory.GetCurrentDirectory(); 
 
        private void 幫助ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            //調用幫助窗體 
            FrmHelp help = new FrmHelp(); 
            help.ShowDialog(); 
        } 
         
        //打開文件 
        private void 打開ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            OpenFileDialog ofd = new OpenFileDialog(); 
            if (ofd.ShowDialog() == DialogResult.OK) 
            { 
                this.rtbNotePad.Text+=ofd.FileName; 
                this.rtbNotePad.LoadFile(ofd.FileName,RichTextBoxStreamType.PlainText); 
                string ShortFileName = ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1); 
                this.Text = ShortFileName; 
 
                ToolStripMenuItem tlomenu = new ToolStripMenuItem(ofd.FileName); 
                int i = this.文件ToolStripMenuItem.DropDownItems.Count - 2; 
                this.文件ToolStripMenuItem.DropDownItems.Insert(i, tlomenu); 
                tlomenu.Click += new EventHandler(ToolStripMenuItem_Click); 
            } 
        } 
 
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            SaveFileDialog ofd = new SaveFileDialog (); 
            if(ofd.ShowDialog()==DialogResult.OK) 
            { 
                str = new StreamWriter(ofd.FileName, false); 
            } 
        } 
         
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
 
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            if (str != null) 
            { 
                strTemp = rtbNotePad.Text; 
                str.Write(strTemp); 
            } 
        } 
 
        private void Frm_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            try 
            { 
                str.Close(); 
            } 
            catch 
            { }; 
        } 
 
        private void 字體ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            FrmFont frmfont = new FrmFont(this); 
            Font tempFont = rtbNotePad.Font; 
            frmfont.newFont = rtbNotePad.Font; 
            frmfont.ShowDialog(); 
        } 
 
        private void Frm_Load(object sender, EventArgs e) 
        { 
            if (File.Exists("Menu.ini")) 
            { 
                haveMenu = true; 
                string temp; 
                StreamReader sr = new StreamReader("Menu.ini",Encoding.Default,false); 
                int i = this.文件ToolStripMenuItem.DropDownItems.Count - 2; 
                while ((temp=sr.ReadLine())!= null) 
                { 
                    ToolStripMenuItem tlomenu = new ToolStripMenuItem(temp); 
                    this.文件ToolStripMenuItem.DropDownItems.Insert(i, tlomenu); 
                    tlomenu.Click += new EventHandler(ToolStripMenuItem_Click); 
                    i++; 
                } 
                sr.Close(); 
            } 
        } 
 
        private void ToolStripMenuItem_Click(object sender, EventArgs e) 
        {   www.2cto.com
            //點擊歷史記錄 
            string temp = ((ToolStripMenuItem)sender).Text; 
            this.rtbNotePad.LoadFile(temp, RichTextBoxStreamType.PlainText); 
            string ShortFileName = temp.Substring(temp.LastIndexOf("\\") + 1); 
            this.Text = ShortFileName; 
 
            string newpath = path + @"\Menu.ini"; 
            StreamWriter sr = new StreamWriter(newpath, true, Encoding.Default); 
            sr.WriteLine(temp); 
            sr.Close(); 
        } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved