Skip to content

Commit

Permalink
Changed winAPI methods. fixed pasting bug
Browse files Browse the repository at this point in the history
  • Loading branch information
robbinsa530 committed Apr 3, 2020
1 parent 76687a4 commit 562b7ea
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,7 @@ ASALocalRun/
*.nvuser

# MFractors (Xamarin productivity tool) working folder
.mfractor/
.mfractor/

# Other
qlip-notes.txt
35 changes: 19 additions & 16 deletions Qlip/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public partial class MainWindow : Window, INotifyPropertyChanged, IDisposable
/// </summary>
private bool _exited = false;

/// <summary>
/// Used for capturing system clip events
/// </summary>
private IntPtr _nextClipboardViewer;

/// <summary>
/// Timer used for auto pasting when no action for some time
/// </summary>
Expand Down Expand Up @@ -131,6 +126,8 @@ public MainWindow(Model model)

_sponge = new SpongeWindow();
_sponge.WndProcCalled += (s, e) => ProcessMessage(e);

HandleCopy(); // Grab existing clip if one exists
}

/// <summary>
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}
}

/// <summary>
Expand Down
13 changes: 6 additions & 7 deletions Qlip/Native/ClipboardListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 1 addition & 2 deletions Qlip/Native/KeyCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Qlip/SpongeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 562b7ea

Please sign in to comment.