如何在Vb.Net中使用Sendmessage()模拟Control + A键 [英] How Can I Simulate The Control + A Key Using Sendmessage() In Vb.Net

查看:398
本文介绍了如何在Vb.Net中使用Sendmessage()模拟Control + A键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在vb.net中使用按钮按下从文档中选择文本。所以,我想在vb.net中使用Sendmessage()模拟控件+ A键。我尝试使用keybd_event它不起作用,甚至sendkeys.send()也不起作用。



这是我到目前为止尝试的

I want to select a text from a document using a button press in vb.net. So, i wanted to simulate the control + A key using Sendmessage() in vb.net. I tried using keybd_event and it doesn't work and even sendkeys.send() doesnot work too.

This is what i tried so far

 Private Const WM_KEYDOWN = &H100
 Const VK_A = &H41
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       
        SendMessage(Handle, WM_KEYDOWN, VK_A, 0)
End sub





请帮帮我!



Please help me out!

推荐答案

请使用经证实运作良好的东西: SendInput

<啊REF = http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx> http://msdn.microsoft.com/en-us/ library / windows / desktop / ms646310%28v = vs.85%29.aspx [ ^ ]。



您可以使用P /这里已经为你做了一次调用: http://www.pinvoke.net/default.aspx/user32 .sendinput [ ^ ]。< br $>


请注意 keybd_event 已被 SendInput 取代。



至于 SendMessage ,它也可以使用,但有很多限制。首先,它只能用于窗口控件。所以,WPF是不可能的(注意你没有指出你想要使用的UI库,这是不好的;总是指出它以及你的应用程序类型是个好主意)。 SendInput 是首选,因为它模拟较低级别的输入,就好像它是由输入设备驱动程序调用的,因此它可以被普遍使用。



最后,我想警告不要使用输入模拟。在某些特定情况下,它可能很重要:播放键盘(鼠标)宏,开发测试UI的程序,类似的东西。如果您只是尝试将其用于UI开发,那将是一个很大的滥用。



-SA
Please use something which was proven to work well: SendInput:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

You do it using P/Invoke, which is already done for you, here: http://www.pinvoke.net/default.aspx/user32.sendinput[^].

Note that keybd_event has been superseded with SendInput.

As to SendMessage, it can also be used but has a number of limitations. First of all, it can be used on a windowed control only. So, WPF would be out of question (note that you did not indicate the UI library you want to use, which is bad; it' a good idea to always indicate it, as well as your application type). SendInput is preferable, because it simulates the input on lower level, as if it was called by the input device driver, so it can be used universally.

Finally, I would like to warn against the use of input simulation. It can be important in some specific cases: playing keyboard (mouse) macro, development of programs testing UI, something like that. If you simply try to use it for UI development, it would be a big abuse.

—SA

这篇关于如何在Vb.Net中使用Sendmessage()模拟Control + A键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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