471,594 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,594 developers and data experts.

How To Handle More events Visual Basic 6.0 Not Support

112 100+
Hi Every one...

With this visual Basic 6.0 Code you can handle more event that visual basic Support as Mouse wheel and hover or you can control event before VB IDE Default Windows proc as WM_CREATE when windows start creation, this task is useful for some application , for example you can create new UI Control at run time using Form1.Control.add("vb.CommandButton","Cmd1") Function and you can handle command button event by WM_COMMAND Message and Command ID


Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  3. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  4. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  5. Private 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
  6. Private Declare Sub CopyPtrToObj Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Object, ByRef Source As Long, Optional ByVal Length As Long = 4)
  7. Private Declare Sub CopyObjToPtr Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Long, ByRef Source As Object, Optional ByVal Length As Long = 4)
  10. Private Const GWL_USERDATA = (-21)
  11. Private Const GWL_WNDPROC = (-4)
  14. Private Const WM_MOUSEWHEEL As Long = &H20A
  15. Private Const WM_MOUSEHOVER As Long = &H21A
  17. Public Sub ChangeWindowProc(ByVal WindowObject As Object)
  18. Dim LastFormProc As Long
  19. Dim WindowObjectPointer As Long
  20.     'Get Proc Address assigned by VB IDE
  21.     LastFormProc = GetWindowLong(WindowObject.hwnd, GWL_WNDPROC)
  22.     Call CopyObjToPtr(WindowObjectPointer, WindowObject)        'Copy Object memory Pointer to Long variable
  23.     WindowObject.Tag = LastFormProc                             'hold lastProc in tag property u can create public variable in each window and assign this value to it and use tag for ur work
  24.     'Save Last Window Object Pointer in Window Class User Extedned Data,now u can change proc for multiple form at one time
  25.     'sure user cant active 2 forms in same time, but because of timer event and winsock
  26.     Call SetWindowLong(WindowObject.hwnd, GWL_USERDATA, WindowObjectPointer)
  27.     'change VB IDE proc by new our Proc Address
  28.     Call SetWindowLong(WindowObject.hwnd, GWL_WNDPROC, AddressOf WindowExtendedEventProc)
  29. End Sub
  31. 'Reset Window Proc To Orignal VB IDE Proc.
  32. Public Sub ResetWindowProc(ByVal WindowObject As Object)
  33. Dim LastFormProc As Long
  34.     LastFormProc = Val(WindowObject.Tag)
  35.     Call SetWindowLong(WindowObject.hwnd, GWL_USERDATA, LastFormProc)
  36. End Sub
  38. 'Our New Window Defined proc
  39. Public Function WindowExtendedEventProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  40. 'this variable is static because visual basic will unallocate all objects and variables within function after excution complete
  41. 'try to change it to Dim FormObject as object your application will crash
  42. Static FormObject As Object
  43. Dim LastFormProc As Long
  44. Dim FormObjectPointer As Long
  45.     'get Object Pointer Again in long var
  46.     FormObjectPointer = GetWindowLong(hwnd, GWL_USERDATA)
  47.     Call CopyPtrToObj(FormObject, FormObjectPointer)    'this line seems as "Set FormObject=Form1" !!!!
  48.     LastFormProc = Val(FormObject.Tag)                  'retrieve LastProc Address From tag
  49.     Select Case Msg
  50.         Case WM_MOUSEWHEEL                  'if user Roll Mouse Wheel
  51.             Call FormObject.Form_MouseWheel
  52.             WindowExtendedEventProc = 0
  53.             Exit Function
  54.         Case WM_MOUSEHOVER
  55.             Call FormObject.Form_MouseWheel
  56.             WindowExtendedEventProc = 0
  57.             Exit Function
  58.     End Select
  59.     'call orignal VB IDE Proc with other windows messages
  60.     WindowExtendedEventProc = CallWindowProc(LastFormProc, hwnd, Msg, wParam, lParam)
  61. End Function
'================================================= =======

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  3. Private Sub Form_Activate()
  4.    Call ChangeWindowProc(Me)
  5. End Sub
  7. Private Sub Form_Deactivate()
  8.    Call ResetWindowProc(Me)
  9. End Sub
  11. Public Sub Form_MouseWheel()
  12.     MsgBox "mouse Wheel"
  13. End Sub
  15. Public Sub Form_MouseHover()
  16.     MsgBox "mouse Wheel"
  17. End Sub
Good Luck
Nov 16 '07 #1
0 6647

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by askingBoy | last post: by
26 posts views Thread by Bruno Jouhier [MVP] | last post: by
3 posts views Thread by Assaf | last post: by
97 posts views Thread by Master Programmer | last post: by
4 posts views Thread by Goran Djuranovic | last post: by
3 posts views Thread by =?Utf-8?B?Rmxhc2hwcm8=?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.