Changeset 13


Ignore:
Timestamp:
Feb 8, 2021, 4:23:17 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Fixes to existing classes.
  • Added: File type association method.
  • Added: RecentFiles and Table classes.
Location:
Common
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • Common/ComboBoxEx.cs

    r3 r13  
    1 using System;
     1using System;
    22using System.Collections.Generic;
    33using System.ComponentModel;
     
    3131                if (index >= 0) Items.RemoveAt(index);
    3232                Text = previousText;
    33                 Items.Insert(0, Text);
     33                Items.Insert(0, previousText);
    3434
    3535                while (Items.Count > maxHistoryCount)
  • Common/ExtTools.cs

    r8 r13  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
     1using System;
    62using System.IO;
    73using System.Diagnostics;
    84using System.Windows.Forms;
     5using Microsoft.Win32;
     6using System.Runtime.InteropServices;
    97
    108namespace Common
     
    1210    static class ExtTools
    1311    {
    14         public static string compareProgram;
    15         public static string compareAttributes;
     12        public static string CompareProgram;
     13        public static string CompareAttributes;
    1614
    1715        static ExtTools()
    1816        {
    1917            // Autodetect location of Beyond Compare executable
    20             compareProgram = "C:\\Program Files\\Beyond Compare 4\\BCompare.exe";
    21             if (!File.Exists(compareProgram)) compareProgram = "C:\\Program Files (x86)\\Beyond Compare 4\\BCompare.exe";
    22             if (!File.Exists(compareProgram)) compareProgram = "C:\\Program Files\\Beyond Compare 3\\BCompare.exe";
    23             if (!File.Exists(compareProgram)) compareProgram = "C:\\Program Files (x86)\\Beyond Compare 3\\BCompare.exe";
     18            CompareProgram = "C:\\Program Files\\Beyond Compare 4\\BCompare.exe";
     19            if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files (x86)\\Beyond Compare 4\\BCompare.exe";
     20            if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files\\Beyond Compare 3\\BCompare.exe";
     21            if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files (x86)\\Beyond Compare 3\\BCompare.exe";
    2422
    25             compareAttributes = "\"<<left_file>>\" \"<<right_file>>\"";
     23            CompareAttributes = "\"<<left_file>>\" \"<<right_file>>\"";
    2624        }
    2725
     
    2927        {
    3028            return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" +
    31                 System.Windows.Forms.Application.ProductName + "\\Temp";
     29                Application.ProductName + "\\Temp";
    3230        }
    3331
     
    5149            {
    5250                // Execute comparison process
    53                 string atts = compareAttributes;
    54                 atts = atts.Replace("<<left_file>>", filenameLeft);
    55                 atts = atts.Replace("<<right_file>>", filenameRight);
    56                 ExecuteProgram(compareProgram, atts);
     51                string attributes = CompareAttributes;
     52                attributes = attributes.Replace("<<left_file>>", filenameLeft);
     53                attributes = attributes.Replace("<<right_file>>", filenameRight);
     54                ExecuteProgram(CompareProgram, attributes);
    5755            } catch (Exception e)
    5856            {
    59                 MessageBox.Show(e.Message.ToString());
     57                MessageBox.Show(e.Message);
    6058            }
    6159        }
    6260
    63         public static void ExecuteProgram(string executableFileName, string arguments, string workingDirectory = "")
     61        public static void ExecuteProgram(string executableFileName, string arguments = "", string workingDirectory = "")
    6462        {
    6563            // Execute external process
     
    106104            }
    107105            return output;
    108         }       
     106        }
     107
     108        public static void SetFileAssociation(string extension, string progId, string openWith, string fileDescription)
     109        {
     110            RegistryKey BaseKey;
     111            RegistryKey OpenMethod;
     112            RegistryKey Shell;
     113            RegistryKey CurrentUser;
     114
     115            BaseKey = Registry.ClassesRoot.CreateSubKey(extension);
     116            BaseKey?.SetValue("", progId);
     117
     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();
     127
     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);
     135        }
     136
     137        [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
     138        public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
    109139    }
    110140}
  • Common/FormFind.cs

    r3 r13  
    1 using System;
     1using System;
    22using System.Collections.Generic;
    33using System.ComponentModel;
     
    9696            Theme.UseTheme(this);
    9797            DpiScaling.Apply(this);
    98             this.Icon = Icon.ExtractAssociatedIcon(Application.Execut‌​ablePath);
     98            this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
    9999            new FormDimensions().Load(this, Owner);
    100100            comboBoxWhat.Focus();
  • Common/Prompt.cs

    r12 r13  
    284284            SaveFileDialog saveFileDialog = new SaveFileDialog()
    285285            {
    286                 Filter = fileType + " (." + fileExtension + ")|*." +
     286                Filter = fileType + "|*." +
    287287                fileExtension + "|Any file|*.*",
    288288                DefaultExt = fileExtension
  • Common/Registry.cs

    r9 r13  
    1 using System;
     1using System;
    22using System.Collections.Generic;
    33using System.Linq;
     
    3535        }
    3636
     37        public static long GetValueLong(this RegistryKey regKey, string name, long defaultValue)
     38        {
     39            object value = regKey.GetValue(name, defaultValue);
     40            if (value is long) return (long)value;
     41            else return defaultValue;
     42        }
     43
    3744        public static float GetValueFloat(this RegistryKey regKey, string name, float defaultValue)
    3845        {
    3946            object value = regKey.GetValue(name, defaultValue.ToString());
    40             float num;
    41             if (float.TryParse((string)value, out num))
     47            if (float.TryParse((string) value, out float num))
     48            {
    4249                return num;
     50            }
    4351            else return defaultValue;
    4452        }
     
    6068        }
    6169
     70        public static void SetValueLong(this RegistryKey regKey, string name, long value)
     71        {
     72            regKey.SetValue(name, value);
     73        }
     74
    6275        public static void SetValueFloat(this RegistryKey regKey, string name, float value)
    6376        {
  • Common/RichTextBoxEx.cs

    r1 r13  
    1 using System;
     1using System;
    22using System.Collections.Generic;
    3 using System.ComponentModel;
    4 using System.Data;
    53using System.Drawing;
    6 using System.Linq;
    7 using System.Text;
    8 using System.Threading.Tasks;
     4using System.Runtime.InteropServices;
    95using System.Windows.Forms;
    106
     
    6662            List<string> linkMatchStartString = new List<string>();
    6763            foreach (var linkMatch in linkMatches)
    68                 linkMatchStartString.Add(linkMatch.startString.ToLower());
     64                linkMatchStartString.Add(linkMatch.startString.ToLowerInvariant());
    6965
    7066            string content = richTextBox.Text;
    71             string contentLowerCase = content.ToLower();
     67            string contentLowerCase = content.ToLowerInvariant();
    7268
    7369            RichTextBox tempRichTextBox = new RichTextBox();
     
    149145        }
    150146
    151         [System.Runtime.InteropServices.DllImport("user32.dll")]
     147        [DllImport("user32.dll")]
    152148        private static extern int SendMessage(IntPtr hwndLock, Int32 wMsg, Int32 wParam, Int32 lParam);
    153149
     
    192188            RichTextBoxContext context = new RichTextBoxContext();
    193189            context.SaveContext(this);
    194             //SelectionStart = 0;
    195             SelectAll();
    196             SelectedRtf = previousRtf;
     190            Rtf = previousRtf;
    197191            UndoUnknownActions();
    198192
     
    279273                    if (link != "")
    280274                    {
    281                         int number;
    282275                        foreach (var linkMatch in linkMatches)
    283276                        {
    284277                            if ((link.Length >= linkMatch.startString.Length) && (
    285278                                (linkMatch.caseSensitive && (link.Substring(0, linkMatch.startString.Length) == linkMatch.startString)) ||
    286                                 (!linkMatch.caseSensitive && (link.ToLower().Substring(0, linkMatch.startString.Length) == linkMatch.startString.ToLower()))))
     279                                (!linkMatch.caseSensitive && (link.ToLowerInvariant().Substring(0, linkMatch.startString.Length) == linkMatch.startString.ToLower()))))
    287280                            {
    288281                                string linkNumber = link.Substring(linkMatch.startString.Length).Trim(new char[] { ' ', '#' });
    289                                 if (int.TryParse(linkNumber, out number))
     282                                if (int.TryParse(linkNumber, out int number))
    290283                                {
    291284                                    linkMatch.ExecuteLinkAction(number);
     
    314307                StartPosition = FormStartPosition.CenterScreen,
    315308            };
    316             textForm.Icon = Icon.ExtractAssociatedIcon(Application.Execut‌​ablePath);
     309            textForm.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
    317310            RichTextBoxEx richTextBox = new RichTextBoxEx();
    318311            richTextBox.parentForm = textForm;
     
    446439                        formFind = new FormFind();
    447440                        formFind.richTextBox = this;
    448                         formFind.Owner = this.parentForm;
     441                        formFind.Owner = parentForm;
    449442                    }
    450443                    formFind.Show();
     
    463456                cms.Items.Add(tsmiShowInWindow);
    464457
    465                 cms.Opening += delegate (object sender, CancelEventArgs e)
     458                cms.Opening += delegate
    466459                {
    467460                    HideLinks();
     
    481474        private int oldLength;
    482475
    483         [System.Runtime.InteropServices.DllImport("user32.dll")]
    484         private extern static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    485         [System.Runtime.InteropServices.DllImport("user32.dll")]
     476        [DllImport("user32.dll")]
     477        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
     478        [DllImport("user32.dll")]
    486479        private static extern int SendMessage(IntPtr hwndLock, Int32 wMsg, Int32 wParam, ref Point pt);
    487         [System.Runtime.InteropServices.DllImport("User32.dll")]
    488         extern static int GetScrollPos(IntPtr hWnd, int nBar);
    489         [System.Runtime.InteropServices.DllImport("user32.dll")]
     480        [DllImport("User32.dll")]
     481        static extern int GetScrollPos(IntPtr hWnd, int nBar);
     482        [DllImport("user32.dll")]
    490483        static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
    491484
     
    542535        public void ExecuteLinkAction(int number)
    543536        {
    544             LinkAction(number);
     537            LinkAction?.Invoke(number);
    545538        }
    546539
     
    573566            number = content.Substring(numberStart, i);
    574567
    575             int intNumber = 0;
    576             if (int.TryParse(number, out intNumber))
     568            if (int.TryParse(number, out int intNumber))
    577569            {
    578570                linkLength += number.Length;
  • Common/SuspendDrawing.cs

    r5 r13  
    11using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
     2using System.Runtime.InteropServices;
    63using System.Windows.Forms;
    74
     
    107    class SuspendDrawing
    118    {
    12         [System.Runtime.InteropServices.DllImport("user32.dll")]
     9        [DllImport("user32.dll")]
    1310        private static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
    1411        private const int WM_SETREDRAW = 11;
  • Common/Theme.cs

    r12 r13  
    8787                    else if (color == Color.LightGray) color = Color.DarkGray;
    8888                    else if (color == Color.DarkGray) color = Color.LightGray;
     89                    else if (color == Color.Orchid) color = Color.DarkOrchid;
     90                    else if (color == Color.DarkOrchid) color = Color.Orchid;
    8991                }
    9092            }
Note: See TracChangeset for help on using the changeset viewer.