CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2000
    Posts
    264

    Msgbox Positioning

    Is there a way to specify the position of a MsgBox? You know like centerowner, etc... without making a separate form for the MsgBox? I am using the MsgBox statement.


  2. #2
    Join Date
    Jan 2000
    Location
    CA
    Posts
    52

    Re: Msgbox Positioning

    I don't think that you can specify the position of MsgBox, but, you can create your own, using a new form. Set this form to be a dialog box (can't resize it) and place the appropriate text and your desired buttons onto it. You can specify the location of a form.

    Good Luck...


  3. #3
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Msgbox Positioning

    Here's a Wrapper I wrote for the MsgBox Dialog which adds 3 extra Parameters;
    X, Y and StartupPostion which allow you to specify either Custom X/Y Coords or predefined ones like CenterScreen and CenterOwner:
    Code:
    'In a Module...
    public Enum StartupPos
        CenterScreen
        CenterOwner
        Custom
    End Enum
    
    private Type RECT
            Left as Long
            Top as Long
            Right as Long
            Bottom as Long
    End Type
    
    private Type CWPSTRUCT
            lParam as Long
            wParam as Long
            message as Long
            hwnd as Long
    End Type
    
    private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination as Any, Source as Any, byval Length 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 CallNextHookEx Lib "user32" (byval hHook as Long, byval nCode as Long, byval wParam as Long, lParam as Any) as Long
    private Declare Function UnhookWindowsHookEx Lib "user32" (byval hHook as Long) as Long
    private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (byval hwnd as Long, byval nIndex as Long, byval dwNewLong as Long) as Long
    private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (byval lpPrevWndFunc as Long, byval hwnd as Long, byval Msg as Long, byval wParam as Long, byval lParam as Long) as Long
    private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (byval hwnd as Long, byval lpClassName as string, byval nMaxCount 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
    private Declare Function GetWindowRect Lib "user32" (byval hwnd as Long, lpRect as RECT) as Long
    private Declare Function GetParent Lib "user32" (byval hwnd as Long) as Long
    
    private Const SWP_FRAMECHANGED = &H20
    private Const SWP_NOSIZE = &H1
    private Const SWP_NOZORDER = &H4
    private Const GWL_WNDPROC = (-4)
    private Const WH_CALLWNDPROC = 4
    private Const WM_CREATE = &H1
    private Const WM_INITDIALOG = &H110
    
    private MSGBOXEX_X as Integer
    private MSGBOXEX_Y as Integer
    private MSGBOXEX_STARTUP as StartupPos
    private lPrevWnd as Long
    private lHook as Long
    
    private Function SubMsgBox(byval hwnd as Long, byval Msg as Long, byval wParam as Long, byval lParam as Long) as Long
        Dim tRECT as RECT
        Dim tOWNER as RECT
        If Msg = WM_INITDIALOG then
            'Reposition the MsgBox is Neccessary..
            If MSGBOXEX_STARTUP = CenterOwner then
                Call GetWindowRect(GetParent(hwnd), tOWNER)
            else
                Call GetWindowRect(0, tOWNER)
            End If
            Call GetWindowRect(hwnd, tRECT)
            Select Case MSGBOXEX_STARTUP
            Case Custom
                If MSGBOXEX_X = -1 then
                    'Center Horz
                    tRECT.Left = ((Screen.Width / Screen.TwipsPerPixelX) - (tRECT.Right - tRECT.Left)) / 2
                else
                    'Position Horz
                    tRECT.Left = MSGBOXEX_X
                End If
                If MSGBOXEX_Y = -1 then
                    'Center Vert
                    tRECT.Top = ((Screen.Height / Screen.TwipsPerPixelY) - (tRECT.Bottom - tRECT.Top)) / 2
                else
                    'Position Vert
                    tRECT.Top = MSGBOXEX_Y
                End If
                Call SetWindowPos(hwnd, 0, tRECT.Left, tRECT.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)
            
            Case CenterOwner
                tRECT.Left = tOWNER.Left + (((tOWNER.Right - tOWNER.Left) - (tRECT.Right - tRECT.Left)) / 2)
                tRECT.Top = tOWNER.Top + (((tOWNER.Bottom - tOWNER.Top) - (tRECT.Bottom - tRECT.Top)) / 2)
                Call SetWindowPos(hwnd, 0, tRECT.Left, tRECT.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)
            
            End Select
            'Remove the Messagebox Subclassing
            Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWnd)
        End If
        SubMsgBox = CallWindowProc(lPrevWnd, hwnd, Msg, wParam, byval lParam)
    End Function
    
    private Function HookWindow(byval nCode as Long, byval wParam as Long, byval lParam as Long) as Long
        Dim tCWP as CWPSTRUCT
        Dim sClass as string
        'This is where you need to Hook the Messagebox
        CopyMemory tCWP, byval lParam, len(tCWP)
        Select Case tCWP.message
        Case WM_CREATE
            sClass = Space(255)
            sClass = Left(sClass, GetClassName(tCWP.hwnd, byval sClass, 255))
            If sClass = "#32770" then
                'Subclass the Messagebox as it's created
                lPrevWnd = SetWindowLong(tCWP.hwnd, GWL_WNDPROC, AddressOf SubMsgBox)
            End If
        End Select
        HookWindow = CallNextHookEx(lHook, nCode, wParam, byval lParam)
    End Function
    
    public Function MsgBoxEx(Prompt, optional X = -1, optional Y = -1, optional StartupPosition as StartupPos = CenterScreen, optional Buttons as VbMsgBoxStyle = vbOKOnly, optional Title, optional HelpFile, optional Context) as VbMsgBoxResult
        MSGBOXEX_X = X
        MSGBOXEX_Y = Y
        MSGBOXEX_STARTUP = StartupPosition
        'set a Thread Message Hook..
        lHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookWindow, App.hInstance, App.ThreadID)
        MsgBoxEx = MsgBox(Prompt, Buttons, Title, HelpFile, Context)
        'Remove the Hook
        Call UnhookWindowsHookEx(lHook)
    End Function
    Usage:
    MsgBoxEx "CodeGuru is one of the Best VB Forums!!", , , CenterOwner, vbInformation + vbOKOnly

    Aaron Young
    Analyst Programmer
    ajyoung@pressenter.com
    aarony@redwingsoftware.com
    Certified AllExperts Expert: http://www.allexperts.com/displayExp...p?Expert=11884
    Last edited by Cimperiali; September 24th, 2003 at 03:44 AM.
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  4. #4
    Join Date
    Jan 2000
    Posts
    264

    Re: Msgbox Positioning

    Thanks. I found something similar in the Knowledge Base Article Q180936.



  5. #5
    Join Date
    Jul 1999
    Location
    Israel
    Posts
    140

    Re: Msgbox Positioning

    Thanks for your full answer.
    What is "#32770"?


  6. #6
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Msgbox Positioning

    #32770 is the Windows Dialog Classname.

    Aaron Young
    Analyst Programmer
    ajyoung@pressenter.com
    aarony@redwingsoftware.com
    Certified AllExperts Expert: http://www.allexperts.com/displayExp...p?Expert=11884
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  7. #7
    Join Date
    Jul 1999
    Location
    Israel
    Posts
    140

    Re: Msgbox Positioning

    How do I know what is the windows dialog classname?


  8. #8
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Msgbox Positioning

    This sample is simply superb!. Really Great

    RK

  9. #9
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Msgbox Positioning

    Another piece of excellent code by Aaron - I've posted it in a downloadable form on the CodeGuru site at :

    [Cimperiali: link updated]

    http://codeguru.earthweb.com/vb/articles/2064.shtml
    - If anyone wants to try it out.


    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/updates/whats_new.shtml?id=613
    Last edited by Cimperiali; September 23rd, 2003 at 04:22 PM.

  10. #10
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Yes, this is really great

    Indeed.

    Cesare Imperiali
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured