Is there a clipboard changed or updated event that i can access through C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
There are multiple ways of doing this but this is my favorite and works for me. I've created a class library so that others may add the project and include the DLL then simply call on it and use it wherever they want within their applications.
This answer was made with the help of this one.
More steps under code.
You may now type ClipboardMonitor.Start or .Stop or .OnClipboardChanged
Ok so this is an old post but we found a solution that seems very simple compared to the current set of answers. We are using WPF and we wanted to have our own custom Commands (in a ContextMenu) enable and disable if the Clipboard contains text. There is already an ApplicationCommands.Cut, Copy and Paste and these commands respond correctly to the clipboard changing. So we just added the following EventHandler.
We actually are controlling the CanExecute on our own Command this way. Works for what we needed and maybe it will help others out there.
here is a good example of using
AddClipboardFormatListener
.In order to do that we need to pinvoke the
AddClipboardFormatListener
andRemoveClipboardFormatListener
.Then we need to add our window to the clipboard format listener list by calling the
AddClipboardFormatListener
method with our window’s handle as a parameter. Place the following code in your main window form constructor or any of its load events.Override the
WndProc
method so we can catch when the WM_CLIPBOARDUPDATE is send.And finally make sure to remove your main window from the clipboard format listener list before closing your form.
I had this challenge in WPF and ended up using the approach described below. For windows forms there are excellent examples elsewhere in this answer, such as the ClipboardHelper control.
For WPF we cannot override WndProc, so we have to hook it explicitly with an HwndSource AddHook call using the Source from a window. The clipboard listener still uses the AddClipboardFormatListener native interop call.
Native methods:
Clipboard Manager class:
This gets used in a WPF window by adding the event in OnSourceInitialized or later such as the Window.Loaded event or during operation. (when we have enough information to use the native hooks):
I'm using this approach in a Path of Exile item analyzer project, as the game exposes item information via the clipboard when you hit Ctrl-C.
https://github.com/ColinDabritz/PoeItemAnalyzer
I hope this helps someone with WPF clipboard change handling!
I think you'll have to use some p/invoke:
See this article on how to set up a clipboard monitor in c#
Basically you register your app as a clipboard viewer using
and then you will recieve the
WM_DRAWCLIPBOARD
message, which you can handle by overridingWndProc
:(There's more to be done; passing things along the clipboard chain and unregistering your view, but you can get that from the article)
I believe one of the earlier solutions doesn't check for a null on the dispose method: