Results 1 to 17 of 17

Thread: message box

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779

    Question message box

    Is there a way to center a message box on the form? In other words I want the message box to be center owner. It always seems that the message box is center screen.

  2. #2
    Fanatic Member laserman's Avatar
    Join Date
    Jan 2002
    Location
    Wales U.K
    Posts
    775
    This is what tou want.
    Attached Files Attached Files

  3. #3
    Fanatic Member laserman's Avatar
    Join Date
    Jan 2002
    Location
    Wales U.K
    Posts
    775
    Here is the exe version.
    Attached Files Attached Files

  4. #4
    Hyperactive Member VB IT's Avatar
    Join Date
    Feb 2003
    Posts
    381

    Use Frame

    You Can Use Frame For That, Design Frame like massage box, at desigen time set frame ‘Bring to Front’ and it run time

    If WantotShow=True Then
    MasageFrmae.Visible = True
    Else
    MasageFrmae.Visible= False
    EndIf

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779
    Hey VB IT, I have no clue what the you're talking about.

  6. #6
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    1st: create your own form, design it and put text and buttons.
    2nd: name and set options for the form, and then set StartupPosition to 2 - CenterOwner
    3rd: to open it, just put in *Formname.visble = True, and to close it; *Formname.visble = False

    * Replace Formname with your forms name...


  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    779
    I don't want to create a new form for the message box. All I want to know is, is there an easy way to center my message box in my form instead of the screen. I only want to use the MsgBox function, I don't want to create a new form.

  8. #8
    Lively Member NuLLziLLa's Avatar
    Join Date
    Nov 2002
    Location
    USA
    Posts
    103
    hmm i too am interested in seeing how you can center it with code.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I don't believe it can be done.
    I tried using the API message box and get the handel,
    but it does not return the handel only a '1' for success.
    My thought was to get the message box handel and use
    GetParent and SetParent API's to set it to your form.
    Without the handel it can not be moved.
    Since the vb message box function and API message box functions
    utilize the same user32.dll, unless you had the code to the dll,
    I don't believe it can be done.
    Looks like you will have to create another form to simulate
    the message box.

    If someone else has any other ideas, or can prove me wrong,
    I'd like to see them too.

  10. #10
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I also do not beleive it is possible. You'll have to make your own message box which is actually better if you ask me becasue you can totaly customize it and do whatever you want with it.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  11. #11
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    I too suggest that U go for a Custom Designed MessageBox with Form, though the code does something like what U'r after using the MessageBox API Call.
    VB Code:
    1. '1 Form
    2. '1 Timer Control name tmrMBox
    3. '1 Command Button to popup the MsgBox
    4.  
    5. Const MB_OK = &H0&
    6.  
    7. Private Declare Function MessageBox _
    8. Lib "user32" Alias "MessageBoxA" _
    9. (ByVal hwnd As Long, _
    10. ByVal lpText As String, _
    11. ByVal lpCaption As String, _
    12. ByVal wType As Long) As Long
    13.  
    14. Private Declare Function FindWindow _
    15. Lib "user32" Alias "FindWindowA" _
    16. (ByVal lpClassName As String, _
    17. ByVal lpWindowName As String) As Long
    18.  
    19. Private Declare Function MoveWindow Lib "user32" _
    20. (ByVal hwnd As Long, _
    21. ByVal x As Long, _
    22. ByVal y As Long, _
    23. ByVal nWidth As Long, _
    24. ByVal nHeight As Long, _
    25. ByVal bRepaint As Long) As Long
    26.  
    27.  
    28. Dim lngMBoxHwnd As Long
    29.  
    30. Private Sub Command1_Click()
    31. MessageBox Me.hwnd, "MBox Prompt", "MBox Title", MB_OK
    32. End Sub
    33.  
    34. Private Sub Form_Load()
    35. Me.ScaleMode = vbPixels
    36. tmrMBox.Interval = 1
    37. tmrMBox.Enabled = True
    38. End Sub
    39.  
    40. Private Sub tmrMBox_Timer()
    41. Dim oLeft As Long
    42. Dim oTop As Long
    43. Debug.Print Now
    44. lngMBoxHwnd = FindWindow(ByVal vbNullString, "MBox Title")
    45.     If lngMBoxHwnd > 0 Then
    46.         oTop = Me.ScaleTop + (Me.ScaleHeight / 2)
    47.         oLeft = Me.ScaleLeft + (Me.ScaleWidth / 2)
    48.         'U could use the GetWindowPlacement API Call to
    49.         'Find out the length of the MessageBox to correctly
    50.         'set the width of the msgbox
    51.         MoveWindow lngMBoxHwnd, oLeft, oTop, 100, 100, 1
    52.     End If
    53. End Sub

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    KayJay: wont the messagebox stop the processing of the code since its modal?
    Therefore the timer will not fire until after the messagebox has been dismissed.

  13. #13
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Almost anything is possible in VB with a little help from the API's:

    In a Standard Module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type CWPSTRUCT
    4.   lParam As Long
    5.   wParam As Long
    6.   message As Long
    7.   hwnd As Long
    8. End Type
    9.  
    10. Private Type RECT
    11.   Left As Long
    12.   Top As Long
    13.   Right As Long
    14.   Bottom As Long
    15. End Type
    16.  
    17. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    18. 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
    19. Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
    20. Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    21. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    22. 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
    23. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    24. 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
    25. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    26.  
    27. Private Const WH_CALLWNDPROC = 4
    28. Private Const GWL_WNDPROC = (-4)
    29.  
    30. Private Const WM_DESTROY = &H2
    31. Private Const WM_CREATE = &H1
    32. Private Const WM_SHOWWINDOW = &H18
    33.  
    34. Private Const SWP_NOSIZE = &H1
    35. Private Const SWP_NOZORDER = &H4
    36.  
    37. Private mlHook As Long
    38. Private mlPrevWnd As Long
    39. Private mlCenterhWnd As Long
    40.  
    41. Private Function SubMsgBox(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    42.   Dim tRECT As RECT
    43.   Dim tOWNER As RECT
    44.  
    45.   Select Case Msg
    46.   Case WM_SHOWWINDOW
    47.     ' MsgBox Displayed, If a window Handle was passed to the MsgBoxEx Function
    48.     ' Attempt to center this dialog on that Window.
    49.     If mlCenterhWnd Then
    50.     ' Get the Dialog's Coords
    51.     Call GetWindowRect(hwnd, tRECT)
    52.     ' Get the Owner Window's Coords
    53.     Call GetWindowRect(mlCenterhWnd, tOWNER)
    54.    
    55.     ' Calculate the Center Owner Position
    56.     tRECT.Left = tOWNER.Left + (((tOWNER.Right - tOWNER.Left) - (tRECT.Right - tRECT.Left)) / 2)
    57.     tRECT.Top = tOWNER.Top + (((tOWNER.Bottom - tOWNER.Top) - (tRECT.Bottom - tRECT.Top)) / 2)
    58.    
    59.     ' Reposition the MsgBox Dialog
    60.     SetWindowPos hwnd, 0, tRECT.Left, tRECT.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER
    61.    
    62.     End If
    63.    
    64.   Case WM_DESTROY
    65.     ' Remove the MsgBox Subclassing
    66.     Call SetWindowLong(hwnd, GWL_WNDPROC, mlPrevWnd)
    67.    
    68.   End Select
    69.  
    70.   SubMsgBox = CallWindowProc(mlPrevWnd, hwnd, Msg, wParam, ByVal lParam)
    71.  
    72. End Function
    73.  
    74. Private Function HookWindow(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    75.   Dim tCWP As CWPSTRUCT
    76.   Dim sClass As String
    77.  
    78.   ' This is where you need to Hook the Messagebox
    79.   CopyMemory tCWP, ByVal lParam, Len(tCWP)
    80.  
    81.   If tCWP.message = WM_CREATE Then
    82.     sClass = Space(255)
    83.     sClass = Left(sClass, GetClassName(tCWP.hwnd, ByVal sClass, 255))
    84.    
    85.     If sClass = "#32770" Then
    86.       ' Subclass the Messagebox as it's created
    87.       mlPrevWnd = SetWindowLong(tCWP.hwnd, GWL_WNDPROC, AddressOf SubMsgBox)
    88.     End If
    89.   End If
    90.   HookWindow = CallNextHookEx(mlHook, nCode, wParam, ByVal lParam)
    91. End Function
    92.  
    93. Public Function MsgBoxEx(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title, Optional HelpFile, Optional Context, Optional CenterhWnd As Long) As VbMsgBoxResult
    94.   ' Store the Parent Window Handle (if specified)
    95.   mlCenterhWnd = CenterhWnd
    96.  
    97.   'Set the Defaults
    98.   If IsEmpty(Title) Then Title = App.Title
    99.          
    100.   'Hook the Thread
    101.   mlHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookWindow, App.hInstance, App.ThreadID)
    102.  
    103.   'Store the Return Value
    104.   MsgBoxEx = MsgBox(Prompt, Buttons, Title, HelpFile, Context)
    105.  
    106.   'Remove the Hook
    107.   Call UnhookWindowsHookEx(mlHook)
    108. End Function
    109.  
    110. Private Function LOWORD(dwValue As Long) As Integer
    111.   CopyMemory LOWORD, dwValue, 2
    112. End Function
    113.  
    114. Private Function HIWORD(dwValue As Long) As Integer
    115.   CopyMemory HIWORD, ByVal VarPtr(dwValue) + 2, 2
    116. End Function
    117.  
    118. Private Function MAKELONG(wLow As Long, wHigh As Long) As Long
    119.   MAKELONG = LOWORD(wLow) Or (&H10000 * LOWORD(wHigh))
    120. End Function
    Example Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.   MsgBoxEx "Example of a Messagebox" & vbCrLf & "Which has been Centered on it's owner.", vbInformation + vbOKOnly, CenterhWnd:=Me.hwnd
    3. End Sub

  14. #14
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Aaron - you are the man! That is incredible!

    That is also the most code I have ever seen to display a msgbox!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Just tried out your example Aaron.

    Works - Cool job

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


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

  17. #17

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