From 85821be1dc6751b17db200cbd3dd68912e7c9020 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 26 Mar 2015 13:44:23 -0700 Subject: [PATCH] Basic Working Version It works. It's ugly and doesn't unsubscribe from clipboard events correctly, but it works. --- App.config | 6 ++ App.xaml | 8 ++ App.xaml.cs | 17 ++++ ClipboardRegex.csproj | 104 +++++++++++++++++++++++++ MainWindow.xaml | 24 ++++++ MainWindow.xaml.cs | 128 +++++++++++++++++++++++++++++++ Properties/AssemblyInfo.cs | 55 +++++++++++++ Properties/Resources.Designer.cs | 71 +++++++++++++++++ Properties/Resources.resx | 117 ++++++++++++++++++++++++++++ Properties/Settings.Designer.cs | 30 ++++++++ Properties/Settings.settings | 7 ++ 11 files changed, 567 insertions(+) create mode 100644 App.config create mode 100644 App.xaml create mode 100644 App.xaml.cs create mode 100644 ClipboardRegex.csproj create mode 100644 MainWindow.xaml create mode 100644 MainWindow.xaml.cs create mode 100644 Properties/AssemblyInfo.cs create mode 100644 Properties/Resources.Designer.cs create mode 100644 Properties/Resources.resx create mode 100644 Properties/Settings.Designer.cs create mode 100644 Properties/Settings.settings diff --git a/App.config b/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/App.xaml b/App.xaml new file mode 100644 index 0000000..d27f39b --- /dev/null +++ b/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/App.xaml.cs b/App.xaml.cs new file mode 100644 index 0000000..55aaf0c --- /dev/null +++ b/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace ClipboardRegex +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/ClipboardRegex.csproj b/ClipboardRegex.csproj new file mode 100644 index 0000000..fe4be5c --- /dev/null +++ b/ClipboardRegex.csproj @@ -0,0 +1,104 @@ + + + + + Debug + AnyCPU + {129A0662-5FE4-493C-817B-51D64E1DD74C} + WinExe + Properties + ClipboardRegex + ClipboardRegex + v4.5 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + MainWindow.xaml + Code + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + + \ No newline at end of file diff --git a/MainWindow.xaml b/MainWindow.xaml new file mode 100644 index 0000000..f791f03 --- /dev/null +++ b/MainWindow.xaml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs new file mode 100644 index 0000000..e7040c8 --- /dev/null +++ b/MainWindow.xaml.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using System.Runtime.InteropServices; +using System.Windows.Interop; +using System.Text.RegularExpressions; + +namespace ClipboardRegex +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + DateTime lastProcessed; + static TimeSpan hundredMS = new TimeSpan(0, 0, 0, 0, 100); + + public MainWindow() + { + InitializeComponent(); + lastProcessed = DateTime.Now; + + } + + // Clipboard code taken from http://www.fluxbytes.com/csharp/how-to-monitor-for-clipboard-changes-using-addclipboardformatlistener/ + /// + /// Places the given window in the system-maintained clipboard format listener list. + /// + [DllImport("user32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool AddClipboardFormatListener(IntPtr hwnd); + + /// + /// Removes the given window from the system-maintained clipboard format listener list. + /// + [DllImport("user32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool RemoveClipboardFormatListener(IntPtr hwnd); + + /// + /// Sent when the contents of the clipboard have changed. + /// + private const int WM_CLIPBOARDUPDATE = 0x031D; + + // Event-processing code adapted from http://stackoverflow.com/a/1926796/ + protected override void OnSourceInitialized(EventArgs e) + { + base.OnSourceInitialized(e); + HwndSource source = PresentationSource.FromVisual(this) as HwndSource; + source.AddHook(WndProc); + + bool result = AddClipboardFormatListener(source.Handle); + if (!result) + { + var err = Marshal.GetLastWin32Error(); + Console.WriteLine(err); + } + Console.WriteLine(result); + + } + + private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + { + if (msg == WM_CLIPBOARDUPDATE) + { + if ((DateTime.Now - lastProcessed) > hundredMS) // Hack to avoid feedback loop + { + IDataObject iData = Clipboard.GetDataObject(); // Clipboard's data. + + /* Depending on the clipboard's current data format we can process the data differently. + * Feel free to add more checks if you want to process more formats. */ + if (iData.GetDataPresent(DataFormats.Text)) + { + string text = (string)iData.GetData(DataFormats.Text); + Clipboard.SetText(runUserRegex(text)); + } + lastProcessed = DateTime.Now; + } + + } + + return IntPtr.Zero; + } + + private string runUserRegex(string input) + { + if (!MultLineOpt.IsChecked.HasValue || !DotMatchNL.IsChecked.HasValue) + { + return input; // Should never happen + } + string pattern = FindWhat.Text; + string replacement = ReplaceWith.Text; + RegexOptions opts = (bool)MultLineOpt.IsChecked ? RegexOptions.Multiline : RegexOptions.None; + opts |= (bool)DotMatchNL.IsChecked ? RegexOptions.Singleline : RegexOptions.None; + + try + { + return System.Text.RegularExpressions.Regex.Replace(input, pattern, replacement, opts, new TimeSpan(0, 0, 1)); + } + catch (ArgumentException) + { + MessageBox.Show("Invalid Regular Expression"); + return input; + } + + } + + + + + } + + + + + +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..085e59e --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ClipboardRegex")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ClipboardRegex")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +//In order to begin building localizable applications, set +//CultureYouAreCodingWith in your .csproj file +//inside a . For example, if you are using US english +//in your source files, set the to en-US. Then uncomment +//the NeutralResourceLanguage attribute below. Update the "en-US" in +//the line below to match the UICulture setting in the project file. + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] + + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs new file mode 100644 index 0000000..1cb31dc --- /dev/null +++ b/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.34209 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ClipboardRegex.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ClipboardRegex.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/Properties/Resources.resx b/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs new file mode 100644 index 0000000..49bc83e --- /dev/null +++ b/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.34209 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ClipboardRegex.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/Properties/Settings.settings b/Properties/Settings.settings new file mode 100644 index 0000000..033d7a5 --- /dev/null +++ b/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file