Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grabbing The Copy Shortcut text out side or vb? 2

Status
Not open for further replies.
Jun 15, 2003
8
US
I would like to know if there is a way to grab a users activity outside of the vb app (exe) and use it in the app.

Basicly: (If my program is running) if the user right-clicks on a link on a webpage and selects "copy shortcut" My vb program will paste the link into a text box. If the text box is already filled with data it will fill the next text box down up to 20 text boxes?

Thank you for any help

 
You need to register your application's window as a clipboard wiewer window. This will insert you window in the change of clipboard viewers' chain and Windows will send messages to your window whenever the clipboard's contents are changed by any application. After examining the contents of the clipboard, your application will extract a (possible) shortcut or url and will use it anywhere in the program...

See how the following example works.
1. Start a new project and add a listbox (List1) to your form. Also set the IntegralHeight property of the listbox to False.
2. Paste the following code in Forms code window.
___
'Form's code
Private Sub Form_Load()
RegisterClipboardViewer hWnd
End Sub

Private Sub Form_Resize()
List1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnRegisterClipboardViewer
End Sub
___
3. Add a standard code module and paste the following code in it.
___
'Module's code
Option Explicit
Const WM_CHANGECBCHAIN = &H30D
Const WM_DRAWCLIPBOARD = &H308
Const GWL_WNDPROC = (-4)
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function ChangeClipboardChain Lib "user32" (ByVal hWnd As Long, ByVal hWndNext As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function SetClipboardViewer Lib "user32" (ByVal hWnd As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Dim hWndThis As Long, hWndNext As Long, lppwp As Long, strLast As String
Sub RegisterClipboardViewer(hWnd As Long)
If hWndThis = 0 Then
hWndThis = hWnd
hWndNext = SetClipboardViewer(hWndThis)
lppwp = SetWindowLong(hWndThis, GWL_WNDPROC, AddressOf WndProc)
End If
End Sub

Sub UnRegisterClipboardViewer()
If hWndThis <> 0 Then
SetWindowLong hWndThis, GWL_WNDPROC, lppwp
ChangeClipboardChain hWndThis, hWndNext
hWndThis = 0
lppwp = 0
End If
End Sub

Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case WM_CHANGECBCHAIN
If hWndNext = wParam Then
hWndNext = lParam
Else
SendMessage hWndNext, uMsg, wParam, ByVal lParam
End If
Case WM_DRAWCLIPBOARD
ClipboardChanged
SendMessage hWndNext, uMsg, wParam, ByVal lParam
End Select
WndProc = CallWindowProc(lppwp, hWnd, uMsg, wParam, lParam)
End Function

Sub ClipboardChanged()
Dim S As String
S = Clipboard.GetText
If S = vbNullString Then Exit Sub
If strLast = S Then Exit Sub
If InStr(S, &quot; <> 1 And InStr(S, &quot;ftp://&quot;) <> 1 Then Exit Sub
'Don't forget to remove the semi-colons(;)
'from the above line. They are added by tek-tips.
strLast = S
Form1.List1.AddItem Time$ & &quot;> &quot; & S, 0
End Sub
___
4. Run the program. Open a web page and copy a link. It will be added to the list box.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top