I want to copy the highlighted text from outside of winform using vb.net.
For example, if the user highlights a text in a browser or in notepad it should be copied into a textbox in a winform.
Any help would be great!
Thanks in advance.
Ok!
Thanks to this link I got my answer.
My Logic is First of all the user will highlight a value, not specific to browser but anywhere.
then the user will press the Hotkey, In my case it's F8. Then Code will trigger the copy command and then retrieve the Clipboard value and will assign it to a textbox text.
Here is the Full Code along with Helping Class.
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 Section will Trigger the Hotkey Code and do the rest of logic
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
Related
I have made a custom task pane using a user control.
Every time I resize it I raise a method called SuspendDrawing the reason I do this is because the buttons on my custom task pane flicker when I resize it therefore I need to suspend it. I add these buttons using the designer
This works like a treat but then I need to call a method called ResumeDrawing when the user stops resizing however I don't know what event I should use to call the ResumeDrawing method as there is no ResizeEnd event for a user control.
Any suggestions would be appreciated as I'm out of ideas.
My code is as follows
Public Property Resizing As Boolean
<DllImport("user32.dll", EntryPoint:="SendMessageA", ExactSpelling:=True, CharSet:=CharSet.Ansi, SetLastError:=True)>
Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
Public Shared Sub SuspendDrawing(ByVal target As Control)
SendMessage(target.Handle, WM_SETREDRAW, 0, 0)
End Sub
Public Shared Sub ResumeDrawing(ByVal target As Control)
ResumeDrawing(target, True)
End Sub
Public Shared Sub ResumeDrawing(ByVal target As Control, ByVal redraw As Boolean)
SendMessage(target.Handle, WM_SETREDRAW, 1, 0)
If redraw Then
target.Refresh()
End If
End Sub
Private Sub SideBarPowerPoint_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
SuspendDrawing(Me)
End Sub
My Custom Task Pane
Dim taskPaneView = New PowerPointCommon.SideBarPowerPoint
sideBarTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(taskPaneView, " ")
If sideBarTaskPane.Visible = True Then
sideBarTaskPane.Visible = False
End If
sideBarTaskPane.Visible = True
sideBarTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight
sideBarTaskPane.Width = 100
I'm creating a program to manage a local network but Im having a little problem.
When the form starts, it starts hidden.
I used this code to hide it:
Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
If Not Me.IsHandleCreated Then
Me.CreateHandle()
value = False
End If
MyBase.SetVisibleCore(value)
End Sub
What I want exactly is to show the form when I click ALT+X for example.
Use a Global Hotkey. That way you don't have to worry about your Application having focus. A good example for VB.Net is here:
http://www.kirsbo.com/How_to_add_global_hotkeys_to_applications_in_VB.NET
To summarise. Define a HotKey class:
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>
Public Declare Function RegisterHotKey Lib "user32" _
(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public 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, Asc(triggerKey.ToUpper))
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)
MsgBox("The hotkey was pressed")
End Sub
#End Region
End Class
Then add the following Sub to your main form:
'System wide hotkey event handling
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = Hotkey.WM_HOTKEY Then
Hotkey.handleHotKeyEvent(m.WParam)
End If
MyBase.WndProc(m)
End Sub
Then register a HotKey in the startup code for your form:
Hotkey.RegisterHotKey(Me, "X", Hotkey.KeyModifier.Alt)
The first parameter is the form, the second is the key to be handled and the third is the modifier key. Once this Hotkey is registered it will trigger code in HotKey.handleHotKeyEvent.
You can then use some sort of Callback to trigger a method in the calling form itself if you so desire.
I would like to know how to capture right click and paste option through mouse click.
It is a winforms application. I would be modifying the contents of clipboard before pasting.
I am able to perform this through ctrl+V but not able to find a way to handle mouse right click.
I have tried this so far:
Private Const WM_PASTE As Integer = &H302
Protected Overrides Sub WndProc(ByRef msg As Message)
If msg.Msg = WM_PASTE AndAlso Clipboard.ContainsText() Then
Clipboard.SetText(Clipboard.GetText().Replace(vbCrLf, " "))
End If
MyBase.WndProc(msg)
End Sub
You have to process the WM_PASTE windows message using WndProc (a list of all messages can be found here).
For example, this TextBox will print all text pasted into it (no matter how) to the console instead of displaying it itself:
Class CapturePasteBox
Inherits TextBox
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = &H302 AndAlso Clipboard.ContainsText() Then
Dim text = Clipboard.GetText()
'' do something with text
Console.WriteLine(text)
Return '' return so the text won't be pasted into the TextBox
End If
MyBase.WndProc(m)
End Sub
End Class
In response to your comment:
The ComboBox-control needs some special treatment, since
When sent to a combo box, the WM_PASTE message is handled by its edit control.
So you can use the following function/class using a NativeWindow:
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError := True)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
Public Class PasteHandler
Inherits NativeWindow
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = &H302 Then
Clipboard.SetText(ClipBoard.GetText().Replace("e", "XXX"))
End If
MyBase.WndProc(m)
End Sub
End Class
and use it with your ComboBox:
'' Get the edit control of the combobox
Dim lhWnd As IntPtr = FindWindowEx(yourComboBox.Handle, IntPtr.Zero, "EDIT", Nothing)
'' assign the edit control to the Pastehandler
Dim p As New PasteHandler()
p.AssignHandle(lhWnd)
I found this to be working like awesome:
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
Private Sub SearchCriteria_MouseDown(sender As Object, e As MouseEventArgs) Handles SearchCriteria.MouseDown
Dim lhWnd As IntPtr = FindWindowEx(SearchCriteria.Handle, IntPtr.Zero, "EDIT", Nothing)
If e.Button = Windows.Forms.MouseButtons.Right And lhWnd <> 0 Then
Clipboard.SetText(Clipboard.GetText().Replace(vbCrLf, " "))
End If
End Sub
I am making a program in VB.NET and need to hide the Caret in textboxes.
I could live with it being either completely hidden or just the same colour as the textboxes background colour. How can I go about doing this? I would prefer to stay away from Custom Controls if at all possible.
Thank you
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
Public Class NoCaretBox
Inherits TextBox
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
HideCaret(Me.Handle)
MyBase.OnGotFocus(e)
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
ShowCaret(Me.Handle)
MyBase.OnLostFocus(e)
End Sub
Private Declare Function HideCaret Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function ShowCaret Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
End Class
I asked a question earlier about keyhooks in vb.net.
My current issue is such:
I have created a program which should perform a certain action whenever a certain group of keys is pressed at the same time. The program must be able to run in the background, or in the system tray or something. Essentially, this should work like the KeyDown event on a form, except the form in this case is everything.
I'm not certain if there is a way to do this directly from within the .net API, but if there is I certainly have not found it.
That doesn't require a keyboard hook, you'll want to register a hot key. Much easier to implement and much less demanding of system resources. Here's an example, it restores the form to the foreground if it was minimized. Note that you can register more than one key:
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Public Class Form1
Private Const cHotKeyId As Integer = 0
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
'--- Register Ctrl + Shift + U as a hot key
If Not RegisterHotKey(Me.Handle, cHotKeyId, MOD_CONTROL + MOD_SHIFT, Keys.U) Then
Throw New Win32Exception()
End If
MyBase.OnLoad(e)
End Sub
Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
UnregisterHotKey(Me.Handle, cHotKeyId)
MyBase.OnFormClosing(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Console.WriteLine(m.ToString())
If (m.Msg = WM_HOTKEY AndAlso m.WParam = CType(cHotKeyId, IntPtr)) Then
Me.Visible = True
If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal
SetForegroundWindow(Me.Handle)
End If
MyBase.WndProc(m)
End Sub
'--- P/Invoke declarations
Private Const WM_HOTKEY As Integer = &H312
Private Const MOD_ALT As Integer = &H1
Private Const MOD_CONTROL As Integer = &H2
Private Const MOD_SHIFT As Integer = &H4
Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vk As Integer) As Boolean
Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
End Class