Changeset 15
- Timestamp:
- Jun 18, 2024, 12:15:33 PM (5 months ago)
- Location:
- Common
- Files:
-
- 10 added
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/ComboBoxEx.cs
r14 r15 8 8 public partial class ComboBoxEx : ComboBox 9 9 { 10 public string regSubKey;11 public int maxHistoryCount;10 public string RegSubKey; 11 public int MaxHistoryCount; 12 12 13 13 public ComboBoxEx() 14 14 { 15 15 InitializeComponent(); 16 regSubKey = Name;17 maxHistoryCount = 20;16 RegSubKey = Name; 17 MaxHistoryCount = 20; 18 18 } 19 19 … … 23 23 { 24 24 BeginUpdate(); 25 string previousText = Text; 26 int index = Items.IndexOf(Text); 27 if (index >= 0) Items.RemoveAt(index); 28 Text = previousText; 29 Items.Insert(0, previousText); 25 try 26 { 27 string previousText = Text; 28 int index = Items.IndexOf(Text); 29 if (index >= 0) Items.RemoveAt(index); 30 Text = previousText; 31 Items.Insert(0, previousText); 30 32 31 while (Items.Count > maxHistoryCount) 32 Items.RemoveAt(Items.Count - 1); 33 EndUpdate(); 33 while (Items.Count > MaxHistoryCount) 34 Items.RemoveAt(Items.Count - 1); 35 } 36 finally 37 { 38 EndUpdate(); 39 } 34 40 } 35 41 } … … 37 43 public void LoadFromRegistry() 38 44 { 39 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey( regSubKey);40 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);45 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey) ?? 46 Application.UserAppDataRegistry.CreateSubKey(RegSubKey); 41 47 42 48 int count = regKey.GetValueInt("Count", 0); 43 49 BeginUpdate(); 44 while (Items.Count > count) Items.RemoveAt(Items.Count - 1); 45 while (Items.Count < count) Items.Add(""); 46 for (int i = 0; i < count; i++) 50 try 47 51 { 48 Items[i] = (string)regKey.GetValue(i.ToString(), ""); 52 while (Items.Count > count) Items.RemoveAt(Items.Count - 1); 53 while (Items.Count < count) Items.Add(""); 54 for (int i = 0; i < count; i++) 55 { 56 Items[i] = (string)regKey.GetValue(i.ToString(), ""); 57 } 49 58 } 50 EndUpdate(); 59 finally 60 { 61 EndUpdate(); 62 } 51 63 } 52 64 53 65 public void SaveToRegistry() 54 66 { 55 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey( regSubKey, true);56 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);67 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey, true) ?? 68 Application.UserAppDataRegistry.CreateSubKey(RegSubKey); 57 69 58 70 int i = 0; -
Common/CsvExport.cs
r14 r15 6 6 namespace Common 7 7 { 8 class CsvExport8 public class CsvExport 9 9 { 10 public void ListViewToCsv(ListView listView, string filePath, bool includeHidden)10 public static void ListViewToCsv(ListView listView, string filePath, bool includeHidden) 11 11 { 12 12 // Make header string … … 15 15 16 16 // Export data rows 17 foreach (ListViewItem listItem in listView.Items) 18 WriteCsvRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listItem.SubItems[i].Text); 17 for (int j = 0; j < listView.Items.Count; j++) 18 { 19 WriteCsvRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, 20 i => listView.Items[j].SubItems[i].Text); 21 } 19 22 20 23 File.WriteAllText(filePath, result.ToString()); 21 24 } 22 25 23 public void DataGridViewToCsv(DataGridView dataGridView, string filePath, bool includeHidden)26 public static void DataGridViewToCsv(DataGridView dataGridView, string filePath, bool includeHidden) 24 27 { 25 28 // Make header string … … 34 37 } 35 38 36 private void WriteCsvRow(StringBuilder result, int itemsCount, Func<int, bool> isColumnNeeded, Func<int, string> columnValue)39 private static void WriteCsvRow(StringBuilder result, int itemsCount, Func<int, bool> isColumnNeeded, Func<int, string> columnValue) 37 40 { 38 41 bool isFirstTime = true; … … 46 49 isFirstTime = false; 47 50 48 result.Append( String.Format("\"{0}\"", columnValue(i).Replace("\"", "\"\"")));51 result.Append($"\"{columnValue(i).Replace("\"", "\"\"")}\""); 49 52 } 50 53 result.AppendLine(); 51 54 } 52 53 55 } 54 56 } -
Common/Dialogs.cs
r14 r15 1 using System.IO; 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 2 5 using System.Windows.Forms; 3 6 4 7 namespace Common 5 8 { 6 class Dialogs9 public class Dialogs 7 10 { 8 11 public const string AnyFile = "Any file"; 12 public const string AnyFileExt = "*"; 9 13 10 public static string OpenFileDialog(string fileName, string fileType, string fileExtension)14 public static bool OpenFileDialog(string fileName, string[] fileTypes, string defaultExt, out string newFileName) 11 15 { 12 OpenFileDialog dlg = new OpenFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*.*", DefaultExt = fileExtension }; 16 string filter = string.Join(@"|", fileTypes) + @"|" + AnyFile + @"|*." + AnyFileExt; 17 OpenFileDialog openFileDialog = new OpenFileDialog { Filter = filter, DefaultExt = defaultExt }; 13 18 if (!string.IsNullOrEmpty(fileName)) 14 19 { 15 dlg.FileName = Path.GetFileName(fileName);16 dlg.InitialDirectory = Path.GetDirectoryName(fileName);20 openFileDialog.FileName = Path.GetFileName(fileName); 21 openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName); 17 22 } 18 23 19 if ( dlg.ShowDialog() == DialogResult.OK)24 if (openFileDialog.ShowDialog() == DialogResult.OK) 20 25 { 21 fileName = dlg.FileName; 26 newFileName = openFileDialog.FileName; 27 return true; 22 28 } 23 return fileName; 29 else 30 { 31 newFileName = null; 32 return false; 33 } 24 34 } 25 35 26 public static string SaveFileDialog(string fileName, string fileType, string fileExtension)36 public static bool OpenFileDialogMulti(string fileName, string fileType, string fileExtension, out string[] newFileNames) 27 37 { 28 SaveFileDialog dlg = new SaveFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*.*", DefaultExt = fileExtension }; 38 if (fileExtension.StartsWith(".", StringComparison.Ordinal)) fileExtension = fileExtension.Substring(1); 39 OpenFileDialog openFileDialog = new OpenFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*." + AnyFileExt, DefaultExt = fileExtension }; 29 40 if (!string.IsNullOrEmpty(fileName)) 30 41 { 31 dlg.FileName = Path.GetFileName(fileName);32 dlg.InitialDirectory = Path.GetDirectoryName(fileName);42 openFileDialog.FileName = Path.GetFileName(fileName); 43 openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName); 33 44 } 34 45 35 if (dlg.ShowDialog() == DialogResult.OK) 46 openFileDialog.Multiselect = true; 47 48 if (openFileDialog.ShowDialog() == DialogResult.OK) 36 49 { 37 fileName = dlg.FileName; 50 newFileNames = openFileDialog.FileNames; 51 return true; 38 52 } 39 return fileName; 53 else 54 { 55 newFileNames = null; 56 return false; 57 } 58 } 59 60 public static bool SaveFileDialog(string fileName, string fileType, string fileExtension, out string newFileName) 61 { 62 if (fileExtension.StartsWith(".", StringComparison.Ordinal)) fileExtension = fileExtension.Substring(1); 63 SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = fileType + @"|*." + fileExtension + @"|" + AnyFile + @"|*." + AnyFileExt, DefaultExt = fileExtension }; 64 if (!string.IsNullOrEmpty(fileName)) 65 { 66 saveFileDialog.FileName = Path.GetFileName(fileName); 67 saveFileDialog.InitialDirectory = Path.GetDirectoryName(fileName); 68 } 69 70 if (saveFileDialog.ShowDialog() == DialogResult.OK) 71 { 72 newFileName = saveFileDialog.FileName; 73 return true; 74 } 75 else 76 { 77 newFileName = null; 78 return false; 79 } 40 80 } 41 81 } -
Common/DpiScaling.cs
r14 r15 6 6 namespace Common 7 7 { 8 class DpiScaling8 public class DpiScaling 9 9 { 10 10 public static float SystemDpi; 11 11 public static bool UseCustomDpi; 12 12 public static float CustomDpi = 96; 13 private static bool _changed; 13 private static bool _changed; 14 14 15 15 public static void ApplyToComponent(Component component) 16 16 { 17 if (component is Control )17 if (component is Control control) 18 18 { 19 foreach (Control child in (component as Control).Controls)19 foreach (Control child in control.Controls) 20 20 { 21 21 ApplyToComponent(child); 22 22 } 23 23 } 24 if (component is ToolStrip )24 if (component is ToolStrip toolStrip) 25 25 { 26 26 //(component as ToolStrip).AutoSize = false; 27 Size newSize = new Size((int)( (component as ToolStrip).ImageScalingSize.Width * CustomDpi / 96F),28 (int)( (component as ToolStrip).ImageScalingSize.Height * CustomDpi / 96F));27 Size newSize = new Size((int)(toolStrip.ImageScalingSize.Width * CustomDpi / 96F), 28 (int)(toolStrip.ImageScalingSize.Height * CustomDpi / 96F)); 29 29 /* foreach (ToolStripItem item in (component as ToolStrip).Items) 30 30 { … … 42 42 } 43 43 } 44 */ (component as ToolStrip).ImageScalingSize = newSize;44 */ toolStrip.ImageScalingSize = newSize; 45 45 46 46 /*(component as ToolStrip).Font = new System.Drawing.Font((component as ToolStrip).Font.FontFamily, 8.25F * customDpi / 96F, … … 52 52 */ 53 53 } 54 if (component is MenuStrip)54 switch (component) 55 55 { 56 (component as MenuStrip).Font = new Font((component as MenuStrip).Font.FontFamily, 8.25F * CustomDpi / 96F, 57 (component as MenuStrip).Font.Style, GraphicsUnit.Point, (component as MenuStrip).Font.GdiCharSet); 58 } 59 if (component is TabControl) 60 { 61 foreach (TabPage tabPage in (component as TabControl).TabPages) 56 case MenuStrip menuStrip: 57 menuStrip.Font = new Font(menuStrip.Font.FontFamily, 8.25F * CustomDpi / 96F, 58 menuStrip.Font.Style, GraphicsUnit.Point, menuStrip.Font.GdiCharSet); 59 break; 60 case TabControl tabControl: 62 61 { 63 ApplyToComponent(tabPage); 62 foreach (TabPage tabPage in tabControl.TabPages) 63 { 64 ApplyToComponent(tabPage); 65 } 66 67 break; 64 68 } 65 } 66 if (component is ToolStripItem) 67 { 68 (component as ToolStripItem).Font = new Font((component as ToolStripItem).Font.FontFamily, 8.25F * CustomDpi / 96F, 69 (component as ToolStripItem).Font.Style, GraphicsUnit.Point, (component as ToolStripItem).Font.GdiCharSet); 69 case ToolStripItem toolStripItem: 70 toolStripItem.Font = new Font(toolStripItem.Font.FontFamily, 8.25F * CustomDpi / 96F, 71 toolStripItem.Font.Style, GraphicsUnit.Point, toolStripItem.Font.GdiCharSet); 72 break; 70 73 } 71 74 } … … 84 87 form.Font.Style, GraphicsUnit.Point, form.Font.GdiCharSet); 85 88 ApplyToComponent(form); 86 } 89 } 87 90 } 88 91 … … 114 117 { 115 118 if (Application.UserAppDataRegistry == null) return; 116 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true) ;117 if (regKey == null) regKey =Application.UserAppDataRegistry.CreateSubKey(regSubKey);119 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true) ?? 120 Application.UserAppDataRegistry.CreateSubKey(regSubKey); 118 121 119 122 regKey.SetValueInt("CustomDpiEnabled", (int)CustomDpi); -
Common/ExtTools.cs
r13 r15 108 108 public static void SetFileAssociation(string extension, string progId, string openWith, string fileDescription) 109 109 { 110 RegistryKey BaseKey; 111 RegistryKey OpenMethod; 112 RegistryKey Shell; 113 RegistryKey CurrentUser; 110 RegistryKey baseKey = Registry.ClassesRoot.CreateSubKey(extension); 111 baseKey?.SetValue("", progId); 114 112 115 BaseKey = Registry.ClassesRoot.CreateSubKey(extension); 116 BaseKey?.SetValue("", progId); 113 RegistryKey openMethod = Registry.ClassesRoot.CreateSubKey(progId); 114 openMethod?.SetValue("", fileDescription); 115 openMethod?.CreateSubKey("DefaultIcon")?.SetValue("", "\"" + openWith + "\",0"); 116 RegistryKey shell = openMethod?.CreateSubKey("Shell"); 117 shell?.CreateSubKey("edit")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\""); 118 shell?.CreateSubKey("open")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\""); 119 shell?.Close(); 120 openMethod?.Close(); 121 baseKey?.Close(); 117 122 118 OpenMethod = Registry.ClassesRoot.CreateSubKey(progId); 119 OpenMethod?.SetValue("", fileDescription); 120 OpenMethod?.CreateSubKey("DefaultIcon")?.SetValue("", "\"" + openWith + "\",0"); 121 Shell = OpenMethod?.CreateSubKey("Shell"); 122 Shell?.CreateSubKey("edit")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\""); 123 Shell?.CreateSubKey("open")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\""); 124 Shell?.Close(); 125 OpenMethod?.Close(); 126 BaseKey?.Close(); 123 // Delete explorer override 124 RegistryKey currentUser = Registry.CurrentUser.OpenSubKey( 125 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension, true); 126 currentUser?.DeleteSubKey("UserChoice", false); 127 currentUser?.Close(); 127 128 128 // Delete explorer override 129 CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension, true); 130 CurrentUser?.DeleteSubKey("UserChoice", false); 131 CurrentUser?.Close(); 132 133 // Tell explorer the file association has been changed 134 SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); 129 // Tell explorer the file association has been changed 130 SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); 135 131 } 136 132 -
Common/FormDimensions.cs
r14 r15 7 7 namespace Common 8 8 { 9 class FormDimensions9 public class FormDimensions 10 10 { 11 11 public Form Form; … … 45 45 const UInt32 SW_RESTORE = 9; 46 46 47 private WINDOWPLACEMENT GetPlacement(Form form)47 private static WINDOWPLACEMENT GetPlacement(Form form) 48 48 { 49 49 WINDOWPLACEMENT placement = new WINDOWPLACEMENT(); … … 53 53 } 54 54 55 private bool IsVisibleOnAnyScreen(Rectangle rect)56 { 57 int minVisible = 50;55 private static bool IsVisibleOnAnyScreen(Rectangle rect) 56 { 57 const int minVisible = 50; 58 58 foreach (Screen screen in Screen.AllScreens) 59 59 { … … 70 70 public void Load(Form form, Form parentForm = null) 71 71 { 72 this.Form = form;73 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + form.Name) ;74 if (regKey == null) regKey =Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + form.Name);72 Form = form; 73 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + form.Name) ?? 74 Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + form.Name); 75 75 76 76 string name = ""; … … 138 138 form.WindowState = FormWindowState.Maximized; 139 139 break; 140 default:141 break;142 140 } 143 141 form.Visible = prevVisibleState; … … 147 145 public void Save(Form form, Form parentForm = null) 148 146 { 149 this.Form = form;150 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + form.Name, true) ;151 if (regKey == null) regKey =Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + form.Name);147 Form = form; 148 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + form.Name, true) ?? 149 Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + form.Name); 152 150 153 151 string name = ""; … … 186 184 public void LoadControl(Control control) 187 185 { 188 if (control is ListView) 189 { 190 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true); 191 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name); 192 193 for (int I = 0; I < (control as ListView).Columns.Count; I++) 194 { 195 if (regKey.GetValue("ColWidth" + I.ToString()) != null) 196 (control as ListView).Columns[I].Width = (int) regKey.GetValue("ColWidth" + I.ToString()); 197 } 198 } else 199 if (control is DataGridView) 200 { 201 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true); 202 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name); 203 204 for (int I = 0; I < (control as DataGridView).Columns.Count; I++) 205 { 206 if (regKey.GetValue("ColWidth" + I.ToString()) != null) 207 (control as DataGridView).Columns[I].Width = (int)regKey.GetValue("ColWidth" + I.ToString()); 208 } 209 } 210 if (control is SplitContainer) 211 { 212 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true); 213 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name); 214 215 if (regKey.GetValue("SplitterDistance") != null) 216 (control as SplitContainer).SplitterDistance = (int)regKey.GetValue("SplitterDistance"); 186 switch (control) 187 { 188 case ListView listView: 189 { 190 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + listView.Name, true) ?? 191 Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + listView.Name); 192 193 for (int I = 0; I < listView.Columns.Count; I++) 194 { 195 object value = regKey.GetValue(listView.Columns[I].Text); 196 if (value != null) listView.Columns[I].Width = (int)value; 197 } 198 199 break; 200 } 201 case DataGridView dataGridView: 202 { 203 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + dataGridView.Name, true) ?? 204 Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + dataGridView.Name); 205 206 for (int I = 0; I < dataGridView.Columns.Count; I++) 207 { 208 object value = regKey.GetValue("ColWidth" + I); 209 if (value != null) dataGridView.Columns[I].Width = (int)value; 210 } 211 212 break; 213 } 214 case SplitContainer splitContainer: 215 { 216 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + splitContainer.Name, true) ?? 217 Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + splitContainer.Name); 218 219 object value = regKey.GetValue("SplitterDistance"); 220 if (value != null) 221 splitContainer.SplitterDistance = (int)value; 222 break; 223 } 217 224 } 218 225 … … 225 232 public void SaveControl(Control control) 226 233 { 227 if (control is ListView) 228 { 229 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true); 230 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name); 231 232 for (int I = 0; I < (control as ListView).Columns.Count; I++) 233 { 234 regKey.SetValue("ColWidth" + I.ToString(), (control as ListView).Columns[I].Width); 235 } 236 } else 237 if (control is DataGridView) 238 { 239 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true); 240 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name); 241 242 for (int I = 0; I < (control as DataGridView).Columns.Count; I++) 243 { 244 regKey.SetValue("ColWidth" + I.ToString(), (control as DataGridView).Columns[I].Width); 245 } 246 } 247 if (control is SplitContainer) 248 { 249 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name, true); 250 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + control.Name); 251 252 regKey.SetValue("SplitterDistance", (control as SplitContainer).SplitterDistance); 234 switch (control) 235 { 236 case ListView listView: 237 { 238 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + listView.Name, true) ?? 239 Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + listView.Name); 240 241 for (int I = 0; I < listView.Columns.Count; I++) 242 { 243 regKey.SetValue(listView.Columns[I].Text, listView.Columns[I].Width); 244 } 245 246 break; 247 } 248 case DataGridView dataGridView: 249 { 250 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + dataGridView.Name, true) ?? 251 Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + dataGridView.Name); 252 253 for (int I = 0; I < dataGridView.Columns.Count; I++) 254 { 255 regKey.SetValue("ColWidth" + I, dataGridView.Columns[I].Width); 256 } 257 258 break; 259 } 260 case SplitContainer splitContainer: 261 { 262 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(RegSubKey + "\\" + Form.Name + "\\" + splitContainer.Name, true) ?? 263 Application.UserAppDataRegistry.CreateSubKey(RegSubKey + "\\" + Form.Name + "\\" + splitContainer.Name); 264 265 regKey.SetValue("SplitterDistance", splitContainer.SplitterDistance); 266 break; 267 } 253 268 } 254 269 -
Common/FormFind.Designer.cs
r14 r15 29 29 private void InitializeComponent() 30 30 { 31 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormFind)); 31 32 this.buttonFindNext = new System.Windows.Forms.Button(); 32 33 this.buttonCancel = new System.Windows.Forms.Button(); … … 72 73 this.label1.TabIndex = 0; 73 74 this.label1.Text = "Find what:"; 74 this.label1.Click += new System.EventHandler(this.label1_Click);75 75 // 76 76 // checkBoxMatchWholeWordOnly … … 80 80 this.checkBoxMatchWholeWordOnly.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 81 81 this.checkBoxMatchWholeWordOnly.Name = "checkBoxMatchWholeWordOnly"; 82 this.checkBoxMatchWholeWordOnly.Size = new System.Drawing.Size(19 3, 24);82 this.checkBoxMatchWholeWordOnly.Size = new System.Drawing.Size(194, 24); 83 83 this.checkBoxMatchWholeWordOnly.TabIndex = 2; 84 84 this.checkBoxMatchWholeWordOnly.Text = "Match whole word only"; 85 85 this.checkBoxMatchWholeWordOnly.UseVisualStyleBackColor = true; 86 this.checkBoxMatchWholeWordOnly.CheckedChanged += new System.EventHandler(this.checkBoxMatchWholeWordOnly_CheckedChanged);87 86 // 88 87 // checkBoxMatchCase … … 92 91 this.checkBoxMatchCase.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 93 92 this.checkBoxMatchCase.Name = "checkBoxMatchCase"; 94 this.checkBoxMatchCase.Size = new System.Drawing.Size(11 6, 24);93 this.checkBoxMatchCase.Size = new System.Drawing.Size(117, 24); 95 94 this.checkBoxMatchCase.TabIndex = 3; 96 95 this.checkBoxMatchCase.Text = "Match case"; 97 96 this.checkBoxMatchCase.UseVisualStyleBackColor = true; 98 this.checkBoxMatchCase.CheckedChanged += new System.EventHandler(this.checkBoxMatchCase_CheckedChanged);99 97 // 100 98 // buttonFindPrevious … … 120 118 this.comboBoxWhat.Size = new System.Drawing.Size(346, 28); 121 119 this.comboBoxWhat.TabIndex = 7; 122 this.comboBoxWhat.SelectedIndexChanged += new System.EventHandler(this.comboBoxWhat_SelectedIndexChanged);123 120 this.comboBoxWhat.TextChanged += new System.EventHandler(this.textBoxWhat_TextChanged); 124 121 this.comboBoxWhat.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxWhat_KeyPress); … … 136 133 this.Controls.Add(this.buttonCancel); 137 134 this.Controls.Add(this.buttonFindNext); 135 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 138 136 this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 139 137 this.MaximizeBox = false; … … 141 139 this.MinimumSize = new System.Drawing.Size(440, 189); 142 140 this.Name = "FormFind"; 143 this.StartPosition = System.Windows.Forms.FormStartPosition. Manual;141 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; 144 142 this.Text = "Find"; 145 143 this.TopMost = true; -
Common/FormFind.cs
r14 r15 6 6 namespace Common 7 7 { 8 public partial class FormFind : Form 8 public partial class FormFind : FormEx 9 9 { 10 10 public RichTextBoxEx richTextBox; … … 88 88 private void FormFind_Load(object sender, EventArgs e) 89 89 { 90 Theme.UseTheme(this);91 DpiScaling.Apply(this);92 Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);93 new FormDimensions().Load(this, Owner);94 90 comboBoxWhat.Focus(); 95 comboBoxWhat. regSubKey = regSubKey + "\\History";91 comboBoxWhat.RegSubKey = regSubKey + "\\History"; 96 92 LoadFromRegistry(); 97 93 UpdateInterface(); … … 107 103 SaveToRegistry(); 108 104 richTextBox.FormFind = null; 109 new FormDimensions().Save(this, Owner);110 105 } 111 106 … … 115 110 { 116 111 buttonFindNext.PerformClick(); 117 } else118 if (e.KeyChar == 27)119 {120 Close();121 112 } 122 113 } … … 144 135 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey); 145 136 146 147 137 regKey.SetValue("MatchWholeWordOnly", checkBoxMatchWholeWordOnly.Checked); 148 138 regKey.SetValue("MatchCase", checkBoxMatchCase.Checked); … … 150 140 comboBoxWhat.SaveToRegistry(); 151 141 } 152 153 private void comboBoxWhat_SelectedIndexChanged(object sender, EventArgs e)154 {155 156 }157 158 private void label1_Click(object sender, EventArgs e)159 {160 161 }162 163 private void checkBoxMatchWholeWordOnly_CheckedChanged(object sender, EventArgs e)164 {165 166 }167 168 private void checkBoxMatchCase_CheckedChanged(object sender, EventArgs e)169 {170 171 }172 142 } 173 143 } -
Common/FormProgress.cs
r14 r15 2 2 using System.Collections.Generic; 3 3 using System.ComponentModel; 4 using System.Drawing;5 4 using System.Linq; 6 5 using System.Windows.Forms; … … 8 7 namespace Common 9 8 { 10 public partial class FormProgress : Form 9 public partial class FormProgress : FormEx 11 10 { 12 11 public List<Progress> Tasks; 13 12 public DateTime StartTime; 14 13 public Progress CurrentTask; 14 private bool _canClose; 15 15 16 16 public FormProgress() … … 20 20 Tasks = new List<Progress>(); 21 21 CurrentTask = null; 22 _canClose = false; 22 23 } 23 24 24 25 private void timer1_Tick(object sender, EventArgs e) 25 26 { 26 int steps = 10000;27 const int steps = 10000; 27 28 int total = Tasks.Count * steps; 28 29 int current; … … 48 49 } 49 50 else labelTime.Text = ""; 51 52 // Update task bar progress 53 if (progressBar1.Value < progressBar1.Maximum) 54 { 55 TaskBarProgress.SetValue(Handle, progressBar1.Value, progressBar1.Maximum); 56 if (progressBar1.Value == 0) 57 { 58 TaskBarProgress.SetState(Handle, TaskBarProgress.TaskBarStates.Indeterminate); 59 } 60 } 61 else 62 { 63 TaskBarProgress.SetState(Handle, TaskBarProgress.TaskBarStates.NoProgress); 64 } 50 65 } 51 66 52 67 private void FormProgress_Load(object sender, EventArgs e) 53 68 { 54 Theme.UseTheme(this);55 DpiScaling.Apply(this);56 Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);57 58 69 timer1_Tick(this, null); 59 70 timer1.Enabled = true; … … 63 74 private void FormProgress_FormClosing(object sender, FormClosingEventArgs e) 64 75 { 76 e.Cancel = !_canClose; 65 77 CurrentTask.Terminated = true; 66 78 timer1.Enabled = false; … … 86 98 { 87 99 CurrentTask = Tasks[nextIndex]; 88 BackgroundWorker b g= new BackgroundWorker();89 b g.DoWork += bg_DoWork;90 b g.RunWorkerCompleted += bg_DoWorkerCompleted;91 b g.RunWorkerAsync();100 BackgroundWorker backgroundWorker = new BackgroundWorker(); 101 backgroundWorker.DoWork += bg_DoWork; 102 backgroundWorker.RunWorkerCompleted += bg_DoWorkerCompleted; 103 backgroundWorker.RunWorkerAsync(); 92 104 } 93 else Close(); 94 } 95 else Close(); 105 else 106 { 107 _canClose = true; 108 Close(); 109 } 110 } 111 else 112 { 113 _canClose = true; 114 Close(); 115 } 96 116 } 97 117 … … 122 142 StartTime = DateTime.Now; 123 143 CurrentTask = Tasks.First(); 124 BackgroundWorker b g= new BackgroundWorker();125 b g.DoWork += bg_DoWork;126 b g.RunWorkerCompleted += bg_DoWorkerCompleted;127 b g.RunWorkerAsync();144 BackgroundWorker backgroundWorker = new BackgroundWorker(); 145 backgroundWorker.DoWork += bg_DoWork; 146 backgroundWorker.RunWorkerCompleted += bg_DoWorkerCompleted; 147 backgroundWorker.RunWorkerAsync(); 128 148 ShowDialog(); 129 149 } -
Common/ListViewComparer.cs
r14 r15 3 3 using System.Runtime.InteropServices; 4 4 using System.Collections; 5 using System.Collections.Generic; 6 using System.Linq; 7 using Microsoft.Win32; 5 8 6 9 namespace Common 7 10 { 8 public enum ColumnDataType { String, Integer, DateTime, Decimal, Custom }; 11 public enum ColumnDataType { String, Integer, DateTime, Decimal, Custom } 12 13 public delegate int ListViewItemCompareEventHandler(ListViewItem x, ListViewItem y); 14 15 public class ListViewColumn 16 { 17 public string Name; 18 public int Width; 19 public HorizontalAlignment TextAlign; 20 public bool Visible; 21 public int FieldIndex; 22 public ColumnDataType DataType; 23 public ListViewItemCompareEventHandler OnCompare; 24 } 9 25 10 26 // Supports sorting by column in ascending or descending order 11 public class ListViewItemComparer : IComparer 27 public class ListViewItemComparer : IComparer, IComparer<ListViewItem> 12 28 { 13 private int column; 14 private SortOrder order; 29 public int Column; 30 public SortOrder Order; 31 public ListView ListView; 32 public ListViewManager ListViewManager; 15 33 16 34 public void ColumnClick(int newColumn) 17 35 { 18 36 // Determine if clicked column is already the column that is being sorted. 19 if (newColumn == column)37 if (newColumn == Column) 20 38 { 21 39 // Reverse the current sort direction for this column. 22 if (order == SortOrder.Ascending) 23 { 24 order = SortOrder.Descending; 25 } 26 else 27 { 28 order = SortOrder.Ascending; 29 } 40 Order = Order == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending; 30 41 } 31 42 else 32 43 { 33 44 // Set the column number that is to be sorted; default to ascending. 34 column = newColumn;35 order = SortOrder.Ascending;45 Column = newColumn; 46 Order = SortOrder.Ascending; 36 47 } 37 48 } … … 39 50 public void SetColumn(int newColumn) 40 51 { 41 column = newColumn;52 Column = newColumn; 42 53 } 43 54 44 55 public void SetOrder(SortOrder newOrder) 45 56 { 46 order = newOrder;57 Order = newOrder; 47 58 } 48 59 49 60 public ListViewItemComparer() 50 61 { 51 column = -1; 52 order = SortOrder.None; 53 } 54 55 public delegate int CompareItemEventHandler(ListViewItem x, ListViewItem y, int column); 56 57 public event CompareItemEventHandler OnCompareItem; 58 59 public int Compare(object x, object y) 60 { 61 ListViewItem listViewItemX = (ListViewItem)x; 62 ListViewItem listViewItemY = (ListViewItem)y; 63 62 Column = -1; 63 Order = SortOrder.None; 64 } 65 66 public int Compare(ListViewItem x, ListViewItem y) 67 { 68 if (Column >= ListView.Columns.Count) 69 { 70 Column = -1; 71 } 64 72 int result = 0; 65 if (listViewItemX != null && listViewItemY != null) 66 { 67 if (column < listViewItemX.SubItems.Count && column < listViewItemY.SubItems.Count && column >= 0) 68 { 73 if (x != null && y != null) 74 { 75 if (Column < x.SubItems.Count && Column < y.SubItems.Count && Column >= 0) 76 { 77 ListViewColumn listViewColumn = null; 69 78 ColumnDataType dataType = ColumnDataType.String; 70 71 if (listViewItemX.ListView.Columns[column].Tag != null)72 {73 dataType = (ColumnDataType) (listViewItemX.ListView.Columns[column].Tag);79 if (ListView.Columns[Column].Tag != null) 80 { 81 listViewColumn = (ListViewColumn)(ListView.Columns[Column].Tag); 82 dataType = listViewColumn.DataType; 74 83 } 75 84 76 85 if (dataType == ColumnDataType.Integer) 77 86 { 78 if (int.TryParse( listViewItemX.SubItems[column].Text, out varxi) &&79 int.TryParse( listViewItemY.SubItems[column].Text, out varyi))87 if (int.TryParse(x.SubItems[Column].Text, out int xi) && 88 int.TryParse(y.SubItems[Column].Text, out int yi)) 80 89 { 81 90 if (xi < yi) result = -1; … … 86 95 else if (dataType == ColumnDataType.Decimal) 87 96 { 88 if (decimal.TryParse( listViewItemX.SubItems[column].Text, out varxi) &&89 decimal.TryParse( listViewItemY.SubItems[column].Text, out varyi))97 if (decimal.TryParse(x.SubItems[Column].Text, out decimal xi) && 98 decimal.TryParse(y.SubItems[Column].Text, out decimal yi)) 90 99 { 91 100 if (xi < yi) result = -1; … … 96 105 else if (dataType == ColumnDataType.DateTime) 97 106 { 98 if (DateTime.TryParse( listViewItemX.SubItems[column].Text, out varxi) &&99 DateTime.TryParse( listViewItemY.SubItems[column].Text, out varyi))107 if (DateTime.TryParse(x.SubItems[Column].Text, out DateTime xi) && 108 DateTime.TryParse(y.SubItems[Column].Text, out DateTime yi)) 100 109 { 101 110 result = DateTime.Compare(xi, yi); … … 104 113 else if (dataType == ColumnDataType.Custom) 105 114 { 106 if ( OnCompareItem!= null)107 { 108 result = OnCompareItem(listViewItemX, listViewItemY, column);115 if (listViewColumn?.OnCompare != null) 116 { 117 result = listViewColumn.OnCompare(x, y); 109 118 } 110 119 } 111 120 else 112 result = string.CompareOrdinal( listViewItemX.SubItems[column].Text,113 listViewItemY.SubItems[column].Text);121 result = string.CompareOrdinal(x.SubItems[Column].Text, 122 y.SubItems[Column].Text); 114 123 } 115 124 else 116 125 { 117 if (!listViewItemX.Selected && listViewItemY.Selected) result = 1; 118 else if (listViewItemX.Selected && !listViewItemY.Selected) result = -1; 119 } 120 } 121 122 switch (order) 126 if (ListView.VirtualMode) 127 { 128 bool xSelected = ListView.SelectedIndices.IndexOf(x.Index) != -1; 129 bool ySelected = ListView.SelectedIndices.IndexOf(y.Index) != -1; 130 if (!xSelected && ySelected) result = 1; 131 else if (xSelected && !ySelected) result = -1; 132 } 133 else 134 { 135 if (!x.Selected && y.Selected) result = 1; 136 else if (x.Selected && !y.Selected) result = -1; 137 } 138 } 139 } 140 141 switch (Order) 123 142 { 124 143 case SortOrder.Ascending: … … 126 145 case SortOrder.Descending: 127 146 return -result; 147 case SortOrder.None: 128 148 default: 129 149 return 0; … … 131 151 } 132 152 133 public static void ConfigureListView(ListView listView, int column = -1, SortOrder order = SortOrder.None) 134 { 135 if (listView.ListViewItemSorter == null) 136 { 137 listView.ListViewItemSorter = new ListViewItemComparer(); 138 } 139 140 ((ListViewItemComparer) listView.ListViewItemSorter).SetOrder(order); 141 ((ListViewItemComparer) listView.ListViewItemSorter).SetColumn(column); 153 public int Compare(object x, object y) 154 { 155 return Compare((ListViewItem)x, (ListViewItem)y); 156 } 157 } 158 159 public class ListViewManager 160 { 161 public List<ListViewColumn> ListColumns; 162 public List<ListViewColumn> VisibleListColumns; 163 public ListView ListView; 164 public List<ListViewItem> ListViewCache; 165 public Dictionary<object, ListViewItem> ListViewDict; 166 private readonly List<ListViewItem> listViewCacheMatched; 167 public delegate void LoadItemHandler(ListViewItem listViewItem, int itemIndex); 168 public event LoadItemHandler LoadItem; 169 public delegate void LoadItemObjectHandler(ListViewItem listViewItem, object item); 170 public event LoadItemObjectHandler LoadItemObject; 171 public delegate bool FilterItemHandler(object item, Dictionary<int, string> filter); 172 public event FilterItemHandler FilterItem; 173 public Dictionary<int, string> Filter; 174 175 public ListViewManager(ListView listView) 176 { 177 ListView = listView; 178 var listViewComparer = new ListViewItemComparer 179 { 180 ListView = listView, 181 ListViewManager = this 182 }; 183 listView.ListViewItemSorter = listViewComparer; 184 185 ListColumns = new List<ListViewColumn>(); 186 foreach (ColumnHeader columnHeader in ListView.Columns) 187 { 188 ListViewColumn listViewColumn = new ListViewColumn() 189 { 190 Name = columnHeader.Text, 191 TextAlign = columnHeader.TextAlign, 192 Width = columnHeader.Width, 193 Visible = true, 194 FieldIndex = Convert.ToInt32(columnHeader.Tag) 195 }; 196 columnHeader.Tag = listViewColumn; 197 ListColumns.Add(listViewColumn); 198 } 199 VisibleListColumns = new List<ListViewColumn>(); 200 if (listView.VirtualMode) 201 { 202 ListViewCache = new List<ListViewItem>(); 203 204 ListView.RetrieveVirtualItem += delegate (object sender, RetrieveVirtualItemEventArgs e) 205 { 206 e.Item = ListViewCache[e.ItemIndex]; 207 }; 208 } 209 ListViewDict = new Dictionary<object, ListViewItem>(); 210 listViewCacheMatched = new List<ListViewItem>(); 211 ListView.ColumnClick += delegate (object sender, ColumnClickEventArgs e) 212 { 213 ((ListViewItemComparer)((ListView)sender).ListViewItemSorter).ColumnClick(e.Column); 214 Sort(); 215 }; 216 Filter = new Dictionary<int, string>(); 217 218 ConfigureListView(); 219 } 220 221 public void Sort() 222 { 223 ListViewItemComparer listViewComparer = (ListViewItemComparer)ListView.ListViewItemSorter; 224 225 if (((ListViewItemComparer)ListView.ListViewItemSorter).Column != -1 && 226 ((ListViewItemComparer)ListView.ListViewItemSorter).Order != SortOrder.None) 227 { 228 229 if (ListView.VirtualMode) 230 { 231 // Store selected items 232 List<object> selectedItems = null; 233 if (ListView.SelectedIndices.Count > 0) 234 { 235 int[] selected = new int[ListView.SelectedIndices.Count]; 236 ListView.SelectedIndices.CopyTo(selected, 0); 237 selectedItems = new List<object>(selected.Length); 238 foreach (int index in ListView.SelectedIndices) 239 { 240 selectedItems.Add(ListViewCache[index].Tag); 241 } 242 } 243 244 ListViewCache.Sort(listViewComparer); 245 ListView.Refresh(); 246 247 // Restore selected items 248 if (selectedItems != null) 249 { 250 ListView.BeginUpdate(); 251 try 252 { 253 ListView.SelectedIndices.Clear(); 254 foreach (var selectedObject in selectedItems) 255 { 256 ListView.SelectedIndices.Add(ListViewCache.IndexOf(ListViewDict[selectedObject])); 257 } 258 } 259 finally 260 { 261 ListView.EndUpdate(); 262 } 263 } 264 } 265 else 266 { 267 ListView.Sort(); 268 } 269 } 270 271 ListView.SetSortIcon(listViewComparer.Column, listViewComparer.Order); 272 } 273 274 public void ReloadCache(int itemCount) 275 { 276 ListView.BeginUpdate(); 277 try 278 { 279 // Sync number of cached items 280 if (listViewCacheMatched.Count > itemCount) listViewCacheMatched.RemoveRange(itemCount, listViewCacheMatched.Count - itemCount); 281 if (listViewCacheMatched.Capacity < itemCount) listViewCacheMatched.Capacity = itemCount; 282 while (listViewCacheMatched.Count < itemCount) 283 { 284 ListViewItem listViewItem = new ListViewItem(); 285 listViewCacheMatched.Add(listViewItem); 286 } 287 288 // Update items content 289 if (LoadItem != null) 290 { 291 for (int i = 0; i < itemCount; i++) 292 { 293 LoadItem?.Invoke(listViewCacheMatched[i], i); 294 } 295 } 296 297 if (Filter.Count > 0 && FilterItem != null) 298 { 299 ListViewCache = listViewCacheMatched.FindAll(x => (bool)FilterItem?.Invoke((object)x.Tag, Filter)).ToList(); 300 } 301 else 302 { 303 ListViewCache = listViewCacheMatched.ToList(); 304 } 305 ListView.VirtualListSize = ListViewCache.Count; 306 307 ListViewDict = ListViewCache.ToDictionary(key => key.Tag, value => value); 308 Sort(); 309 } 310 finally 311 { 312 ListView.EndUpdate(); 313 } 314 } 315 316 public void ReloadCacheItem(ListViewItem listViewItem, int itemIndex) 317 { 318 LoadItem?.Invoke(listViewItem, itemIndex); 319 } 320 321 public void ReloadCacheItemObject(ListViewItem listViewItem, object item) 322 { 323 LoadItemObject?.Invoke(listViewItem, item); 324 } 325 326 public void UpdateFromListView() 327 { 328 // Store current width 329 for (int i = 0; i < VisibleListColumns.Count; i++) 330 { 331 VisibleListColumns[i].Width = ListView.Columns[i].Width; 332 } 333 } 334 335 public void UpdateToListView() 336 { 337 ListView.BeginUpdate(); 338 try 339 { 340 VisibleListColumns = ListColumns.FindAll(x => x.Visible); 341 342 while (ListView.Columns.Count < VisibleListColumns.Count) 343 ListView.Columns.Add(""); 344 while (ListView.Columns.Count > VisibleListColumns.Count) 345 ListView.Columns.RemoveAt(ListView.Columns.Count - 1); 346 for (int i = 0; i < VisibleListColumns.Count; i++) 347 { 348 ColumnHeader item = ListView.Columns[i]; 349 item.Text = VisibleListColumns[i].Name; 350 item.Width = VisibleListColumns[i].Width; 351 item.TextAlign = VisibleListColumns[i].TextAlign; 352 item.Tag = VisibleListColumns[i]; 353 Theme.ApplyTheme(item); 354 } 355 } 356 finally 357 { 358 ListView.EndUpdate(); 359 } 360 } 361 362 public void UpdateColumns() 363 { 364 UpdateFromListView(); 365 UpdateToListView(); 366 } 367 368 public void ConfigureListView(int column = -1, SortOrder order = SortOrder.None) 369 { 370 ListViewItemComparer listViewComparer = (ListViewItemComparer)ListView.ListViewItemSorter; 371 listViewComparer.SetOrder(order); 372 listViewComparer.SetColumn(column); 142 373 if ((column != -1) && (order != SortOrder.None)) 143 374 { 144 listView.Sort(); 145 listView.SetSortIcon(column, order); 146 } 147 listView.ColumnClick += delegate (object sender, ColumnClickEventArgs e) 148 { 149 ((ListViewItemComparer) ((ListView) sender).ListViewItemSorter).ColumnClick(e.Column); 150 ((ListView) sender).Sort(); 151 ((ListView) sender).SetSortIcon(((ListViewItemComparer) ((ListView) sender).ListViewItemSorter).column, ((ListViewItemComparer) ((ListView) sender).ListViewItemSorter).order); 152 }; 153 } 154 155 public static void SetColumnDataType(ListView listView, int index, ColumnDataType dataType) 156 { 157 listView.Columns[index].Tag = dataType; 375 Sort(); 376 } 377 } 378 379 public void SetColumnDataType(int fieldIndex, ColumnDataType dataType, ListViewItemCompareEventHandler compareHandler = null) 380 { 381 ListViewColumn column = ListColumns.FirstOrDefault(x => x.FieldIndex == fieldIndex); 382 if (column == null) return; 383 column.DataType = dataType; 384 column.OnCompare = compareHandler; 385 } 386 387 public void SetColumnVisible(int fieldIndex, bool visible) 388 { 389 ListViewColumn column = ListColumns.FirstOrDefault(x => x.FieldIndex == fieldIndex); 390 if (column == null) return; 391 column.Visible = visible; 392 } 393 394 public void LoadFromRegistry(string regSubKey) 395 { 396 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey) ?? 397 Application.UserAppDataRegistry.CreateSubKey(regSubKey); 398 399 ListViewItemComparer listViewComparer = (ListViewItemComparer)ListView.ListViewItemSorter; 400 listViewComparer.Order = (SortOrder)regKey.GetValueInt("SortOrder", (int)listViewComparer.Order); 401 listViewComparer.Column = regKey.GetValueInt("SortColumn", listViewComparer.Column); 402 foreach (ListViewColumn listViewColumn in ListColumns) 403 { 404 listViewColumn.Width = regKey.GetValueInt("Width" + listViewColumn.Name, listViewColumn.Width); 405 listViewColumn.Visible = regKey.GetValueBool("Visible" + listViewColumn.Name, listViewColumn.Visible); 406 } 407 UpdateToListView(); 408 } 409 410 public void SaveToRegistry(string regSubKey) 411 { 412 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true) ?? 413 Application.UserAppDataRegistry.CreateSubKey(regSubKey); 414 415 ListViewItemComparer listViewComparer = (ListViewItemComparer)ListView.ListViewItemSorter; 416 regKey.SetValueInt("SortOrder", (int)listViewComparer.Order); 417 regKey.SetValueInt("SortColumn", listViewComparer.Column); 418 foreach (ListViewColumn listViewColumn in ListColumns) 419 { 420 regKey.SetValueInt("Width" + listViewColumn.Name, listViewColumn.Width); 421 regKey.SetValueBool("Visible" + listViewColumn.Name, listViewColumn.Visible); 422 } 158 423 } 159 424 } -
Common/NamedList.cs
r14 r15 1 using System; 2 using System.Collections.Generic; 1 using System.Collections.Generic; 3 2 using System.Linq; 4 3 -
Common/Prompt.cs
r14 r15 139 139 public static bool ShowDialog(string text, string caption, object[] states, object initialValue, out object result) 140 140 { 141 int dataSize = 200;141 const int dataSize = 200; 142 142 Form prompt = new Form() 143 143 { … … 209 209 public static bool ShowDialog(string text, string caption, string initialValue, out string result, bool monospaceFont = false) 210 210 { 211 int dataSize = 200;211 const int dataSize = 200; 212 212 Form prompt = new Form() 213 213 { 214 214 Width = 500, 215 215 Height = 150 + dataSize, 216 FormBorderStyle = FormBorderStyle.FixedDialog, 217 Text = caption, 218 StartPosition = FormStartPosition.CenterScreen, 219 }; 220 Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text }; 221 RichTextBox textBox = new RichTextBox() { Left = 50, Top = 44, Width = 400, Height = dataSize, Text = initialValue }; 216 FormBorderStyle = FormBorderStyle.Sizable, 217 Text = caption, 218 StartPosition = FormStartPosition.CenterScreen 219 }; 220 Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text }; 221 RichTextBoxEx textBox = new RichTextBoxEx() 222 { 223 Left = 50, Top = 44, Width = 400, Height = dataSize, Text = initialValue, 224 Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left 225 }; 222 226 textBox.TextChanged += delegate 223 227 { … … 225 229 }; 226 230 textBox.SelectAll(); 227 Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = dataSize + 70, DialogResult = DialogResult.OK }; 231 Button confirmation = new Button() 232 { 233 Text = @"Ok", Left = 350, Width = 100, Top = dataSize + 70, DialogResult = DialogResult.OK, 234 Anchor = AnchorStyles.Bottom | AnchorStyles.Right 235 }; 228 236 confirmation.Click += (sender, e) => { prompt.Close(); }; 229 237 prompt.Controls.Add(textBox); -
Common/RecentFiles.cs
r14 r15 11 11 public int MaxCount = 10; 12 12 public event EventHandler Change; 13 public EventArgs e = null;14 13 15 14 public void AddItem(string fileName) … … 24 23 public void LimitMaxCount() 25 24 { 26 while(Items.Count > MaxCount)27 Items.Remove At(Items.Count - 1);25 if (Items.Count > MaxCount) 26 Items.RemoveRange(MaxCount, Items.Count - MaxCount); 28 27 } 29 28 … … 36 35 private void DoChange() 37 36 { 38 Change?.Invoke(this, new EventArgs());37 Change?.Invoke(this, EventArgs.Empty); 39 38 } 40 39 41 40 public void LoadFromRegistry(string regSubKey) 42 41 { 43 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey) ;44 if (regKey == null) regKey =Application.UserAppDataRegistry.CreateSubKey(regSubKey);42 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey) ?? 43 Application.UserAppDataRegistry.CreateSubKey(regSubKey); 45 44 46 45 int count = (int)regKey.GetValue("Count", 0); … … 55 54 public void SaveToRegistry(string regSubKey) 56 55 { 57 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true) ;58 if (regKey == null) regKey =Application.UserAppDataRegistry.CreateSubKey(regSubKey);56 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true) ?? 57 Application.UserAppDataRegistry.CreateSubKey(regSubKey); 59 58 60 59 regKey.SetValue("Count", Items.Count); … … 75 74 } 76 75 77 while (menuItem.DropDownItems.Count > Items.Count) menuItem.DropDownItems.RemoveAt(menuItem.DropDownItems.Count - 1); 76 while (menuItem.DropDownItems.Count > Items.Count) 77 menuItem.DropDownItems.RemoveAt(menuItem.DropDownItems.Count - 1); 78 78 for (int i = 0; i < Items.Count; i++) 79 79 { -
Common/Registry.cs
r14 r15 67 67 } 68 68 69 public static double GetValueDouble(this RegistryKey regKey, string name, double defaultValue) 70 { 71 object value = regKey.GetValue(name, defaultValue.ToString()); 72 if (double.TryParse((string)value, out double num)) 73 { 74 return num; 75 } 76 else return defaultValue; 77 } 78 79 public static double? GetValueDoubleNullable(this RegistryKey regKey, string name, double? defaultValue) 80 { 81 object value = regKey.GetValue(name, defaultValue.ToString()); 82 if (double.TryParse((string)value, out double num)) 83 { 84 return num; 85 } 86 else if (value is string && (string)value == "") return null; 87 else return defaultValue; 88 } 89 69 90 public static void SetValueBool(this RegistryKey regKey, string name, bool value) 70 91 { … … 110 131 regKey.SetValue(name, value); 111 132 } 133 134 public static void SetValueDouble(this RegistryKey regKey, string name, double value) 135 { 136 regKey.SetValue(name, value); 137 } 138 139 public static void SetValueDoubleNullable(this RegistryKey regKey, string name, double? value) 140 { 141 if (value != null) 142 { 143 regKey.SetValue(name, value); 144 } 145 else regKey.SetValue(name, ""); 146 } 112 147 } 113 148 } -
Common/RichTextBoxEx.cs
r14 r15 319 319 LinkMatches = LinkMatches, 320 320 ReadOnly = ReadOnly, 321 LinkClick = delegate(string linkText) { LinkClick?.Invoke(linkText); }321 LinkClick = linkText => LinkClick?.Invoke(linkText) 322 322 }; 323 323 textForm.Controls.Add(richTextBox); -
Common/SecureApi.cs
r1 r15 1 1 using System; 2 using System.Collections.Generic;3 using System.Linq;4 2 using System.Text; 5 3 using System.Security; 6 4 using System.Security.Cryptography; 7 8 5 9 6 namespace Common … … 11 8 static class SecureApi 12 9 { 13 static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("Salt Is Not A Password");10 private static readonly byte[] Entropy = Encoding.Unicode.GetBytes("Salt Is Not A Password"); 14 11 15 public static string EncryptString(S ystem.Security.SecureString input)12 public static string EncryptString(SecureString input) 16 13 { 17 byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect( 18 System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)), 19 entropy, 20 System.Security.Cryptography.DataProtectionScope.CurrentUser); 14 byte[] encryptedData = ProtectedData.Protect(Encoding.Unicode.GetBytes(ToInsecureString(input)), 15 Entropy, DataProtectionScope.CurrentUser); 21 16 return Convert.ToBase64String(encryptedData); 22 17 } … … 26 21 try 27 22 { 28 byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect( 29 Convert.FromBase64String(encryptedData), 30 entropy, 31 System.Security.Cryptography.DataProtectionScope.CurrentUser); 32 return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData)); 23 byte[] decryptedData = ProtectedData.Unprotect(Convert.FromBase64String(encryptedData), Entropy, 24 DataProtectionScope.CurrentUser); 25 return ToSecureString(Encoding.Unicode.GetString(decryptedData)); 33 26 } 34 27 catch … … 51 44 public static string ToInsecureString(SecureString input) 52 45 { 53 string returnValue = string.Empty;46 string returnValue; 54 47 IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input); 55 48 try -
Common/SplitButton.cs
r14 r15 13 13 public bool ShowMenuUnderCursor { get; set; } 14 14 15 private bool menuShown = false;15 private bool menuShown; 16 16 17 17 protected override void OnMouseDown(MouseEventArgs mouseEvent) … … 56 56 int arrowY = ClientRectangle.Height / 2 - 1; 57 57 58 Brush brush = Enabled ? SystemBrushes.ControlText: SystemBrushes.ControlDark;59 Point[] arrows = new Point[]{58 Brush brush = Enabled ? new SolidBrush(Theme.ColorControlText) : SystemBrushes.ControlDark; 59 Point[] arrows = { 60 60 new Point(arrowX, arrowY), 61 61 new Point(arrowX + (int)DpiScaling.Scale(8), arrowY), -
Common/Table.cs
r14 r15 9 9 public enum OutputFormat 10 10 { 11 [FieldDisplayName("Excel (Tab separated)")] 11 12 Excel, 13 [FieldDisplayName("Plain text")] 12 14 Plain, 15 [FieldDisplayName("CSV")] 13 16 Csv, 17 [FieldDisplayName("HTML")] 14 18 Html, 15 ListView 19 [FieldDisplayName("ListView")] 20 ListView, 21 [FieldDisplayName("MediaWiki")] 22 MediaWiki, 23 [FieldDisplayName("JSON")] 24 Json, 25 [FieldDisplayName("XML")] 26 Xml 16 27 }; 17 28 18 public class Row29 public class TableRow 19 30 { 20 31 public List<string> Cells = new List<string>(); … … 26 37 } 27 38 39 public class TableColumn 40 { 41 public string Name; 42 public ColumnDataType DataType; 43 } 44 28 45 public class Table 29 46 { 30 public List<Row> Rows = new List<Row>(); 47 public string Title; 48 public List<TableRow> Rows = new List<TableRow>(); 49 public List<TableColumn> Columns = new List<TableColumn>(); 31 50 32 51 public void Clear() 33 52 { 53 Columns.Clear(); 34 54 Rows.Clear(); 35 55 } 36 56 37 public Row AddRow() 38 { 39 Row row = new Row(); 57 public void AddColumn(string name, ColumnDataType dataType = ColumnDataType.String) 58 { 59 Columns.Add(new TableColumn() { DataType = dataType, Name = name }); 60 } 61 62 public TableRow AddRow() 63 { 64 TableRow row = new TableRow(); 40 65 Rows.Add(row); 41 66 return row; … … 45 70 { 46 71 StringBuilder output = new StringBuilder(); 72 output.AppendLine(string.Join("\t", Columns.Select(x => x.Name))); 47 73 foreach (var row in Rows) 48 74 { … … 55 81 { 56 82 StringBuilder output = new StringBuilder(); 83 output.AppendLine(string.Join(Environment.NewLine, Columns.Select(x => x.Name))); 84 output.AppendLine(); 85 output.AppendLine("==========================="); 86 output.AppendLine(); 57 87 foreach (var row in Rows) 58 88 { … … 68 98 { 69 99 StringBuilder output = new StringBuilder(); 70 foreach (var row in Rows) 71 { 72 output.AppendLine(string.Join(",", row.Cells.Select(x => "\"" + x + "\""))); 100 output.AppendLine(string.Join(",", Columns.Select(x => "\"" + x.Name + "\""))); 101 foreach (var row in Rows) 102 { 103 output.AppendLine(string.Join(",", row.Cells.Select(x => "\"" + x.Replace("\"", "\"\"") + "\""))); 73 104 } 74 105 return output.ToString(); … … 78 109 { 79 110 StringBuilder output = new StringBuilder(); 80 output.AppendLine("<table border=\"1\">"); 81 foreach (var row in Rows) 82 { 83 output.AppendLine("<tr><td>" + string.Join("</td><td>", row.Cells.Select(x => x.Replace(Environment.NewLine, "<br/>"))) + "</td></tr>"); 84 } 111 output.AppendLine("<html>"); 112 if (!string.IsNullOrEmpty(Title)) 113 { 114 output.AppendLine(" <head>"); 115 output.AppendLine(" <title>" + Title + "</title>"); 116 output.AppendLine(" </head>"); 117 } 118 output.AppendLine(" <body>"); 119 output.AppendLine(" <table border=\"1\">"); 120 output.AppendLine(" <tr><th>" + string.Join("</th><th>", Columns.Select(x => x.Name.Replace(Environment.NewLine, "<br/>"))) + "</th></tr>"); 121 foreach (var row in Rows) 122 { 123 output.AppendLine(" <tr><td>" + string.Join("</td><td>", row.Cells.Select(x => x.Replace(Environment.NewLine, "<br/>"))) + "</td></tr>"); 124 } 125 output.AppendLine(" </table>"); 126 output.AppendLine(" </body>"); 127 output.AppendLine("</html>"); 128 return output.ToString(); 129 } 130 131 public string GetOutputXml() 132 { 133 StringBuilder output = new StringBuilder(); 134 output.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 135 output.AppendLine("<table>"); 136 if (!string.IsNullOrEmpty(Title)) output.AppendLine(" <title>" + Title + "</title>"); 137 output.AppendLine(" <rows>"); 138 foreach (var row in Rows) 139 { 140 output.AppendLine(" <row>"); 141 int x = 0; 142 foreach (var cell in row.Cells) 143 { 144 string columnName = ""; 145 if (x < Columns.Count) columnName = Columns[x].Name.Replace("\"", "\\\""); 146 output.AppendLine(" <cell name=\"" + columnName + "\">" + cell + "</cell>"); 147 x++; 148 } 149 output.AppendLine(" </row>"); 150 } 151 output.AppendLine(" </rows>"); 85 152 output.AppendLine("</table>"); 86 153 return output.ToString(); 87 154 } 155 156 public string GetOutputMediaWiki() 157 { 158 StringBuilder output = new StringBuilder(); 159 output.AppendLine("{| class=\"wikitable sortable\""); 160 output.AppendLine("! " + string.Join(" !! ", Columns.Select(x => x.Name.Replace(Environment.NewLine, "<br/>")))); 161 if (!string.IsNullOrEmpty(Title)) output.AppendLine("|+ " + Title); 162 foreach (var row in Rows) 163 { 164 output.AppendLine("|-"); 165 output.AppendLine("| " + string.Join(" || ", row.Cells.Select(x => x.Replace(Environment.NewLine, "<br/>")))); 166 } 167 output.AppendLine("|}"); 168 return output.ToString(); 169 } 170 171 public string GetOutputJson() 172 { 173 StringBuilder output = new StringBuilder(); 174 output.AppendLine("["); 175 for (int y = 0; y < Rows.Count; y++) 176 { 177 output.AppendLine(" {"); 178 for (int x = 0; x < Rows[y].Cells.Count; x++) 179 { 180 string columnName = ""; 181 if (x < Columns.Count) columnName = Columns[x].Name.Replace("\"", "\\\""); 182 output.Append(" \"" + columnName + "\": \"" + Rows[y].Cells[x].Replace("\"", "\\\"") + "\""); 183 if (x < Rows[y].Cells.Count - 1) 184 { 185 output.Append(","); 186 } 187 output.AppendLine(); 188 } 189 output.Append(" }"); 190 if (y < Rows.Count - 1) 191 { 192 output.Append(","); 193 } 194 output.AppendLine(); 195 } 196 output.AppendLine("]"); 197 return output.ToString(); 198 } 88 199 89 200 public void GetOutputListView(ListView listView) 90 201 { 91 foreach (var row in Rows) 92 { 93 if (listView.Columns.Count == 0 && row.Cells.Count > 0) 94 { 95 while (listView.Columns.Count < row.Cells.Count) 96 listView.Columns.Add(row.Cells[listView.Columns.Count]); 97 } 98 else 202 listView.BeginUpdate(); 203 try 204 { 205 List<ListViewItem> listViewItems = new List<ListViewItem> 206 { 207 Capacity = Rows.Count 208 }; 209 210 while (listView.Columns.Count < Columns.Count) 211 listView.Columns.Add(Columns[listView.Columns.Count].Name); 212 213 for (int i = 0; i < Columns.Count; i++) 214 { 215 listView.Columns[i].Tag = i; 216 } 217 ListViewManager listViewManager = new ListViewManager(listView); 218 for (int i = 0; i < Columns.Count; i++) 219 { 220 listViewManager.SetColumnDataType(i, Columns[i].DataType); 221 } 222 223 foreach (var row in Rows) 99 224 { 100 225 while (listView.Columns.Count < row.Cells.Count) 101 226 listView.Columns.Add(""); 227 102 228 ListViewItem item = new ListViewItem(); 103 229 int i = 0; 104 foreach ( varcell in row.Cells)230 foreach (string cell in row.Cells) 105 231 { 106 232 if (i == 0) item.Text = cell; 107 233 else item.SubItems.Add(cell); 108 234 i++; 109 235 } 110 listView.Items.Add(item); 111 } 112 } 113 236 237 listViewItems.Add(item); 238 } 239 240 // Add prepared list view items into visible list view at once 241 listView.Items.Clear(); 242 listView.Items.AddRange(listViewItems.ToArray()); 243 244 // Update columns width 245 foreach (ColumnHeader column in listView.Columns) 246 { 247 column.Width = listView.Width / listView.Columns.Count; 248 } 249 } 250 finally 251 { 252 listView.EndUpdate(); 253 } 254 } 255 256 public string GetOutput(OutputFormat outputFormat) 257 { 258 switch (outputFormat) 259 { 260 case OutputFormat.Excel: 261 return GetOutputTabs(); 262 case OutputFormat.Plain: 263 return GetOutputPlain(); 264 case OutputFormat.Csv: 265 return GetOutputCsv(); 266 case OutputFormat.Html: 267 return GetOutputHtml(); 268 case OutputFormat.MediaWiki: 269 return GetOutputMediaWiki(); 270 case OutputFormat.Json: 271 return GetOutputJson(); 272 case OutputFormat.Xml: 273 return GetOutputXml(); 274 default: 275 return ""; 276 } 277 } 278 279 public static string GetFileExt(OutputFormat outputFormat) 280 { 281 string[] outputFormatFileExt = new string[] { ".txt", ".txt", ".csv", ".htm", ".txt", ".txt", ".json", ".xml"}; 282 283 return outputFormatFileExt[(int)outputFormat]; 284 } 285 286 public void LoadFromListView(ListView listView) 287 { 288 Clear(); 289 Columns.Capacity = listView.Columns.Count; 114 290 foreach (ColumnHeader column in listView.Columns) 115 291 { 116 column.Width = listView.Width / listView.Columns.Count; 117 } 118 } 119 120 public string GetOutput(OutputFormat outputFormat) 121 { 122 if (outputFormat == OutputFormat.Excel) return GetOutputTabs(); 123 else if (outputFormat == OutputFormat.Plain) return GetOutputPlain(); 124 else if (outputFormat == OutputFormat.Csv) return GetOutputCsv(); 125 else if (outputFormat == OutputFormat.Html) return GetOutputHtml(); 126 else return ""; 292 AddColumn(column.Text); 293 } 294 295 Rows.Capacity = listView.Items.Count; 296 for (int i = 0; i < listView.Items.Count; i++) 297 { 298 TableRow row = AddRow(); 299 row.Cells.Capacity = listView.Items[i].SubItems.Count; 300 foreach (ListViewItem.ListViewSubItem subItem in listView.Items[i].SubItems) 301 { 302 row.AddCell(subItem.Text); 303 } 304 } 127 305 } 128 306 }
Note:
See TracChangeset
for help on using the changeset viewer.