如何在 C# 中监视剪贴板内容的变化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2226920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I monitor clipboard content changes in C#?
提问by Weiming
I want to have this feature in my C# program: When the user do Ctrl+ Cor Copy anywhere (i.e. when the clipboard content changes), my program will get notified, and check whether the content met certain criteria, if so, become the active program, and process the content, etc.
我想在我的 C# 程序中有这个功能:当用户在任何地方执行Ctrl+C或复制时(即当剪贴板内容更改时),我的程序将收到通知,并检查内容是否满足某些条件,如果是,则成为活动程序,并处理内容等。
I can get the contents out from System.Windows.Forms.Clipboard
, however, I don't know how to monitor the content changes from the clipboard.
我可以从 中获取内容System.Windows.Forms.Clipboard
,但是,我不知道如何从剪贴板监控内容更改。
If using Windows Vista or later, use AddClipboardFormatListener
as in John Knoeller's answer, for Windows XP, I have to use the older, more fragile SetClipboardViewer
API, as in the accepted answer.
如果使用 Windows Vista 或更高版本,请使用AddClipboardFormatListener
John Knoeller 的答案,对于 Windows XP,我必须使用旧的、更脆弱的SetClipboardViewer
API,如已接受的答案。
采纳答案by John Knoeller
You could use SetClipboardViewerprovided by Win32 API (through P/Invoke).
您可以使用Win32 API 提供的SetClipboardViewer(通过 P/Invoke)。
Here is a page which contains code to set one up in C#: http://www.codeguru.com/csharp/.net/net_general/tipstricks/article.php/c7315/
这是一个包含在 C# 中设置代码的页面:http: //www.codeguru.com/csharp/.net/net_general/tipstricks/article.php/c7315/
回答by John Knoeller
You can do this with pinvoke to the Win32 API AddClipboardFormatListener
您可以通过对 Win32 API AddClipboardFormatListener 的pinvoke 执行此操作
The listener is a window handle (Form.Handle), and the form will be notified of changes with a WM_CLIPBOARDUPDATEnotification
侦听器是一个窗口句柄(Form.Handle),窗体将通过WM_CLIPBOARDUPDATE通知通知更改
It is a more robust replacement for the older SetClipboardViewer
API.
它是旧SetClipboardViewer
API的更强大的替代品。
回答by μBio
The Win32 API contains a function SetClipboardViewer.
Win32 API 包含一个函数 SetClipboardViewer。
回答by Justin
I've written up a small utility class that uses the AddClipboardFormatListener function function with a Message-onlywindow to do just this.
我已经编写了一个小型实用程序类,它使用AddClipboardFormatListener函数和一个仅消息窗口来执行此操作。
/// <summary>
/// Provides notifications when the contents of the clipboard is updated.
/// </summary>
public sealed class ClipboardNotification
{
/// <summary>
/// Occurs when the contents of the clipboard is updated.
/// </summary>
public static event EventHandler ClipboardUpdate;
private static NotificationForm _form = new NotificationForm();
/// <summary>
/// Raises the <see cref="ClipboardUpdate"/> event.
/// </summary>
/// <param name="e">Event arguments for the event.</param>
private static void OnClipboardUpdate(EventArgs e)
{
var handler = ClipboardUpdate;
if (handler != null)
{
handler(null, e);
}
}
/// <summary>
/// Hidden form to recieve the WM_CLIPBOARDUPDATE message.
/// </summary>
private class NotificationForm : Form
{
public NotificationForm()
{
NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE);
NativeMethods.AddClipboardFormatListener(Handle);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == NativeMethods.WM_CLIPBOARDUPDATE)
{
OnClipboardUpdate(null);
}
base.WndProc(ref m);
}
}
}
internal static class NativeMethods
{
// See http://msdn.microsoft.com/en-us/library/ms649021%28v=vs.85%29.aspx
public const int WM_CLIPBOARDUPDATE = 0x031D;
public static IntPtr HWND_MESSAGE = new IntPtr(-3);
// See http://msdn.microsoft.com/en-us/library/ms632599%28VS.85%29.aspx#message_only
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AddClipboardFormatListener(IntPtr hwnd);
// See http://msdn.microsoft.com/en-us/library/ms633541%28v=vs.85%29.aspx
// See http://msdn.microsoft.com/en-us/library/ms649033%28VS.85%29.aspx
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
}
This class assumes that the notifications are always needed for the duration of the applications lifetime, however it could be modified to provide the ability to unsubscribe via the RemoveClipboardFormatListener function if required.
此类假定在应用程序生命周期内始终需要通知,但可以对其进行修改以提供在需要时通过RemoveClipboardFormatListener 函数取消订阅的功能。