vb.net - 从 vb.net 中的窗体外部获取突出显示的文本

标签 vb.net winforms text highlight

我想使用 vb.net 从 winform 外部复制突出显示的文本。 例如,如果用户在浏览器或记事本中突出显示文本,则应将其复制到 winform 中的文本框中。 任何帮助都会很棒! 提前致谢。

最佳答案

好的! 感谢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

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

Main Form

'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

关于vb.net - 从 vb.net 中的窗体外部获取突出显示的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62542499/

相关文章:

vb.net - msstdfmt.dll 1.0.0 缺失错误

html - 防止在文本元素上覆盖半透明背景

java - 能够查看每个类(class)的文本

c# - FtpWebRequest 移动文件

c# - 在特定位置插入 XML 节点

c# - 用大写字母和粗体格式化 RichTextBox 的第一行

vb.net - 线程可靠性: log file shows function that I call a lot did not reach return statement randomly

C# Task Parallel Library 第一次慢

winforms - 为什么在我的 Visual Studio 中所有类文件都使用 .vb 扩展名创建,而不是作为 .cs 文件创建?我需要 .cs 文件

ios - 子类化 UIFont