C# 剪切时剪贴板侦听器注册两次

C# 剪切时剪贴板侦听器注册两次,c#,forms,listener,clipboard,cut,C#,Forms,Listener,Clipboard,Cut,只有当我切东西时,它才会记录2次。我怎样才能避免这种情况?有没有办法判断我是复制还是剪切了什么 这是我的代码: [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool AddClipboardFormatListener(IntPtr hwnd); [DllImport("user32.dll", SetLastError = true)]

只有当我切东西时,它才会记录2次。我怎样才能避免这种情况?有没有办法判断我是复制还是剪切了什么

这是我的代码:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AddClipboardFormatListener(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
private const int WM_CLIPBOARDUPDATE = 0x031D;

protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_CLIPBOARDUPDATE)
            {
                MessageBox.Show(Convert.ToString("update in clipboard"));
            }
        }

您是否已尝试检查m.wParam和m.lParam中的消息,可能有线索。仅供参考,您不需要对字符串调用
Convert.ToString()
,您在哪里调用
AddClipboardFormatListener
?您的应用程序有可能注册两次吗?我添加了AddClipboardFormatListener(this.Handle);进入Form1\u Load后,一些应用程序会多次打开剪贴板。主要是在剪切内容时。您可能还没有用PhotoShop分层图像、CorelDraw构图或Excel图表对其进行过测试。计数最多可达到20条以上的消息。顺便说一句,您应该返回
m.Result=(IntPtr)0
作为返回值并移动
base.WndProc(参考m)到方法的末尾。关于复制/剪切:
wParam
可能知道(即使它应该是0)。你得自己核实一下。如果设置为
0x12
,则可能是副本;如果设置为
0x0E
,则可能是剪切。按原样提供。没有保证。