Tulin
August 6th, 2001, 12:34 AM
My requirement is to detect Mouse and Keyboard events on a command button in VB6. But there should not be any code written in the form/control. It should be written as a dll which will detect the event and fire the required action. The form which contains the control should remain free of code. If anyone has any input for this ,kindly reply . A sample code will be appreciated.
Clearcode
August 6th, 2001, 04:21 AM
This sounds like a task for subclassing.
What you need to do is intercept the mouse and keyboard messages before the command button processes them. To do this you need to tell the button to use your function rather than its own for each message it recieves.
This is quite an advanced thing to do so if you want a shortcut, download the EventVB.dll from http://www.merrioncomputing.com/Download/index.htm and then create your DLL with a single main class:
'clsMain declarations
private withevents ApiLink as EventVB.ApiFunctions
private withevents ApiWnd as EventVB.ApiWindow
'class initialise....
private Sub Class_Initialize()
set ApiLink = new EventVB.ApiFunctions
set ApiWnd = new EventVB.ApiWindow
End Sub
private Sub Class_Terminate()
set ApiWnd = nothing
set ApiLink = nothing
End Sub
This will then create several new events in your main class - of which two are of interest:
private Sub ApiLink_ApiError(byval Number as Long, byval Source as string, byval Description as string)
End Sub
which is triggered whenever an error occurs within the API subsystem and
private Sub ApiWnd_WindowMessageFired(byval msg as WindowMessages, byval wParam as Long, byval lParam as Long, Cancel as Boolean, ProcRet as Long)
End Sub
which is triggered whenever a window message is processed. It is here that you would put whatever code you want to occur on the keyboard/mouse presses.
The last thing is to provide a mechanism to tell the subclassing control which window to intercept thus:
public property let hwndTarget(byval hwndTargetIn as long)
ApiWnd.hwnd = hwndTargetIn
ApiLink.SubclassedWindows.Add ApiWnd
End property
Then compile this up into a dll.
To use it instantiate it and then set the command button to be subclassed thus:
Dim foo as new Mydll.clsmain
foo.hwndTarget = Form1.Command1.hwnd
Hope this helps,
Duncan
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.