• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:


Can someone point me to some source code that involves hooks
like using a hook on the shell or something
  • 2
1 Solution
'position the message box by screen position
'hook the mouse

'  <<<<<<  bas module code

Option Explicit

Public Type RECT
         Left As Long
         Top As Long
         Right As Long
         Bottom As Long
End Type

Public Type HookParms
        WindowOwner As Long
        xPos As Long
        yPos As Long
        hHook As Long
End Type

Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
    ByVal hmod As Long, ByVal dwThreadId As Long) As Long

Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
    ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
    ByVal cy As Long, ByVal wFlags As Long) As Long

Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Public Const GWL_HINSTANCE = (-6)
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOACTIVATE = &H10
Public Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5
Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

Public Const MB_APPLMODAL = &H0&
Public Const MB_SYSTEMMODAL = &H1000&
Public Const MB_TASKMODAL = &H2000&

Public MessParms As HookParms  '# set up the MessParms as a public type of type HookParms

Public Function MsgBoxSpc(ByVal sCaption As String, Optional ByVal lParms As VbMsgBoxStyle = vbInformation, _
    Optional ByVal sTitle As Variant = "Timneys Tools", Optional ByVal lOwner As Variant, _
    Optional ByVal bPositionBox As Boolean = False, Optional ByVal xVal As Long = 0, Optional ByVal yVal As Long = 0) As Long

On Error GoTo local_error

If Len(Trim(sCaption)) = 0 Then '# determine if caption was passed
    Exit Function
End If

If IsMissing(lOwner) Then
    lOwner = "&h" & Hex(0) '# convert to hex
    lOwner = lOwner.hwnd '# if it cant be given a window handle, then its already a window handle
End If

lParms = "&h" & (Hex(lParms))

'# and now determine where the message box should be positioned

Dim hInst As Long
Dim Thread As Long
         'Set up the CBT hook
hInst = GetWindowLong(lOwner, GWL_HINSTANCE)
Thread = GetCurrentThreadId()

    '# set up the hookparms type values
    '# this is used by the hooker to
    '# intercept the windows messages
    '# and then activate the hook against the function HookProc
    MessParms.WindowOwner = lOwner
    MessParms.xPos = IIf(bPositionBox = True, xVal, 0)
    MessParms.yPos = IIf(bPositionBox = True, yVal, 0)
    MessParms.hHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, hInst, Thread) '# create the hook
    '# end hookparms set

    MsgBoxSpc = MessageBox(lOwner, sCaption, sTitle, lParms + MB_TASKMODAL) '# invoke API with specific owner, and modal state

Exit Function
' the caller passed a window handle in - oops
' oh well, lets use it anyway as a parent
Resume Next

End Function

Private Function HookProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

      Dim rectForm As RECT, rectMsg As RECT
      Dim x As Long, y As Long

         Debug.Print "hooked in at " & Time
         'On HCBT_ACTIVATE, show the MsgBox centered over Form1
         If lMsg = HCBT_ACTIVATE Then
            'Get the coordinates of the form and the message box so that
            'you can determine where the center of the form is located
            '# get the message box hookparms values, this is the only safe
            '# way to pass things to a windows proc while hooked
            '# evaluate the type values
            '# re-evaluate the owner of the message - must have a parent
            MessParms.WindowOwner = IIf(MessParms.WindowOwner = 0, GetDesktopWindow, MessParms.WindowOwner)
            GetWindowRect MessParms.WindowOwner, rectForm
            GetWindowRect wParam, rectMsg
            x = IIf(Not MessParms.xPos = 0, MessParms.xPos, rectForm.Left + (rectForm.Right - rectForm.Left) / 2) - _
                ((rectMsg.Right - rectMsg.Left) / 2)
            y = IIf(Not MessParms.yPos = 0, MessParms.yPos, rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2) - _
                ((rectMsg.Bottom - rectMsg.Top) / 2)
            'Position the msgbox
            SetWindowPos wParam, 0, x, y, 0, 0, _
            'Release the CBT hook
            UnhookWindowsHookEx MessParms.hHook
            Debug.Print "hooked out at " & Time
         End If
         HookProc = False

End Function

'  <<<<<<     event call

 Private Sub Command1_Click()

'WARNING.. when you change the message you change the size of the msgbox and
'so the position needs to be adjusted...go slowly..if you increment too large
'a number you will position yourself off the screen and hang...the old alt/ctrl/del
'gets you out eventually but it shuts VB down...I'm sure you've been there.
'values to feed the function are Message,,Title,FormName,True,Top,Bottom
'value ,,    optional ..see function...but the ,, have to be there (byval)
'call messagebox to 4 corners and center

'bottom left...title bar =This is my Special Box"
 MsgBoxSpc "Bottom Left!", , "This is my Special Box", Me, True, 80, 520

'top left..new title
 MsgBoxSpc "Top Left", , "A New Title", Me, True, 60, 60

'top right..Another new title
 MsgBoxSpc "Top Right", , "Another New Title", Me, True, 724, 60

'bottom right
 MsgBoxSpc "Bottom Right", , "Yet Another", Me, True, 730, 520

'default screen position and default title
 MsgBoxSpc "Default"

End Sub
TCPIP2600Author Commented:
i was just looking for a simple hook example. What does all this do? besides move a little message box around.... i fugred that much out
that is a hook...
stop the normal procedure of something and replace it with your own.

As you are aware, the messge box is always defaulted to center screen...this hook stops the default and alows you to put it where you want.

That my friend is an example of hooking...not the same as hooking on the street but hooking none the same.

Have a good one,

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now