[RESOLVED] how can i create a pop msg for 2 seconds and then make it disaper-VBForums
Results 1 to 11 of 11

Thread: [RESOLVED] how can i create a pop msg for 2 seconds and then make it disaper

  1. #1

    Thread Starter
    baila como nunca
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,180

    Resolved [RESOLVED] how can i create a pop msg for 2 seconds and then make it disaper

    how can i create a pop msg for 2 seconds and then make it disappear
    even with a label or something
    i prefer a function
    tnx
    salsa

  2. #2
    PowerPoster
    Join Date
    Oct 2013
    Posts
    2,311

    Re: how can i create a pop msg for 2 seconds and then make it disaper


  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    2,111

    Re: how can i create a pop msg for 2 seconds and then make it disaper

    Here's one I've used for years. Just throw it into a BAS module, do a bit of studying on how to use it, and voila.

    Note that it returns the caption of your clicked button rather than a return code. Also, it returns "TimedOut" if it timed out. Also, clicking the "X" (close button in non-client area) is like a Cancel, which is similar to clicking the button tagged as "Cancel".

    Code:
    
    Option Explicit
    '
    Public Enum MsgIconEnum
        mbCritical = 16
        mbExclamation = 48
        mbInformation = 64
        mbQuestion = 32
    End Enum
    #If False Then ' Intellisense fix.
        Public mbCritical, mbExclamation, mbInformation, mbQuestion
    #End If
    '
    Public Enum MsgStyleEnum
        mbYesNoCancel = &H3&
        mbYesNo = &H4&
        mbRetryCancel = &H5&
        mbOKCancel = &H1&
        mbOkOnly = &H0&
        mbAbortRetryIgnore = &H2&
    End Enum
    #If False Then ' Intellisense fix.
        Public mbYesNoCancel, mbYesNo, mbRetryCancel, mbOKCancel, mbOkOnly, mbAbortRetryIgnore
    #End If
    '
    Private Type MSGBOX_HOOK_PARAMS
       hwndOwner   As Long
       hHook       As Long
    End Type
    '
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private 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
    Private Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) 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 SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Private Declare Function IsWindow Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
    '
    Dim mbStyle As MsgStyleEnum
    Dim mbIcon As MsgIconEnum
    Dim msTitle As String
    '
    Dim TimerID As Long
    Dim TimedOut As Boolean
    '
    Dim msBut1 As String
    Dim msBut2 As String
    Dim msBut3 As String
    '
    Dim mhWndMsgBox As Long
    Dim MSGHOOK As MSGBOX_HOOK_PARAMS
    '
    
    Public Function MsgBoxEx(hwndOwner As Long, Style As MsgStyleEnum, Title As String, message As String, Optional Icon As MsgIconEnum, _
                             Optional ButA As String, Optional ButB As String, Optional ButC As String, _
                             Optional MilliSeconds As Long) As String
        ' This function sets your custom parameters and returns which button was pressed as a string.
        ' If a message box is a style with a "Cancel" button, the "X" on the form will be enabled.
        ' If the "X" of the form is pressed, the text of the last button (corresponding to cancel) will be returned.
        ' On an mbOkOnly textbox, the "X" will return the OK button.
        ' MilliSeconds is a timer.  If it times out, the function returns "TimedOut" string.  Can't be used with (mbAbortRetryIgnore and mbYesNo styles).
        Dim mReturn As Long
        Dim hInstance As Long
        Dim hThreadId As Long
        '
        Const WH_CBT = 5
        Const GWL_HINSTANCE = -6
        '
        Const IDOK = 1
        Const IDCANCEL = 2
        Const IDABORT = 3
        Const IDRETRY = 4
        Const IDIGNORE = 5
        Const IDYES = 6
        Const IDNO = 7
        '
        mbStyle = Style
        mbIcon = Icon
        msTitle = Title
        'mPrompt = Message
        msBut1 = ButA
        msBut2 = ButB
        msBut3 = ButC
        '
        hInstance = App.hInstance
        hThreadId = GetCurrentThreadId()
        MSGHOOK.hwndOwner = GetDesktopWindow()
        MSGHOOK.hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, hInstance, hThreadId)
        '
        ' No default value is defined for mbAbortRetryIgnore and mbYesNo message box styles.
        ' In other words, the close (x) button is disabled, and the timer can NOT close the box.
        ' Therefore, timer can't be used.
        If Style = mbAbortRetryIgnore Or Style = mbYesNo Then MilliSeconds = 0
        If MilliSeconds <> 0 Then TimerID = SetTimer(0&, 0&, MilliSeconds, AddressOf MsgBoxTimerProc)
        '
        mReturn = MessageBox(hwndOwner, message, Space$(120), mbStyle Or mbIcon)
        '
        If TimerID <> 0 Then
            KillTimer 0&, TimerID
            TimerID = 0
        End If
        '
        If TimedOut Then
            MsgBoxEx = "TimedOut"
            TimedOut = False
        Else
            Select Case mReturn
            Case IDOK:            MsgBoxEx = msBut1
            Case IDABORT:         MsgBoxEx = msBut1
            Case IDRETRY:         MsgBoxEx = msBut2
            Case IDIGNORE:        MsgBoxEx = msBut3
            Case IDYES:           MsgBoxEx = msBut1
            Case IDNO:            MsgBoxEx = msBut2
            Case IDCANCEL ' This may be the second or third button.
                If (mbStyle And 7) = mbYesNoCancel Then
                    MsgBoxEx = msBut3
                Else
                    MsgBoxEx = msBut2
                End If
            End Select
        End If
    End Function
    
    Public Function MsgBoxHookProc(ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        ' This function catches the messagebox before it opens
        ' and changes the text of the buttons - then removes the hook.
        Const IDPROMPT = &HFFFF&
        Const IDOK = 1
        Const IDCANCEL = 2
        Const IDABORT = 3
        Const IDRETRY = 4
        Const IDIGNORE = 5
        Const IDYES = 6
        Const IDNO = 7
        Const HCBT_ACTIVATE = 5
        '
        If uMsg = HCBT_ACTIVATE Then
            mhWndMsgBox = wParam
            SetWindowText wParam, msTitle
            'SetDlgItemText wParam, IDPROMPT, mPrompt
            Select Case (mbStyle And 7) ' Filter out other bits.
            Case vbAbortRetryIgnore
                SetDlgItemText wParam, IDABORT, msBut1
                SetDlgItemText wParam, IDRETRY, msBut2
                SetDlgItemText wParam, IDIGNORE, msBut3
            Case vbYesNoCancel
                SetDlgItemText wParam, IDYES, msBut1
                SetDlgItemText wParam, IDNO, msBut2
                SetDlgItemText wParam, IDCANCEL, msBut3
            Case vbOKOnly
                SetDlgItemText wParam, IDOK, msBut1
            Case vbRetryCancel
                SetDlgItemText wParam, IDRETRY, msBut1
                SetDlgItemText wParam, IDCANCEL, msBut2
            Case vbYesNo
                SetDlgItemText wParam, IDYES, msBut1
                SetDlgItemText wParam, IDNO, msBut2
            Case vbOKCancel
                SetDlgItemText wParam, IDOK, msBut1
                SetDlgItemText wParam, IDCANCEL, msBut2
            End Select
            UnhookWindowsHookEx MSGHOOK.hHook
        End If
        MsgBoxHookProc = False
    End Function
    
    Public Function MsgBoxTimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long) As Long
        Const WM_CLOSE = &H10
        If IsWindow(mhWndMsgBox) Then
            PostMessage mhWndMsgBox, WM_CLOSE, 0, ByVal 0&
            TimedOut = True
        End If
    End Function
    
    
    Enjoy,
    Elroy

  4. #4
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    299

    Re: how can i create a pop msg for 2 seconds and then make it disaper

    OK, I have a very stupid question. Why not just use a timer

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    2,111

    Re: how can i create a pop msg for 2 seconds and then make it disaper

    I possibly misunderstood the OP's question. I was thinking he wanted a MsgBox with a timer, and that's what I provided. And, effectively, it does use a timer.

    If OP is just putting up his own form and wants it to disappear, I agree. A typical timer will get it done.

    Regards,
    Elroy

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    11,450

    Re: how can i create a pop msg for 2 seconds and then make it disaper

    Yeah it all depends on what the goal is as far as appearance and function.

    A timer for sure and beyond that there are many options. A label, a frame, a form, a picture and so on.

    It is just a simple matter of making whatever is chosen visible then starting a timer that will unload or hide it when the time is right.


    As far as the statement "I prefer a function" I have no idea why as the purpose of a function is to return a value of some sort and clearly there is no need for that unless there is critical info that was left out of the OP.

    What you would do is create a sub that shows whatever it is you want to show and start or activate a timer then of course in the timer event that object whatever it is would be unloaded or hidden and the timer deactivated or stopped.
    Last edited by DataMiser; Jan 11th, 2017 at 12:43 PM.

  7. #7

    Thread Starter
    baila como nunca
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,180

    Re: how can i create a pop msg for 2 seconds and then make it disaper

    elroy bingo!!

  8. #8
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    299

    Re: [RESOLVED] how can i create a pop msg for 2 seconds and then make it disaper

    Elroy,

    I used this:

    MsgBoxEx Me.hWnd, mbYesNo, "Form1", "Hello from Form1", mbInformation, "Yes", "No", "Maybe", 2000

    and it never terminates

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    2,111

    Re: [RESOLVED] how can i create a pop msg for 2 seconds and then make it disaper

    Hi VB6_Lover. Please read the comments in the MsgBoxEx function. Specifically, "MilliSeconds is a timer. If it times out, the function returns "TimedOut" string. Can't be used with (mbAbortRetryIgnore and mbYesNo styles)." My implementation of the MsgBox timer clicks the "X" close button. Some of the MsgBoxes don't have this button (or it's disabled). In other words, a timeout is thought of like a "Cancel". If you've got no "Cancel", you've got no way to time out.

    Best,
    Elroy

    EDIT1: I know full well that there are other ways to do it. This is just what I had handy.

  10. #10
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    299

    Re: [RESOLVED] how can i create a pop msg for 2 seconds and then make it disaper

    That's a shame, Elroy, as Yes/No are very much used especially in my apps. On a Yes or No why not click the No button. I would think that if a user doesn't respond he means No. Usually clicking the No button would be the same as clicking the X button (maybe not in all cases however) but then why not give the programmer an option in the call statement to choose which button to click if user doesn't click anything

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    2,111

    Re: [RESOLVED] how can i create a pop msg for 2 seconds and then make it disaper



    VB6_Lover, that's just what I had. I can certainly "think through" how to do what you're proposing, but I've gotten a bit busy with another project.

    Best Of Luck,
    Elroy

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.