Use SendMessage in C# to perform a CTRL-C operation on a given handle

余生颓废 提交于 2021-02-08 11:03:30

问题


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.

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.

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

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


回答1:


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

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

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.

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...

  • ignore WM_KEYDOWN and WM_KEYUP and use other means to read the keyboard (e.g. GetAsyncKeyState to see if a key is down)
  • handle WM_KEYDOWN and/or WM_KEYUP messages, but look for "c" and then use GetAsyncKeyState() or similar to detect if ctrl is down when the message is processed.
  • It might still ignore the messages if it doesn't have input focus, or worse, action those messages as though they were received through the input focus window.

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

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.

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.




回答2:


Try using these

WM_CUT = 0x300
WM_COPY = 0x301
WM_PASTE = 0x302
WM_CLEAR = 0x303

as the wmsg param depending on your operation.




回答3:


Since the question is a bit vague as to whether you are talking about sending a Ctrl+C keystroke to terminate a process, or to copy a selection to the clipboard. If it is the former, please see this answer here and here on SO which was asked on a previous occasion, where an OP was asking how to send a Ctrl+C keystrokes to a shell process in order to terminate it.

If you are talking about Ctrl+C for copying a selection to the clipboard, that is even trickier as to whether the selection has indeed being selected first prior to copying to the clipboard...

If I am going off the wrong angle, at least, please edit your question to make it more clearer!




回答4:


I realise this is an ancient question, but given it hasn't been satisfactorily answered, allow me to oblige for anyone else looking:

SendMessage() is not required. Simply use:

System.Windows.Forms.SendKeys.SendWait("^(c)");

And if you want to paste:

System.Windows.Forms.SendKeys.SendWait("^(v)");


来源:https://stackoverflow.com/questions/2563696/use-sendmessage-in-c-sharp-to-perform-a-ctrl-c-operation-on-a-given-handle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!