From 562b7ea3462107cc32c549feae3603ee22f8b6a1 Mon Sep 17 00:00:00 2001 From: Alex Robbins Date: Fri, 3 Apr 2020 10:09:41 -0400 Subject: [PATCH] Changed winAPI methods. fixed pasting bug --- .gitignore | 5 ++++- Qlip/MainWindow.xaml.cs | 35 +++++++++++++++++--------------- Qlip/Native/ClipboardListener.cs | 13 ++++++------ Qlip/Native/KeyCodes.cs | 3 +-- Qlip/SpongeWindow.cs | 2 +- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 7023fb3..840a1fc 100644 --- a/.gitignore +++ b/.gitignore @@ -327,4 +327,7 @@ ASALocalRun/ *.nvuser # MFractors (Xamarin productivity tool) working folder -.mfractor/ \ No newline at end of file +.mfractor/ + +# Other +qlip-notes.txt \ No newline at end of file diff --git a/Qlip/MainWindow.xaml.cs b/Qlip/MainWindow.xaml.cs index 730050f..b59e5ed 100644 --- a/Qlip/MainWindow.xaml.cs +++ b/Qlip/MainWindow.xaml.cs @@ -55,11 +55,6 @@ public partial class MainWindow : Window, INotifyPropertyChanged, IDisposable /// private bool _exited = false; - /// - /// Used for capturing system clip events - /// - private IntPtr _nextClipboardViewer; - /// /// Timer used for auto pasting when no action for some time /// @@ -131,6 +126,8 @@ public MainWindow(Model model) _sponge = new SpongeWindow(); _sponge.WndProcCalled += (s, e) => ProcessMessage(e); + + HandleCopy(); // Grab existing clip if one exists } /// @@ -148,15 +145,8 @@ private void ProcessMessage(System.Windows.Forms.Message message) if (!_pasting) { HandlePaste(); } } break; - case KeyCodes.WM_DRAWCLIPBOARD: + case KeyCodes.WM_CLIPBOARDUPDATE: if (!_pasting) { HandleCopy(); } - ClipboardListener.SendMessage(_nextClipboardViewer, message.Msg, message.WParam, message.LParam); - break; - case KeyCodes.WM_CHANGECBCHAIN: - if (message.WParam == _nextClipboardViewer) - _nextClipboardViewer = message.LParam; - else - ClipboardListener.SendMessage(_nextClipboardViewer, message.Msg, message.WParam, message.LParam); break; } } @@ -206,6 +196,7 @@ private static void HandlePaste() } else { + _this.ResetTimer(); _this._model.Next(); _this.CurrentClip = _this._model.GetCurrentClip(); _this.CurrentLabel = _this._model.GetCurrentForDisplay() + "/" + _this._model.GetCountForDisplay(); @@ -224,7 +215,11 @@ private void Window_Loaded(object sender, RoutedEventArgs e) _sponge.Handle); _pasteListenerId = _hotKeyListenerPaste.getId(); - _nextClipboardViewer = ClipboardListener.SetClipboardViewer(_sponge.Handle); + bool success = ClipboardListener.AddClipboardFormatListener(_sponge.Handle); + if (!success) + { + MessageBox.Show("Error adding clipboard format listener!", "Qlip Error", MessageBoxButton.OK, MessageBoxImage.Error); + } RegisterPaste(); this.Hide(); @@ -361,7 +356,11 @@ private void Window_PreviewKeyDown(object sender, KeyEventArgs e) private void Window_Closing(object sender, CancelEventArgs e) { _hotKeyListenerPaste.Unregiser(); - ClipboardListener.ChangeClipboardChain(_sponge.Handle, _nextClipboardViewer); + bool success = ClipboardListener.RemoveClipboardFormatListener(_sponge.Handle); + if (!success) + { + MessageBox.Show("Error removing clipboard format listener!", "Qlip Error", MessageBoxButton.OK, MessageBoxImage.Error); + } } ~MainWindow() @@ -372,7 +371,11 @@ private void Window_Closing(object sender, CancelEventArgs e) public void Dispose() { _hotKeyListenerPaste.Unregiser(); - ClipboardListener.ChangeClipboardChain(_sponge.Handle, _nextClipboardViewer); + bool success = ClipboardListener.RemoveClipboardFormatListener(_sponge.Handle); + if (!success) + { + MessageBox.Show("Error removing clipboard format listener!", "Qlip Error", MessageBoxButton.OK, MessageBoxImage.Error); + } } /// diff --git a/Qlip/Native/ClipboardListener.cs b/Qlip/Native/ClipboardListener.cs index 5ec593b..eb6f2ae 100644 --- a/Qlip/Native/ClipboardListener.cs +++ b/Qlip/Native/ClipboardListener.cs @@ -6,13 +6,12 @@ namespace Qlip.Native { class ClipboardListener { - [DllImport("User32.dll", CharSet = CharSet.Auto)] - public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer); + [DllImport("user32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool AddClipboardFormatListener(IntPtr hwnd); - [DllImport("User32.dll", CharSet = CharSet.Auto)] - public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext); - - [DllImport("user32.dll", CharSet = CharSet.Auto)] - public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); + [DllImport("user32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool RemoveClipboardFormatListener(IntPtr hwnd); } } diff --git a/Qlip/Native/KeyCodes.cs b/Qlip/Native/KeyCodes.cs index 3f05d01..7b47403 100644 --- a/Qlip/Native/KeyCodes.cs +++ b/Qlip/Native/KeyCodes.cs @@ -9,8 +9,7 @@ public static class KeyCodes public const int NO_REPEAT = 0x4000; public const int WM_HOTKEY_MSG_ID = 0x0312; - public const int WM_DRAWCLIPBOARD = 0x308; - public const int WM_CHANGECBCHAIN = 0x030D; + public const int WM_CLIPBOARDUPDATE = 0x031D; public const int V = 0x56; } diff --git a/Qlip/SpongeWindow.cs b/Qlip/SpongeWindow.cs index 0c88cb2..f6024cd 100644 --- a/Qlip/SpongeWindow.cs +++ b/Qlip/SpongeWindow.cs @@ -14,7 +14,7 @@ public sealed class SpongeWindow : NativeWindow public SpongeWindow() { CreateHandle(new CreateParams()); - codes = new int[] { KeyCodes.WM_HOTKEY_MSG_ID, KeyCodes.WM_DRAWCLIPBOARD, KeyCodes.WM_CHANGECBCHAIN }; + codes = new int[] { KeyCodes.WM_HOTKEY_MSG_ID, KeyCodes.WM_CLIPBOARDUPDATE }; } protected override void WndProc(ref Message m)