比较剪贴板中保存的数据

发布于 2024-10-17 01:07:03 字数 412 浏览 5 评论 0原文

我正在使用此线程中的代码来捕获剪贴板更改

我到底是什么或者代码到底在做什么是,为我的应用程序注册一个剪贴板查看器,以便能够捕获所做的所有剪贴板更改。

一旦剪贴板更改,WndProc 将触发并运行 OnClipboardChanged() 函数。

我正在使用键盘快捷键 (Ctrl+C) 从 Visual Studio 复制文本作为示例。

当我从 Visual Studi 复制时,数据将被粘贴两次,这意味着 WM_DRAWCLIPBOARD 被调用两次?(请检查代码)

我如何检查或停止此行为?

我只是想确保粘贴的数据不会重复。?

I am using the code in this thread to catch the Clipboard changes.

what iam exactly or what the code is exactly doing is, registering a clipboardviewer for my application to be able to catch all the clipboard changes made.

once the clipboard changes the WndProc will fire and run the OnClipboardChanged() function..

iam copying the text from visual studio as example by using the keyboard shortcut (Ctrl+C)

When I copy from Visual Studi,, the data will be pasted twice, which means the WM_DRAWCLIPBOARD is called twice?(please check the code)

How can i check or stop this behaviour?

I just want to make sure that the data pasted won't be duplicated.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-10-24 01:07:03

您可以存储正在复制的数据的哈希码,并查看最后一个是否匹配。这是一个未经测试的文本数据对象示例:

        IDataObject iData = Clipboard.GetDataObject();

        Int hash = iData != null ? iData.GetData(DataFormats.Text).GetHashCode() : 0;

        if (ClipboardChanged != null && hash != lastHash)
        {
            ClipboardChanged(this, new ClipboardChangedEventArgs(iData));
            lastHash = hash;
        }

我不确定一般情况下执行此操作的最佳方法,您始终可以使用 GetFormats() 并检索所有格式+哈希对并使用因为我不确定不同格式之间不会发生冲突。

You could store the hash code of the data being copied and look if the last one match. Here's an untested example for text data objects:

        IDataObject iData = Clipboard.GetDataObject();

        Int hash = iData != null ? iData.GetData(DataFormats.Text).GetHashCode() : 0;

        if (ClipboardChanged != null && hash != lastHash)
        {
            ClipboardChanged(this, new ClipboardChangedEventArgs(iData));
            lastHash = hash;
        }

I'm not sure of the best way to do it in generically, you could always use GetFormats() and retrieve all format+hash pairs and do the lookup with them as I'm not sure there can't be clashes across different formats.

紧拥背影 2024-10-24 01:07:03

我知道这是剪贴板链中某个地方的已知错误。针对这种行为,您可以简单地做的就是过滤这些伪调用。我只是在检查通话之间的时间间隔是否是人为的。就这样:

    DateTime dtLastChangedNotify = DateTime.MinValue;
    TimeSpan tsHumanReactionTime = TimeSpan.FromMilliseconds(100);
    private void MessageProc(IntPtr hwnd, int Msg, IntPtr WParam, IntPtr LParam, ref bool handled)
    {
        if (Msg == WM_CHANGECBCHAIN)
        {
            if (WParam == _nextCBWatcher)
            {
                _nextCBWatcher = LParam;
            }
            else
            {
                SendMessage(_nextCBWatcher, Msg, WParam, LParam);
            }
        }
        else if (Msg == WM_DRAWCLIPBOARD)
        {
            uint cpid = 0, pid = 0;

            if (_wasReset)
            {
                _wasReset = false;
                return;
            }

            GetWindowThreadProcessId(GetClipboardOwner(), out pid);
            cpid = GetCurrentProcessId();

            // i only want info about what is copied by other programs.
            if (pid != cpid)
            {
                // filter no human calls.
                if ((DateTime.Now - dtLastChangedNotify) > tsHumanReactionTime)
                {
                    OnClipboardChange();
                    dtLastChangedNotify = DateTime.Now;
                }
            }

            SendMessage(_nextCBWatcher, Msg, WParam, LParam);
        }
        else if (Msg == WM_DESTROY)
        {
            ChangeClipboardChain(_ownerWnd, _nextCBWatcher);
        }
    }

I know that this is an known error somewhere in the ClipboardChain. What you can simply do against this behavior is filter those pseudo calls. I am just checking if the timespan between the calls seems to be human. here it goes:

    DateTime dtLastChangedNotify = DateTime.MinValue;
    TimeSpan tsHumanReactionTime = TimeSpan.FromMilliseconds(100);
    private void MessageProc(IntPtr hwnd, int Msg, IntPtr WParam, IntPtr LParam, ref bool handled)
    {
        if (Msg == WM_CHANGECBCHAIN)
        {
            if (WParam == _nextCBWatcher)
            {
                _nextCBWatcher = LParam;
            }
            else
            {
                SendMessage(_nextCBWatcher, Msg, WParam, LParam);
            }
        }
        else if (Msg == WM_DRAWCLIPBOARD)
        {
            uint cpid = 0, pid = 0;

            if (_wasReset)
            {
                _wasReset = false;
                return;
            }

            GetWindowThreadProcessId(GetClipboardOwner(), out pid);
            cpid = GetCurrentProcessId();

            // i only want info about what is copied by other programs.
            if (pid != cpid)
            {
                // filter no human calls.
                if ((DateTime.Now - dtLastChangedNotify) > tsHumanReactionTime)
                {
                    OnClipboardChange();
                    dtLastChangedNotify = DateTime.Now;
                }
            }

            SendMessage(_nextCBWatcher, Msg, WParam, LParam);
        }
        else if (Msg == WM_DESTROY)
        {
            ChangeClipboardChain(_ownerWnd, _nextCBWatcher);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文