-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Hello, I've remixed a Notification.cs class to work with UWP projects, If you're interested I could push it to seperate branch.
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace ClipboardTools
{
/// <summary>
/// Provides notifications when the contents of the clipboard is updated.
/// From: https://stackoverflow.com/questions/621577/clipboard-event-c-sharp
/// </summary>
public sealed class UWPNotification
{
/// <summary>
/// Occurs when the contents of the clipboard is updated.
/// </summary>
public static event EventHandler ClipboardUpdate;
private static NotificationWindow _window;
public UWPNotification(HwndSource source)
{
_window = new NotificationWindow(source);
}
/// <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 window to recieve the WM_CLIPBOARDUPDATE message.
/// It gets HwndSource from main window, necessary handler to make Notification class work.
/// </summary>
private partial class NotificationWindow : Window
{
public NotificationWindow(HwndSource source)
{
NativeMethods.AddClipboardFormatListener(source.Handle);
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == NativeMethods.WM_CLIPBOARDUPDATE)
{
OnClipboardUpdate(null);
}
return IntPtr.Zero;
}
}
}
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);
}
}
Metadata
Metadata
Assignees
Labels
No labels