-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
128 lines (103 loc) · 4.25 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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/
/// <summary>
/// Places the given window in the system-maintained clipboard format listener list.
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AddClipboardFormatListener(IntPtr hwnd);
/// <summary>
/// Removes the given window from the system-maintained clipboard format listener list.
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
/// <summary>
/// Sent when the contents of the clipboard have changed.
/// </summary>
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;
}
}
}
}