RecentFilesMenu.cs
上传用户:hbhltzc
上传日期:2022-06-04
资源大小:1925k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. namespace XmlNotepad {
  7.     public class RecentFileEventArgs : EventArgs {
  8.         public string FileName;
  9.         public RecentFileEventArgs(string fname) {
  10.             this.FileName = fname;
  11.         }
  12.     }
  13.     public delegate void RecentFileHandler(object sender, RecentFileEventArgs args);
  14.     public class RecentFilesMenu {
  15.         List<Uri> recentFiles = new List<Uri>();
  16.         const int maxRecentFiles = 10;
  17.         ToolStripMenuItem parent;
  18.         ComboBox location;
  19.         public event RecentFileHandler RecentFileSelected;
  20.         public RecentFilesMenu(ToolStripMenuItem parent, ComboBox location) {
  21.             this.parent = parent;
  22.             this.location = location;
  23.             this.location.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
  24.             this.location.KeyDown += new KeyEventHandler(OnLocationKeyDown);
  25.         }
  26.         public Uri[] ToArray() {
  27.             return recentFiles.ToArray();
  28.         }
  29.         public void Clear() {
  30.             recentFiles.Clear();
  31.         }
  32.         public void SetFiles(Uri[] files) {
  33.             Clear();
  34.             foreach (Uri fileName in files) {
  35.                 AddRecentFileName(fileName);
  36.             }
  37.             SyncRecentFilesMenu();
  38.         }
  39.         bool addingFile;
  40.         void AddRecentFileName(Uri fileName) {
  41.             try {
  42.                 addingFile = true;
  43.                 if (this.recentFiles.Contains(fileName)) {
  44.                     this.recentFiles.Remove(fileName);
  45.                 }
  46.                 string fname = fileName.IsFile ? fileName.LocalPath : fileName.AbsoluteUri;
  47.                 if (this.location.Items.Contains(fname)) {
  48.                     this.location.Items.Remove(fname);
  49.                 }
  50.                 if (fileName.IsFile && !File.Exists(fileName.LocalPath)) {
  51.                     return; // ignore deleted files.
  52.                 }
  53.                 this.recentFiles.Add(fileName);
  54.                 this.location.Items.Insert(0, fname);
  55.                 if (this.recentFiles.Count > maxRecentFiles) {
  56.                     this.recentFiles.RemoveAt(0);
  57.                 }
  58.                 if (this.location.Items.Count > maxRecentFiles) {
  59.                     this.location.Items.RemoveAt(this.location.Items.Count - 1);
  60.                 }
  61.                 this.location.SelectedIndex = 0;
  62.             } catch (System.UriFormatException) {
  63.                 // ignore bad file names
  64.             } catch (System.IO.IOException) {
  65.                 // ignore bad files
  66.             } finally {
  67.                 addingFile = false;
  68.             }
  69.         }
  70.         public void AddRecentFile(Uri fileName) {
  71.             AddRecentFileName(fileName);
  72.             SyncRecentFilesMenu();            
  73.         }
  74.         void SyncRecentFilesMenu() {
  75.             // Synchronize menu items.
  76.             if (this.recentFiles.Count == 0) {
  77.                 return;
  78.             }
  79.             this.parent.Enabled = true;
  80.             
  81.             ToolStripItemCollection ic = this.parent.DropDownItems;
  82.             // Add most recent files first.
  83.             for (int i = this.recentFiles.Count-1, j = 0; i >= 0; i--, j++) {
  84.                 ToolStripItem item = null;
  85.                 if (ic.Count > j) {
  86.                     item = ic[j];
  87.                 } else {
  88.                     item = new ToolStripMenuItem();
  89.                     item.Click += new EventHandler(OnRecentFile);
  90.                     ic.Add(item);
  91.                 }
  92.                 Uri uri = this.recentFiles[i];
  93.                 item.Text = uri.IsFile ? uri.LocalPath : uri.AbsoluteUri;  
  94.             }
  95.             // Remove any extra menu items.
  96.             for (int i = ic.Count - 1, n = this.recentFiles.Count; i > n; i--) {
  97.                 ic.RemoveAt(i);
  98.             }
  99.         }
  100.         void OnRecentFile(object sender, EventArgs e) {
  101.             if (this.RecentFileSelected != null) {
  102.                 ToolStripItem ts = (ToolStripItem)sender;
  103.                 this.RecentFileSelected(sender, new RecentFileEventArgs(ts.Text));
  104.             }
  105.         }
  106.         void OnSelectedIndexChanged(object sender, EventArgs e) {
  107.             if (!addingFile && this.RecentFileSelected != null) {
  108.                 this.RecentFileSelected(sender, new RecentFileEventArgs(this.location.Text));
  109.             }
  110.         }
  111.         void OnLocationKeyDown(object sender, KeyEventArgs e) {
  112.             if (e.KeyCode == Keys.Enter) {
  113.                 // user has entered something new?
  114.                 e.Handled = true;
  115.                 e.SuppressKeyPress = true;
  116.                 this.RecentFileSelected(sender, new RecentFileEventArgs(this.location.Text));
  117.             }
  118.         }
  119.     }
  120. }