Results 1 to 6 of 6

Thread: msgbox location

  1. #1

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    msgbox location

    Is there any way to change the msgbox position?

    Thanks in advance


    Has someone helped you? Then you can Rate their helpful post.

  2. #2
    Addicted Member thedoc1's Avatar
    Join Date
    Jan 2002
    Location
    Scenic Tonawanda, NY
    Posts
    143

    Default..

    Unfortunately, I don't think so. I believe all msgbox come up center form. An alternative is to create your own msgboxes with forms and set them as vbmodal


    Have a great day.
    Thanks

    Doc

  3. #3
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    Oh yes, there's a way to change a MSGBOX position...search MSDN, I had the code...if I find it I will post it...but it is possible...I got the code from MSDN
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

  4. #4
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    Here it is:

    Example:

    1) Start a new Standard EXE project. Form1 is created by default.
    2) Add a module to the project and add the following code to the new module:

    VB Code:
    1. Type RECT
    2.          Left As Long
    3.          Top As Long
    4.          Right As Long
    5.          Bottom As Long
    6.       End Type
    7.  
    8.       Public Declare Function UnhookWindowsHookEx Lib "user32" ( _
    9.          ByVal hHook As Long) As Long
    10.       Public Declare Function GetWindowLong Lib "user32" Alias _
    11.          "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) _
    12.          As Long
    13.       Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
    14.       Public Declare Function SetWindowsHookEx Lib "user32" Alias _
    15.          "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
    16.          ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    17.       Public Declare Function SetWindowPos Lib "user32" ( _
    18.          ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
    19.          ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
    20.          ByVal cy As Long, ByVal wFlags As Long) As Long
    21.       Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd _
    22.          As Long, lpRect As RECT) As Long
    23.  
    24.       Public Const GWL_HINSTANCE = (-6)
    25.       Public Const SWP_NOSIZE = &H1
    26.       Public Const SWP_NOZORDER = &H4
    27.       Public Const SWP_NOACTIVATE = &H10
    28.       Public Const HCBT_ACTIVATE = 5
    29.       Public Const WH_CBT = 5
    30.  
    31.       Public hHook As Long
    32.  
    33.       Function WinProc1(ByVal lMsg As Long, ByVal wParam As Long, _
    34.          ByVal lParam As Long) As Long
    35.  
    36.          If lMsg = HCBT_ACTIVATE Then
    37.             'Show the MsgBox at a fixed location (0,0)
    38.             SetWindowPos wParam, 0, 0, 0, 0, 0, _
    39.                          SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
    40.             'Release the CBT hook
    41.             UnhookWindowsHookEx hHook
    42.          End If
    43.          WinProc1 = False
    44.  
    45.       End Function
    46.  
    47.       Function WinProc2(ByVal lMsg As Long, ByVal wParam As Long, _
    48.          ByVal lParam As Long) As Long
    49.  
    50.       Dim rectForm As RECT, rectMsg As RECT
    51.       Dim x As Long, y As Long
    52.  
    53.          'On HCBT_ACTIVATE, show the MsgBox centered over Form1
    54.          If lMsg = HCBT_ACTIVATE Then
    55.             'Get the coordinates of the form and the message box so that
    56.             'you can determine where the center of the form is located
    57.             GetWindowRect Form1.hwnd, rectForm
    58.             GetWindowRect wParam, rectMsg
    59.             x = (rectForm.Left + (rectForm.Right - rectForm.Left) / 2) - _
    60.                 ((rectMsg.Right - rectMsg.Left) / 2)
    61.             y = (rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2) - _
    62.                 ((rectMsg.Bottom - rectMsg.Top) / 2)
    63.             'Position the msgbox
    64.             SetWindowPos wParam, 0, x, y, 0, 0, _
    65.                          SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
    66.             'Release the CBT hook
    67.             UnhookWindowsHookEx hHook
    68.          End If
    69.          WinProc2 = False
    70.  
    71.       End Function

    3) Add two CommandButtons to Form1.
    4) Add the following code to Form1:

    VB Code:
    1. Private Sub Command1_Click()
    2.       Dim hInst As Long
    3.       Dim Thread As Long
    4.  
    5.          'Set up the CBT hook
    6.          hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
    7.          Thread = GetCurrentThreadId()
    8.          hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, _
    9.                                   Thread)
    10.  
    11.          'Display the message box
    12.          MsgBox "This message box has been positioned at (0,0)."
    13.  
    14.       End Sub
    15.  
    16.       Private Sub Command2_Click()
    17.       Dim hInst As Long
    18.       Dim Thread As Long
    19.  
    20.          'Set up the CBT hook
    21.          hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
    22.          Thread = GetCurrentThreadId()
    23.          hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, _
    24.                                   Thread)
    25.  
    26.          'Display the message box
    27.          MsgBox "This message box is centered over Form1."
    28.       End Sub

    5) Press the F5 key to run the program. Click Command1 and the message box appears at the upper-left corner of the screen (0,0). Click OK to dismiss the message box. Click Command2 and the message box appears at the center of Form1. Click OK to dismiss the message box.
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

  5. #5

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    thanks mxnmx!!!


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    Your welcome!
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

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