[RESOLVED] Position MsgBox-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] Position MsgBox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    356

    Resolved [RESOLVED] Position MsgBox

    Hi

    I downloaded some code so that I can position my MsgBox's and it works fine
    but in there example a command button is used I want it to fire when any of the
    MsgBox's are shown in my project.
    Can someone help me with this please.

    Code:
     Private Sub Command2_Click()
          Dim hInst As Long
          Dim Thread As Long
    
             'Set up the CBT hook
             hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
             Thread = GetCurrentThreadId()
             hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, _
                                      Thread)
    
             'Display the message box
             MsgBox "This message box is centered over Form1."
          End Sub

  2. #2
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: Position MsgBox

    You should be able to overload the MsgBox function by putting the following function in a BAS module.

    Code:
    Public Function MsgBox(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, [Title], [HelpFile], [Context]) As VbMsgBoxResult
    
             Dim hInst As Long
             Dim Thread As Long
    
             'Set up the CBT hook
             hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
             Thread = GetCurrentThreadId()
             hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, _
                                      Thread)
    
             'Display the message box
             MsgBox = VBA.MsgBox Prompt, Buttons, Title, HelpFile, Context
    End Function
    This in and of itself won't compile: You're going to need to correctly define Buttons, Title, HelpFile and Context (or leave them out entirely if you wish). Furthermore, you're going to need to figure out what to do about Me.hwnd, which has no context in a BAS module: you can either pass in a value to the function or if the "Me" always refers to the same form, just place the use the form's name instead (ie GetWindowLong(MDIMain.hwnd, ...).

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Position MsgBox

    In addition you may also need to modify the WinProc2 procedure to ensure that there is no form name hard coded.

    Instead of all of the above I would design my own message box form and activate it from anywhere in your project by passing few parameters (perhaps, parent form, title, message, postion, etc...).
    In such way there will be no subclassing and you will have better control over what and how is displayed.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    356

    Re: Position MsgBox

    Hi RhinoBull

    I think the hook method could cause me more problems than it solves so I decided to go down the route you suggested have a form as a message box.

    I have found an excellent example posted to this forum by LongWolf the code was written by John R Parrish I have figured out how all the code works except
    one how to move the msgbox(form) to my position.
    Can you help with that please?
    The code below is the option I want to use.
    Code:
    frmMsgBox.MsgCstm "Rocket Rons Test", "IP Control", mbInformation, 2, False, _
                         "Yes", "No", "Exit"

  5. #5

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    356

    Re: Position MsgBox

    Hi RhinoBull
    All original code here was written by John R Parrish

    I think I can insert the X/Y params to the MsgCstm Function if my example below is correct

    Code:
    Public Function MsgCstm(ByRef Promt As String, _
                            ByRef Title As String, _
                            ByRef MsgIcon As eMsgIcon, _
                            ByRef DefaultBtn As Long, _
                            ByRef PosLeft as Long _             'added by me
                            ByRef PosTop as Long _              'added by me
                            ByRef ShowDontAsk As Boolean, _
                            ParamArray btnText()) As Long
    End Function
    But what I do not know is how to get the project to accept the numbers I enter to position the form like
    Code:
       PosLeft = Me.Move (Screen.Width - Me.Width) / 2, _
              (Screen.Height - Me.Height) / 2
       PosTop = 200
    Can you help further please

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Position MsgBox

    Well, I would do something slightly different:
    Code:
    'in the form (any form in your project that needs to call msgbox
    Option Explicit
    
    Private Sub Command1_Click()
        ShowMessageBox Me.Left, Me.Top, Me, ... '<<< replace "..." with all manadatory parameters
    End Sub
    
    'in the module
    Option Explicit
    
    Public Sub MsgCstm(X As Single, Y As Single, frmOwner As Form, _
                       sPromt As String, sTitle As String, MsgIcon As eMsgIcon, _
                       DefaultBtn As Long, ShowDontAsk As Boolean, ParamArray btnText() _
                      )
    
        Load frmMessageBox
        
        '--------------------
        'main code goes here
        '--------------------
        
        'position form
        frmMessageBox.Move X, Y
        
        'finally display the msgbox
        frmMessageBox.Show vbModal, frmOwner
    
    End Sub

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    356

    Re: Position MsgBox

    Hi RhinoBull
    Sorry I am being a bit thick today I am not sure where to put the Sub or Function ShowMessageBox.
    This is the code I have added as per your post:-
    frmCopy
    Code:
    Private Sub Command1_Click()
    ShowMessageBox Me.BackColor, Me.Caption, Me.ControlBox, Me.Enabled, Me.Font, Me.Height, _
                   Me.Left, Me.Moveable, Me.Top, Me.Visible
    End Sub
    modMsgBox
    Code:
    Public Sub MsgCstm(X As Single, Y As Single, frmOwner As Form, _
                       sPromt As String, sTitle As String, MsgIcon As eMsgIcon, _
                       DefaultBtn As Long, ShowDontAsk As Boolean, ParamArray btnText() _
                      )
    
        Load frmMessageBox
        
        '--------------------
        'main code goes here
        '--------------------
        
        'position form
        frmMessageBox.Move X, Y
        
        'finally display the msgbox
        frmMessageBox.Show vbModal, frm2 'frmOwner
    
    End Sub
    Does this still call John R Parrish Button code?
    Need to get some more help please.

  9. #9

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    356

    Re: Position MsgBox

    Hi RhinoBull

    When I said I was being a bit thick today it was true.
    Staring me in the face was a form load sub put the position
    code there and it all works fine and as I wanted.
    Thanks for you time.
    Problem resolved

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.