简体   繁体   English

VB.NET如何在其他窗口的光标位置插入文本?

[英]VB.NET How to insert text at cursor position in a different window?

I have a small application that displays a listbox under the cursor position when the user uses a shortcut key. 我有一个小应用程序,当用户使用快捷键时,它会在光标位置下显示一个列表框。

When the user double clicks a selection from the listbox I want to insert that selected text at the curser position of that opened window. 当用户双击列表框中的选择时,我要将所选的文本插入到打开的窗口的光标位置。

Example: user has microsoft word open. 示例:用户打开了Microsoft单词。 He/she uses a shortcut key that displays a listbox just under the cursor position. 他/她使用快捷键在光标位置的下方显示一个列表框。 The listbox has a collection of text. 列表框具有文本集合。 When the user double clicks a selection that selected text is inserted at the cursor position. 当用户双击所选内容时,所选文本将插入到光标位置。

I tried the following: 我尝试了以下方法:

Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Text.Insert(Cursor.Position, ListBox1.SelectedItem)
End Sub

But that doesn't work. 但这是行不通的。

Any help will be sincerely appreciated. 任何帮助将由衷的感谢。

The best (most generic) approaches will be to trick the application into thinking you have entered some text. 最好(最通用)的方法是欺骗应用程序,使您认为您已经输入了一些文本。 For example: 例如:

  • Send keypress windows messages for all of the characters you wish to "type" to the target window (eg with WM_KEYDOWN or WM_CHAR type messages. Some experimentaiton may be needed to find the approach that works best). 将您希望“键入”的所有字符的按键式窗口消息发送到目标窗口(例如,使用WM_KEYDOWN或WM_CHAR类型的消息。可能需要做一些实验才能找到最有效的方法)。

  • Copy the text onto the clipboard and send a single ctrl+V keypress message to the application. 将文本复制到剪贴板上,然后向应用程序发送一条ctrl + V按键消息。 (This will overrite the clipboard and may not work in apps that don't support that key shortcut though) (这将覆盖剪贴板,但可能无法在不支持该快捷键的应用中使用)

If you know the specfic application (eg MS Word) then you may be able to use application-specific automation (OLE, etc) interfaces to insert text. 如果您知道特定的应用程序(例如MS Word),则可以使用特定于应用程序的自动化(OLE等)界面来插入文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM