如何从未聚焦或隐藏的形式控制剪贴板 [英] How to control Clipboard from an unfocused or hidden form

查看:77
本文介绍了如何从未聚焦或隐藏的形式控制剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,其中点击热键会将图像复制到剪贴板。我很怀疑这是Windows限制,但如果我的窗口被隐藏或没有焦点,图像就不会被复制。当我点击热门时运行的方法仍然有效,图像仍然被拍摄,唯一的是图像不会被复制到剪贴板,直到下次我关注应用程序,即使它''' 5分钟后。所以基本上,我的应用程序没有重点,我点热键。应该发生的一切(除了图像进入剪贴板)发生。图像尚未复制到剪贴板。现在,我关注应用程序,一旦我这样做,图像就会被复制。


这是Windows的限制,不允许应用程序对剪贴板进行独占控制,或者那是我可以解决的问题吗?我想我可以简单地记下当前激活的窗口,激活我的窗口,将图像复制到剪贴板,然后重新激活在我点击热键之前激活的窗口。唯一的问题是,我打算使用通知图标隐藏我的应用程序窗口,所以我不知道这是否会起作用。


任何人都可以帮助我出来?

(是的,我在一周前问过类似的问题,但当时我不知道造成这个问题的原因。我以为是键盘钩子造成它,而不是剪贴板,所以这更具体。)

I''m writing a program in which hitting a hotkey will copy an image to the clipboard. I''m prety sure this is a Windows limitation, but if my window is hidden or doesn''t have the focus, the image doesn''t get copied. The method that runs when I hit the hotke still does its thing, the image still gets taken, the only thing is that the image won''t be copied to the clipboard until the next time I focus the application, even if it''s 5 minutes later. So basically, my application is unfocused, I hit the hotkey. Everything that is supposed to happen (except the image going to the clipboard) happens. The image hasn''t been copied to the clipboard. Now, I focus the application and as soon as I do that, the image is copied.

Is that a limitation of Windows not allowing an application to have exclusive control over the clipboard, or is that something I could work around? I was thinking I could simply take note of the currently activated window, activate my window, copythe image to the clipboard, then reactivate the window that was activated before I hit the hotkey. The only problem with that is, I plan on using a notification icon to hide my application window, so I don''t know if that''ll work.

Can anyone help me out?

(Yes, I asked a question similar to this a week ago, but at that time I didn''t know what was causing the problem. I thought it was the keyboard hook causing it, not the clipboard, so this is more specific.)

推荐答案

那......真的很奇怪!我不认为你已经尝试将剪贴板内容放在另一个线程中,而不是你的GUI线程?我不知道这是否会有所帮助,我只是在抛弃想法。


您也可以强制热键来关注您的应用程序,但这可能是不受欢迎的功能。 ..
That''s... really weird! I don''t suppose you''ve tried putting the clipboard stuff in another thread, not your GUI thread? I don''t know if that will help, I''m just throwing out ideas.

You could also force the hotkey to focus your app, but that might be undesirable functionality...


我没想到将剪贴板副本传递给新的线程,后台工作人员会工作吗?


那个'实际上我正在计划做什么,使用P / Invoke来获取前景窗口句柄,激活我的应用程序,复制图像,恢复前景窗口,但是如果应用程序被隐藏到通知图标。


我会调查线程,希望这会工作。谢谢!
I didn''t think of passing the clipboard copy into a new thread, would a background worker work?

And that''s actually what I was planning on doing, using P/Invoke to get the foreground window handle, activate my app, copy the image, the restore the foreground window, but idk if that''ll work if the application is hidden to a notification icon.

I''ll look into threading, hopefully that''ll work. Thanks!


这似乎不太可能发生。

剪贴板控制是在Windows中。使其保留似乎不太可能将它放在剪贴板中,直到你的应用程序集中为止。

我认为它更多地与.NET缓存你的应用程序中的剪贴板内容而不是刷新它直到某些点消息循环。


您是否有我们可以使用的最小功能示例代码?
That seems unlikely behavior.
The "clipboard" controlling is in windows. For it to "retain" what you did without putting it in the clipboard until your application is focused seems unlikely.
I would think it has more to do with .NET cacheing the clipboard contents in your application and not refreshing it until somepoint in the message loop.

Do you have a minimal functioning sample code we could play with?


这篇关于如何从未聚焦或隐藏的形式控制剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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