csharp 使用AddClipboardFormatListener / WM_CLIPBOARDUPDATE监视C#中的剪贴板更改。请参阅剪贴板类:http://msdn.microsoft.co

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 使用AddClipboardFormatListener / WM_CLIPBOARDUPDATE监视C#中的剪贴板更改。请参阅剪贴板类:http://msdn.microsoft.co相关的知识,希望对你有一定的参考价值。

// from: http://stackoverflow.com/questions/2226920/how-to-monitor-clipboard-content-changes-in-c

/// <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);
}

以上是关于csharp 使用AddClipboardFormatListener / WM_CLIPBOARDUPDATE监视C#中的剪贴板更改。请参阅剪贴板类:http://msdn.microsoft.co的主要内容,如果未能解决你的问题,请参考以下文章

csharp 使用while循环查找从1到n的数字之和。从CSharp的计算机编程基础 - http://www.introprogr

csharp 如何使用方法使用命名参数的示例。来自Microsoft C#参考https://docs.microsoft.com/en-us/dotnet/csharp/programmi

csharp 如何使用方法使用命名参数的示例。来自Microsoft C#参考https://docs.microsoft.com/en-us/dotnet/csharp/programmi

使用正确的数据类型在 CSharp 中加载 C++ dll

csharp 使用列表和代理

csharp 如何使用十六进制颜色