MessageDialog on another screen

[quote]MessageDialog.ShowModalWithin
MessageDialog.ShowModalWithin(Parent as Window) As MessageDialogButton
Displays the MessageDialog window as a sheet window (macOS only) for the passed Window.[/quote]

On Windows it seems that MessageDialog appears on Screen 0.
Is there a way to show MessageDialog in another Screen?

Well. It seems I was wrong.
MessageDialog window appears on the screen where active or default window (I’m not sure) belongs. It only appears on Screen 0 if there is no such window.
But anyway, it would be nice to be able to send MessageDialog window to the screen I want.

You can create a modal window with exactly the same size and icon as a MsgDialog, so you can place it on any screen you want.

[quote=350911:@Ramon SASTRE]MessageDialog window appears on the screen where active or default window (I’m not sure) belongs. It only appears on Screen 0 if there is no such window.
But anyway, it would be nice to be able to send MessageDialog window to the screen I want.[/quote]
I would guess it’s appearing on the screen the user has set as their “main screen” when there is no other window.

[quote=350874:@Ramon SASTRE]On Windows it seems that MessageDialog appears on Screen 0.
Is there a way to show MessageDialog in another Screen?[/quote]

There is a way to do it with Windows, and you will need to use some declares. Here is a great article on how to do it, and what all of the descriptions mean.

WIndows Monitor

Sorry, I haven’t had a chance to create the code yet - and its on my to-do list :slight_smile:

I hope this helps.

Thanks Michel, Tim and Eugene for your comments.
I think I have now a better idea how to focus this subject.

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 :slight_smile:

Found a little more information about the error using WinDbg, and it looks like a hook causes an Access Violation:

hInst: 1598029824, hHook: 4917961, mThread: (4d8.1f0): Access violation - code c0000005 (first chance)

The thread is attempting to access the GUI Message Box, which Xojo does not like. It appears that the SetWindowsHookExA command attempts to use the thread and instance (hInst and GetCurrentThreadID) which Xojo does not like. I moved thread and instance access code to a timer, and the same Access Violation error occurs. My guess (thats all it is) is to attempt to bypass the thread accessing gui code for Xojo. Does anyone know of a way to do this?