要求:
在这个程序运行时,把它最小化到后台,当别的程序在前台运行时,按键盘的“F2”,能使程序的Text1里的内容直接粘贴到当前程序的文本框里;按键盘的“F3”,能使程序的Text2里的内容直接粘贴到当前程序的文本框里;按键盘的“F4”,能使程序的Text3里的内容直接粘贴到当前程序的文本框里.
比如在文本框里输入111111
运行记事本,按“F2”直接粘贴出111111
以下代码,只可以在窗口里有一个text时,可以实现这个功能,可是现在需要有三个text,对应三个热键F2 F3 F4都可以使用,如何更改啊,大家帮我看看!
窗体的代码:
Option Explicit
Private Sub Form_Load()
Dim ret As Long
'记录原来的window程序地址
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
'用自定义程序代替原来的window程序
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf Wndproc)
idHotKey = 1
uVirtKey = vbKeyF2
ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
'取消Message的截取,使之送往原来的windows程序
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hwnd, uVirtKey)
End Sub
Private Sub Text1_Change()
End Sub
模块1的代码:
Option Explicit
Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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
Declare Function RegisterHotKey Lib "User32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "User32" (ByVal hwnd As Long, ByVal id As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, uVirtKey1 As Long, idHotKey As Long
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hWord As Integer
End Type
Public Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim KeyStr As String
KeyStr = frmMain.Text1.Text
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then
SendKeys KeyStr
End If
End If
End If
'如果不是热键信息则调用原来的程序
Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
[此贴子已经被作者于2005-11-26 23:42:22编辑过]