C# - 覆盖 Ctrl+C 控件

发布于 2024-08-07 06:52:54 字数 534 浏览 3 评论 0原文

我正在考虑创建一个简单的应用程序来存储我使用 Ctrl+C 复制的内容。现在我在谷歌上搜索了一些有趣的代码:(我宁愿发布它的链接,因为它很大)

http://www.prilepi.com/221 prilepi.com/221(作者:http://www.liensberger。 it/web/blog/?p=207)

http://www.prilepi.com/222

现在一切正常,唯一的问题是它完全覆盖了所有内容。这意味着当我选择文本并按 Ctrl+CI 时无法获取我选择的文本。

我知道 Clipboard 类,但没有任何内容存储在...

Im thinking to create a simple app to store what I have copied using Ctrl+C. Now I have Googled some interesting code: (I will rather post link to it as its huge)

http://www.prilepi.com/221 (by http://www.liensberger.it/web/blog/?p=207)

http://www.prilepi.com/222

Now the thing works fine, the only problem is that it totally overwrites everything. That means that when I select text and hit Ctrl+C I cannot get the text I selected.

I'm aware of Clipboard class but nothing gets stored in...

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

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

发布评论

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

评论(3

以为你会在 2024-08-14 06:52:54

正如您所说,热键完全隐藏了来自其他窗口的消息。您需要做的是找到具有焦点的窗口,然后读取它选择的文本(然后可能手动将其添加到剪贴板,我猜?)

此代码片段应该找到焦点窗口。它是用 C++ 编写的,但您可以轻松地将其转换为 C#。

HWND GetGlobalFocus()
{
   GUITHREADINGO info;
   info.cbSize = sizeof(info);

   if (!GetGUIThreadInfo(0, &info))
      return NULL;

   return info.hwndFocus;
}

一旦你有了这个……这就是棘手的地方。您可以执行 PostMessage(hWnd, WM_COPY, 0, 0); 但如果控件不支持此操作,它将不起作用(任何语法突出显示的控件很可能是非标准的,因此可能不回复此)。

您可以手动发送 WM_GETTEXT 消息来获取文本,然后手动将其添加到剪贴板,但这很可能会失败,因为控件严重非标准,更不用说它不会保留应用程序可能的多种剪贴板格式(例如Word)。

另一种选择是,当您收到热键时,禁用挂钩,使用 keybd_event 再次发送组合键,然后再次启用挂钩,这样您的剪贴板中就会包含数据。这看起来很笨重,但它可以工作,具体取决于 keybd_event 是否阻塞,我不记得了。

希望这有帮助!

The hot key completely hides the message from other windows, as you said. What you need to do is to find the window with the focus and then read the text it has selected (and then possibly add it to the clipboard manually I guess?)

This snippet should find the focused window. It's in C++ but you can easily translate it to C#.

HWND GetGlobalFocus()
{
   GUITHREADINGO info;
   info.cbSize = sizeof(info);

   if (!GetGUIThreadInfo(0, &info))
      return NULL;

   return info.hwndFocus;
}

Once you have that.. this is where it gets tricky. You could do a PostMessage(hWnd, WM_COPY, 0, 0); but it won't work if the control doesn't support this (any syntax highlighted control is most likely non standard and as such might not reply to this).

You could manually send a WM_GETTEXT message to get the text then manually add it to the clipboard, but again this will most likely fail it the control is heavily non-standard, not to mention that it won't preserve the applications possible multiple clipboard formats (think Word).

Another option is when you receive your hot key, disable your hook, send the key combination again with keybd_event and then enable your hook again, and you'll have the data in your clipboard. This seems clunky but it could work depending if keybd_event is blocking or not, I can't remember.

Hope this helps!

蓝天 2024-08-14 06:52:54

要将内容添加到剪贴板,您可以使用:

Clipboard.SetText(Text, TextDataFormat.Text);

除了文本之外,还有其他几种格式选项

To add somthing to the clipboard you can use:

Clipboard.SetText(Text, TextDataFormat.Text);

There are several other format options than just text as well

温折酒 2024-08-14 06:52:54

还有另一种方法,虽然它有点弱,假设您有一个正在等待输入的文本框,您可以通过创建一个没有任何内容的新上下文菜单来分配一个空白上下文菜单,然后将其分配给文本框。这样,用户无法右键单击来调出用于复制/粘贴的默认上下文菜单。

编辑: 由于OP发布了一个不太清楚的问题,“C#覆盖Ctrl+C代码”,我认为他的意思是,防止使用Ctrl+C 复制到剪贴板。抱歉,如果我的回答有误。您能否再澄清一下您的问题,因为它听起来具有误导性。

编辑#2:在 OP 发表评论后,我可以提供一些关于他想要完成的任务的线索,首先,前往 CodeProject 并阅读这篇文章,然后将剪贴板挂钩钩在全球基础,工作完成!无需放入全局键盘挂钩!连接事件 ClipboardChangedClipboardStateChanged

希望这有帮助,
此致,
汤姆.

There is another way of doing it, although it is a bit weak, suppose you have a textbox that is awaiting for input, you can assign a blank context menu by creating a new context menu with nothing on it, then assign it to the textbox. In that way, a user cannot right click to bring up the default context menu for copying/pasting.

Edit: Since the OP posted a question that is not exactly clear, 'C# Overwrite Ctrl+C code', I thought he meant, to prevent the usage of Ctrl+C to copy to the clipboard. Sorry if my answer is wrong. Can you please clarify your question a bit more as it sounds misleading.

Edit#2: After the OP's comments, I can give a clue on what he is trying to accomplish, for starters, head over to CodeProject and read this article and hook in the clipboard hook on a global basis and the job is done! No need to put in a global keyboard hook! Wire up the event ClipboardChanged and ClipboardStateChanged.

Hope this helps,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文