在C#中使用SendMessage在给定的句柄上执行CTRL-C操作 [英] Use SendMessage in C# to perform a CTRL-C operation on a given handle

查看:80
本文介绍了在C#中使用SendMessage在给定的句柄上执行CTRL-C操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Chrome浏览器中,您只需打开对话框并在对话框上执行CTRL-C即可检索JavaScript警报/对话框的文本.这会将对话框中的文本放入剪贴板.

In Google Chrome you can retrieve the text of a JavaScript alert/dialog by simply having the dialog open and doing CTRL-C on the dialog. This will put the text in the dialog into the clipboard.

我正在尝试使用 SendMessage 发送CTRL-C对JavaScript对话框的文本执行复制.我已经设法使用SendInput做到这一点,但是不幸的是,如果窗口没有焦点,这可能会失败.

I'm trying to use SendMessage to send CTRL-C to perform a copy on the text of a JavaScript dialog. I've already managed to do this using SendInput but unfortunately that could fail if the window doesn't have focus.

我已经尝试过将SendMessage与WM_COPY消息一起使用,但这不能满足我的需求.

I have tried using SendMessage with WM_COPY message but that didn't work for my needs.

如果有人在成功使用SendMessage之前已完成此操作,我将非常感谢您的帮助.

Please if anyone has done this before successfully using SendMessage I would greatly appreciate your help.

推荐答案

如果WM_COPY失败,您可以尝试几种方法...

If WM_COPY fails, there are a few avenues you could try...

要伪造按键,您可以发送WM_KEYDOWN和WM_KEYUP消息.

To fake a keypress you can send WM_KEYDOWN and WM_KEYUP messages.

但是,您发送的消息是什么?如果发送"c",则这些消息中将无法通知应用程序也按住了ctrl.

However, what do you send in the message? If you send a "c", then there is no way in these messages for you to inform the application that ctrl was also held down.

您也许可以发送一个字符0x03(这是ctrl + c实际生成的字符代码),但是不能保证接收应用程序会将其解释为"ctrl + c"操作.

You may be able to send a character 0x03 (which is the character code that ctrl+c actually generates), but there is no guarantee that the receiving application will interpret this as a "ctrl+c" action.

为什么它不起作用?正在接收的应用程序可能...

Why might it not work? The receiving application might...

  • 忽略WM_KEYDOWN和WM_KEYUP并使用其他方式读取键盘(例如GetAsyncKeyState以查看按键是否按下)
  • 处理WM_KEYDOWN和/或WM_KEYUP消息,但查找"c",然后使用GetAsyncKeyState()或类似方法检测处理消息时ctrl是否关闭.
  • 如果没有输入焦点,它可能仍然会忽略这些消息,或更糟糕的是,将这些消息当作通过输入焦点窗口接收到的消息来处理.

所以-试试看,但可能无法正常工作.

So - give it a try, but it may not work.

或者,如果SendInput有效,则可以将输入焦点强制到正确的控件SendInput,然后将输入焦点恢复到以前的位置.

Alternatively, if SendInput works, then you could just force the input focus to the correct control, SendInput, and then restore the input focus to its previous location.

另一种方法(可能是最好的一种方法)是,如果它是您已知的对话框,并且包含静态文本字段,则您可能只能找到该子控件和GetText(发送WM_GETTEXT消息),并避免完全使用剪贴板.(或者,如果您需要剪贴板上的文本,请像这样获取文本,然后自己将其放置在剪贴板上).这样可以避免欺骗应用程序以提供文本,并回退到标准的Windows行为.

Another approach (possibly the best one) is if it is a dialog known to you and it contains a static text field, you may just be able to find that child control and GetText on it (send WM_GETTEXT message), and avoid using the clipboard at all. (Or if you need the text on the clipboard, get it like this and then place it on the clipboard yourself). This would avoid tricking the application into providing the text, and fall back to standard Windows behaviour.

这篇关于在C#中使用SendMessage在给定的句柄上执行CTRL-C操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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