如何访问操作系统复制到的变量 [英] How to access the variable to which OS copies

查看:21
本文介绍了如何访问操作系统复制到的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 jDownloader 软件中看到的一个有趣功能是我在浏览器窗口中复制的任何链接(即 Ctrl+c),复制的内容链接会自动出现(即实际上没有我粘贴)到他们的用户界面中,然后开始从链接下载内容(如果链接有效).

An interesting feature, I have seen in jDownloader software is any links I copy in the browser window (i.e., Ctrl+c), the copied content links automatically appears ( i.e., with out me actually pasting it) in their UI and starts downloading the content from the links, if they are valid.

我想编写相同的程序,但对如何访问操作系统复制到的变量感到困惑.请分享您的想法.

I would like to program the same but am puzzled as how to access the variable to which OS copies. Please share your ideas.

谢谢.

推荐答案

Adam Robinson 的答案 是正确的,但并不完全正确.我将尝试提供长"版本(与他的短"版本相反),并在此过程中解释我认为他提出的解决方案在何处/为什么未能实现您的最终目标.

Adam Robinson's answer is on the right track, but is not entirely correct. I'm going to try and provide the "long" version (in contrast to his "short" version), and explain along the way where/why I think the solution that he proposes falls short of achieving your ultimate goal.

正如他链接到的 文档 所解释的那样,有是监视 Windows 剪贴板更改的三种不同方法,每种方法都有自己的注意事项:

As the documentation he links to explains, there are three different ways of monitoring changes to the Windows clipboard, each with their own caveats:

  1. 创建一个剪贴板查看器窗口,该窗口挂接到剪贴板查看器链并在用户更改剪贴板内容时接收通知消息.(适用于所有版本的 Windows,但通常更难编码,因此不鼓励对其功能没有特定需求的较新应用程序.)

  1. Creating a clipboard viewer window that hooks into the clipboard viewer chain and receives notification messages when the contents of the clipboard have been changed by the user. (Available on all versions of Windows, but generally more difficult to code and thus discouraged for newer applications that don't have a specific need for its features.)

查询剪贴板序列号,这是一个 32 位的值,每次剪贴板的内容改变时都会改变.您的程序调用 Windows API 函数 GetClipboardSequenceNumber 一次并缓存它的值,然后每次您想检查剪贴板的内容是否已更改时,您再次调用相同的函数并将其返回值与您缓存的值进行比较.这里有两个重要的警告:

Querying the clipboard sequence number, which is a 32-bit value that changes each time the clipboard's contents are changed. Your program calls the Windows API function GetClipboardSequenceNumber once and caches its value, then each time you want to check if the clipboard's contents have changed, you call that same function again and compare its return value to the value you've cached. There are two important caveats here:

  1. 此功能仅在 Windows 2000 及更新版本中可用.如果您正在编写 .NET 应用程序,这不太可能成为问题,因为早在 3.0 的框架版本就已经放弃了 W2K 支持.

  1. This function is only available in Windows 2000 and newer. This is not likely to be a problem if you're writing .NET apps, as versions of the Framework as early as 3.0 dropped W2K support.

不是一个通知方法,你不应该在轮询循环中重复调用这个函数.这意味着您必须手动调用适当的函数并比较剪贴板序列号.如果您想收听"并在剪贴板的内容发生更改时立即收到通知,则不能使用此方法,如您在问题中所述.文档在这里非常明确:

This is not a notification method, and you should not call this function repeatedly in a polling loop. That means that you have to manually call the appropriate function and compare the clipboard sequence number. You cannot use this method if you want to "listen in" and be immediately notified whenever the clipboard's contents change, as you describe in your question. The documentation is very explicit here:

这种方法更适合基于当前剪贴板内容缓存结果的程序,并且在使用该缓存的结果之前需要知道计算是否仍然有效.请注意,这不是通知方法,不应在轮询循环中使用.要在剪贴板内容更改时收到通知,请使用剪贴板格式侦听器或剪贴板查看器.

This method is more suitable to programs which cache results based on the current clipboard contents and need to know whether the calculations are still valid before using the results from that cache. Note that this is a not a notification method and should not be used in a polling loop. To be notified when clipboard contents change, use a clipboard format listener or a clipboard viewer.

  • 创建一个剪贴板格式监听器,当剪贴板的内容发生变化时,它会注册接收通知.在您的情况下,这是理想的解决方案,因为它避免了创建剪贴板查看器窗口(选项 1)的复杂性,而且还允许您在每次更改剪贴板内容时收听并收到通知(与选项 2 相比).

  • Creating a clipboard format listener, which registers to be notified whenever the clipboard's contents change. This is the ideal solution in your case, because it avoids the complexities of creating a clipboard viewer window (option 1), but also allows you listen in and be notified each time the clipboard's contents are changed (in contrast to option 2).

    问题是这仅在 Windows Vista 及更高版本下可用.如果您仍然需要针对 Windows XP(正如我们大多数人所做的那样),这真的不是您的选择.

    The problem is that this is only available under Windows Vista and later. If you still have any need to target Windows XP (as most of us do), this is really not an option for you.


    因此,从您在问题中提供的示例来看,在我看来,您唯一可用的选项是选项 1,即创建剪贴板查看器窗口.该文档详细介绍了如何使用 <代码>SetClipboardViewer 函数并监听WM_DRAWCLIPBOARDWM_CHANGECBCHAIN 消息.让它正常工作可能是您自己完成的一项艰巨任务,但对我们 .NET 开发人员来说幸运的是,其他人已经为我们完成了艰苦的工作.(我说其他人",尽管我自己其中的一员.)

    这篇关于 CodeProject 的文章就是一个很好的例子.它实现了三种不同类型的钩子:鼠标钩子、键盘钩子和剪贴板钩子.您唯一感兴趣的是剪贴板挂钩,但您只需在项目中添加对 DLL 的引用即可立即开始使用其功能.

    This article on CodeProject is a good example. It implements three different types of hooks: a mouse hook, a keyboard hook, and a clipboard hook. The only thing you're interested in is the clipboard hook, but you can just add a reference to the DLL in your project to start using its functionality immediately.

    如果您对其工作原理感兴趣并想尝试自己编写代码,这篇文章 似乎是对所涉及的具体步骤的精彩描述.

    If you are interested in the internals of how this works and want to try coding it up yourself, this article appears to be a fantastic description of the specific steps involved.

    这篇关于如何访问操作系统复制到的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆