Hi Ramon,
I have worked on this quite a bit, and I am missing something or there may be a bug - likely its me. The good news is that the code should not be complicated. I am able to get the code to move the Message Box in VBA to work well, but the program seems to crash in Xojo. Here is the working VBA code:
[code]Option Explicit
’ Import
Private Declare Function UnhookWindowsHookEx Lib “user32” _
(ByVal hHook As Long) As Long
Private Declare Function GetCurrentThreadId Lib “kernel32” () As Long
Private 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
Private 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
’ Handle to the Hook procedure
Private hHook As Long
’ Position
Private msgbox_x As Long
Private msgbox_y As Long
’ Hook type
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
’ SetWindowPos Flags
Private Const SWP_NOSIZE = &H1 ’ Retains the current size
Private Const SWP_NOZORDER = &H4 ’ Retains the current Z order
Sub TestMsgBox()
MsgBoxPos “Set non-Center Position”, _
vbOKOnly, _
“Message Box Hooking”, _
400, 300
End Sub
Public Sub MsgBoxPos(strPromt As String, _
vbButtons As VbMsgBoxStyle, _
strTitle As String, _
xPos As Long, _
yPos As Long)
' Store position
msgbox_x = xPos
msgbox_y = yPos
' Set Hook
hHook = SetWindowsHookEx(WH_CBT, _
AddressOf MsgBoxHookProc, _
0, _
GetCurrentThreadId)
' Run MessageBox
MsgBox strPromt, vbButtons, strTitle
End Sub
Private Function MsgBoxHookProc(ByVal lMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If lMsg = HCBT_ACTIVATE Then
’ Change position
SetWindowPos wParam, 0, msgbox_x, msgbox_y, _
0, 0, SWP_NOSIZE + SWP_NOZORDER
' Release the Hook
UnhookWindowsHookEx hHook
End If
MsgBoxHookProc = False
End Function
[/code]
Attached is the code for Xojo that crashes:
SetPosMsgBoxVer1
After checking the code, and looked at the Windows Functionality Suite, I found the following comment from Aaron:
[quote] // Well if this isn’t about as strange as you can get… I tried turning this into a
// Unicode-savvy function, but couldn’t make a go of it. Using the exact same
// code (only the W version of SetWindowsHookEx) causes a crash to occur
// immediately after the call returns. I wasn’t able to find any information about
// why the crash was happening, and it doesn’t make any sense (the Windows is
// a Unicode window, so there’s no mixed-type calls). Since this function doesn’t
// deal with strings, there’s no real benefit to making it Unicode-savvy, so I’m leaving
// the function as-is.[/quote]
It looks like something might not be quite right with SetWindowsHookExW, and for now I am using SetWindowsHookExA.
Just a friendly update