从vb.net表单外部获取高亮文本

3
我希望能够使用vb.net从winform之外复制高亮文本。 例如,如果用户在浏览器或记事本中突出显示文本,则应将其复制到winform中的文本框中。 非常感谢任何帮助! 提前致谢。

1
似乎你想要一个通用的选择=复制功能。我不确定是否有一种好的基于事件的方式来获取文本值。因此,我建议尝试使用轮询的方法:每0.1秒发送一次 ClipBoard.Copy(),检查它自上次以来是否发生了变化,如果变化了,则在表单中显示更改,否则将剪贴板重置为旧值。 - Fixation
这个问题的C#等价版本是:https://dev59.com/PGEi5IYBdhLWcg3wPKax - Fixation
1
你可以使用类似这样的代码:当外部应用程序的窗口移动时移动窗口。现在,该代码钩取了一个特定的窗口句柄,但是你当然可以将其更改为钩取前景窗口(接收鼠标输入的活动窗口)。事件必须设置为EVENT_OBJECT_TEXTSELECTIONCHANGED,而该代码当然使用的是EVENT_OBJECT_LOCATIONCHANGE。请注意,你只需要钩取主窗口。文本选择更改通知适用于具有通知文本选择更改的自动化TextPattern的任何子窗口。 - Jimi
1
@Fixation 所有这些代码都假设子窗口有一个句柄。这仅适用于Win32/WinForms控件。WPF/UWP无窗口控件没有句柄。此外,需要不断轮询,这意味着用户活动可能会因此而受到干扰。 - Jimi
1
您还需要删除 ... && idObject == (Hook.SWEH_ObjectId)Hook.SWEH_CHILDID_SELF 条件,因为它是为了防止子对象通知激活相同的过程而添加的,而现在您希望它们被激活。 - Jimi
@Fixation 感谢您的建议,它帮助我构建了逻辑。 - Uniquedesign
1个回答

1

好的!感谢this链接,我得到了答案。

我的逻辑是首先用户将突出显示一个值,不限于浏览器,而是任何地方。 然后用户将按下热键,在我的情况下是F8。然后代码将触发复制命令,然后检索剪贴板值并将其分配给文本框文本。 以下是完整的代码和帮助类。

Hotkey.vb

 Public Class Hotkey

#Region "Declarations - WinAPI, Hotkey constant and Modifier Enum"
    ''' <summary>
    ''' Declaration of winAPI function wrappers. The winAPI functions are used to register / unregister a hotkey
    ''' </summary>
    Private Declare Function RegisterHotKey Lib "user32" _
    (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer

    Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

    Public Const WM_HOTKEY As Integer = &H312

    Enum KeyModifier
        None = 0
        Alt = &H1
        Control = &H2
        Shift = &H4
        Winkey = &H8
    End Enum 'This enum is just to make it easier to call the registerHotKey function: The modifier integer codes are replaced by a friendly "Alt","Shift" etc.
#End Region


#Region "Hotkey registration, unregistration and handling"
    Public Shared Sub registerHotkey(ByRef sourceForm As Form, ByVal triggerKey As String, ByVal modifier As KeyModifier)
        RegisterHotKey(sourceForm.Handle, 1, modifier, &H77)
    End Sub
    Public Shared Sub unregisterHotkeys(ByRef sourceForm As Form)
        UnregisterHotKey(sourceForm.Handle, 1)  'Remember to call unregisterHotkeys() when closing your application.
    End Sub
    Public Shared Sub handleHotKeyEvent(ByVal hotkeyID As IntPtr)
        SendKeys.Send("^(c)") 'for Ctrl-C[/CODE]
    End Sub
#End Region

End Class

这部分将触发热键代码并执行其余逻辑。

主表单

'This Program will wait for a key to press in our case it will wait for the F8 key to be press
'Then it will copy the highlighted text (Outside and Inside of the form) and will concatinate the text with a webaddress'

Imports Name_Of_Your_Project.Hotkey
Public Class Form1

    
    'This Chunk of code will register the F8 key as a main key for the Program'
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Hotkey.registerHotkey(Me, "f8", Hotkey.KeyModifier.None)
    End Sub
    'This sub will trigger the Hotkey Sub Code in the Hotkey.vb Class'

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = Hotkey.WM_HOTKEY Then
            Hotkey.handleHotKeyEvent(m.WParam)

            'After pressing the F8 key It will copy the highlighted data from anywhere and store it to the clipboard'
            If Clipboard.ContainsText Then
               
                Try
                   
                    Textbox1.text = My.Computer.Clipboard.GetData(DataFormats.Text).ToString
                Catch ex As Exception
                    MessageBox.Show("Error in Program" + ex.ToString())
                End Try
            End If
        End If
            MyBase.WndProc(m)



    End Sub
    'System wide hotkey event handling




End Class

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接