我实现了属于我的this 项目。好的,我该如何处理,让我解释一下。
应该考虑两个主要的事情。
- 如何在任何窗口中获取文本?
- 我应该把它存放在哪里?
所以,@jcrada 的回答包含一个优点,即选项 1。
根据上述方法,步骤必须是:
- 从 Nuget 添加 globalmousekeyhook。
- 通过 Usr32.dll 注册 ClipboardContainsText 事件
- 注册鼠标右键事件
- 开始聆听
首先,创建包含剪贴板事件的 Win32 帮助类。
/// <summary>
/// This static class holds the Win32 function declarations and constants needed by
/// this sample application.
/// </summary>
internal static class Win32
{
/// <summary>
/// The WM_DRAWCLIPBOARD message notifies a clipboard viewer window that
/// the content of the clipboard has changed.
/// </summary>
internal const int WmDrawclipboard = 0x0308;
/// <summary>
/// A clipboard viewer window receives the WM_CHANGECBCHAIN message when
/// another window is removing itself from the clipboard viewer chain.
/// </summary>
internal const int WmChangecbchain = 0x030D;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}
其次,注册鼠标和剪贴板事件,
public void Initialize()
{
var wih = new WindowInteropHelper(this.mainWindow);
this.hWndSource = HwndSource.FromHwnd(wih.Handle);
this.globalMouseHook = Hook.GlobalEvents();
this.mainWindow.CancellationTokenSource = new CancellationTokenSource();
var source = this.hWndSource;
if (source != null)
{
source.AddHook(this.WinProc); // start processing window messages
this.hWndNextViewer = Win32.SetClipboardViewer(source.Handle); // set this window as a viewer
}
this.SubscribeLocalevents();
this.growlNotifications.Top = SystemParameters.WorkArea.Top + this.startupConfiguration.TopOffset;
this.growlNotifications.Left = SystemParameters.WorkArea.Left + SystemParameters.WorkArea.Width - this.startupConfiguration.LeftOffset;
this.IsInitialized = true;
}
鼠标事件;
private void SubscribeLocalevents()
{
this.globalMouseHook.MouseDoubleClick += async (o, args) => await this.MouseDoubleClicked(o, args);
this.globalMouseHook.MouseDown += async (o, args) => await this.MouseDown(o, args);
this.globalMouseHook.MouseUp += async (o, args) => await this.MouseUp(o, args);
}
private async Task MouseUp(object sender, MouseEventArgs e)
{
this.mouseSecondPoint = e.Location;
if (this.isMouseDown && !this.mouseSecondPoint.Equals(this.mouseFirstPoint))
{
await Task.Run(() =>
{
if (this.mainWindow.CancellationTokenSource.Token.IsCancellationRequested)
return;
SendKeys.SendWait("^c");
});
this.isMouseDown = false;
}
this.isMouseDown = false;
}
private async Task MouseDown(object sender, MouseEventArgs e)
{
await Task.Run(() =>
{
if (this.mainWindow.CancellationTokenSource.Token.IsCancellationRequested)
return;
this.mouseFirstPoint = e.Location;
this.isMouseDown = true;
});
}
private async Task MouseDoubleClicked(object sender, MouseEventArgs e)
{
this.isMouseDown = false;
await Task.Run(() =>
{
if (this.mainWindow.CancellationTokenSource.Token.IsCancellationRequested)
return;
SendKeys.SendWait("^c");
});
}
最后一部分,当我们被抓住时我们会怎么做,
private IntPtr WinProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case Win32.WmChangecbchain:
if (wParam == this.hWndNextViewer)
this.hWndNextViewer = lParam; //clipboard viewer chain changed, need to fix it.
else if (this.hWndNextViewer != IntPtr.Zero)
Win32.SendMessage(this.hWndNextViewer, msg, wParam, lParam); //pass the message to the next viewer.
break;
case Win32.WmDrawclipboard:
Win32.SendMessage(this.hWndNextViewer, msg, wParam, lParam); //pass the message to the next viewer //clipboard content changed
if (Clipboard.ContainsText() && !string.IsNullOrEmpty(Clipboard.GetText().Trim()))
{
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Background,
(Action)
delegate
{
var currentText = Clipboard.GetText().RemoveSpecialCharacters();
if (!string.IsNullOrEmpty(currentText))
{
//In this section, we are doing something, because TEXT IS CAPTURED.
Task.Run(
async () =>
{
if (this.mainWindow.CancellationTokenSource.Token.IsCancellationRequested)
return;
await
this.WhenClipboardContainsTextEventHandler.InvokeSafelyAsync(this,
new WhenClipboardContainsTextEventArgs { CurrentString = currentText });
});
}
});
}
break;
}
return IntPtr.Zero;
}
诀窍是在另一方面向窗口或操作系统发送复制命令 Control+C 命令,所以SendKeys.SendWait("^c"); 这样做。