Results 1 to 9 of 9

Thread: Position of Message boxes

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Question

    HI.

    Is it possible to change the position that a Message Box is displayed to the center of an MDI child form rather than the default position, which seems to be the center of the screen?

    If my child form only takes up half of the parent form, I would like my message boxes to be displayed centered on the child form.

    Any ideas anyone?

    Thanks.

  2. #2
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    Maybe using some api, maybe one of the MessageBox API's (MessageBox,MessageBoxEx,MessageBoxIndirect)

    Otherwise you might have to use FindWindow, then use SetWindowPos?
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    I haven't played with this but it is supposed to do the trick on forms...Midi ??? maybe it will help.
    Code:
    'position the message box by screen position
    
    '  <<<<<<  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
    Else
        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
    local_error:
    ' 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, _
                             SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
                '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
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    What HeSaidJoe gave you works. MS' example at
    http://support.microsoft.com/support.../Q180/9/36.ASP
    also works great. But for all the coding and the hook employed, I think personally I'll opt for creating my own messagebox. Unless maybe this were made into a dll.
    Wade

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Hmm...possibilities. I'll knock one up in C++ if anyone needs one without extreme coding.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Thanks.

    HeSaidJoe for the code. WadeD, thanks for the link, which seems similar to HeSaidJoe's example.

    WadeD, I think you're right. For all the trouble, it would be easier to just create my own message box. So, Parksie, hold off on using your time to create that DLL, although it may be useful to have it at some point in the future. The amount that I'm getting for this project doesn't justify me spending any more time on this message box issue.

    Thanks again. All the best.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What time? It'll only take about 5 minutes to make.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>


    You are welcome.

    I agree and that's why I hadn't played with that lump of code I acquired somewhere along the way. It's a lot easier to walk around it.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Here ya go: http://www.parksie.uklinux.net/customwin.zip

    There's full source code and an example project (with definition).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width