Results 1 to 3 of 3

Thread: How do I automatically close a message box window?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    New York
    Posts
    165

    Question How do I automatically close a message box window?

    Hi,

    Does anybody know how to automatically close a vb message box window? ie. instead of the user clicking yes, the window closes by itself after displaying the message.

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Put this code in a module (it MUST be in a module, because we will be doing some sub classing)

    Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
    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 Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

    Public Const NV_CLOSEMSGBOX As Long = &H5000&
    Public Const API_FALSE As Long = 0&


    Public Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    KillTimer hWnd, idEvent
    Dim hMessageBox As Long
    'Replace 'Self Closing Message Box' with the title you gave to your message box.
    hMessageBox = FindWindow("#32770", "Self Closing Message Box")
    If hMessageBox Then
    Call SetForegroundWindow(hMessageBox)
    SendKeys "{enter}"
    End If
    Call LockWindowUpdate(API_FALSE)
    End Sub

    'Place this code right before you call your message box
    'Replace 'Self Closing Message Box' with the title you give to your message box.
    ''Replace the '4000' below with the number of milliseconds the message box
    'will appear. 1000 milliseconds = 1 second
    SetTimer hWnd, NV_CLOSEMSGBOX, 4000&, AddressOf TimerProc
    Call MessageBox(hWnd, "Watch this message box close itself after four seconds", _
    "Self Closing Message Box", vbInformation)
    End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    New York
    Posts
    165
    Thanks Hack, that's great

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