| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Windows.Forms;
|
|---|
| 4 | using Microsoft.Win32;
|
|---|
| 5 |
|
|---|
| 6 | namespace Common
|
|---|
| 7 | {
|
|---|
| 8 | class RecentFiles
|
|---|
| 9 | {
|
|---|
| 10 | public List<string> Items = new List<string>();
|
|---|
| 11 | public int MaxCount = 10;
|
|---|
| 12 | public event EventHandler Change;
|
|---|
| 13 |
|
|---|
| 14 | public void AddItem(string fileName)
|
|---|
| 15 | {
|
|---|
| 16 | int index = Items.IndexOf(fileName);
|
|---|
| 17 | if (index != -1) Items.RemoveAt(index);
|
|---|
| 18 | Items.Insert(0, fileName);
|
|---|
| 19 | LimitMaxCount();
|
|---|
| 20 | DoChange();
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | public void LimitMaxCount()
|
|---|
| 24 | {
|
|---|
| 25 | if (Items.Count > MaxCount)
|
|---|
| 26 | Items.RemoveRange(MaxCount, Items.Count - MaxCount);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | public string GetFirstFileName()
|
|---|
| 30 | {
|
|---|
| 31 | if (Items.Count > 0) return Items[0];
|
|---|
| 32 | else return "";
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | private void DoChange()
|
|---|
| 36 | {
|
|---|
| 37 | Change?.Invoke(this, EventArgs.Empty);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | public void LoadFromRegistry(string regSubKey)
|
|---|
| 41 | {
|
|---|
| 42 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey) ??
|
|---|
| 43 | Application.UserAppDataRegistry.CreateSubKey(regSubKey);
|
|---|
| 44 |
|
|---|
| 45 | int count = (int)regKey.GetValue("Count", 0);
|
|---|
| 46 | Items.Clear();
|
|---|
| 47 | for (int i = 0; i < count; i++)
|
|---|
| 48 | {
|
|---|
| 49 | Items.Add((string)regKey.GetValue("FileName" + i, ""));
|
|---|
| 50 | }
|
|---|
| 51 | DoChange();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | public void SaveToRegistry(string regSubKey)
|
|---|
| 55 | {
|
|---|
| 56 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true) ??
|
|---|
| 57 | Application.UserAppDataRegistry.CreateSubKey(regSubKey);
|
|---|
| 58 |
|
|---|
| 59 | regKey.SetValue("Count", Items.Count);
|
|---|
| 60 | for (int i = 0; i < Items.Count; i++)
|
|---|
| 61 | {
|
|---|
| 62 | regKey.SetValue("FileName" + i, Items[i]);
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | public void LoadToMenu(ToolStripMenuItem menuItem, EventHandler handler)
|
|---|
| 67 | {
|
|---|
| 68 | while (menuItem.DropDownItems.Count < Items.Count)
|
|---|
| 69 | {
|
|---|
| 70 | ToolStripMenuItem newItem = new ToolStripMenuItem();
|
|---|
| 71 | newItem.Click += handler;
|
|---|
| 72 | Theme.ApplyTheme(newItem);
|
|---|
| 73 | menuItem.DropDownItems.Add(newItem);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | while (menuItem.DropDownItems.Count > Items.Count)
|
|---|
| 77 | menuItem.DropDownItems.RemoveAt(menuItem.DropDownItems.Count - 1);
|
|---|
| 78 | for (int i = 0; i < Items.Count; i++)
|
|---|
| 79 | {
|
|---|
| 80 | menuItem.DropDownItems[i].Text = Items[i];
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|