Announcement

Collapse
No announcement yet.

Auto closing a MsgBox

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Auto closing a MsgBox

    How do I auto close a MsgBox ?

    I have a simple little program that creates a message box to tell the user that the operation was successful or not.

    Is there a way to get the MsgBox to AUTO-CLOSE after so many seconds?

  • #2
    See this discussion:
    Timed Messagebox
    http://www.jose.it-berater.org/smffo...p?topic=1639.0
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      José

      Thanks. That's the simpliest solution that I have seen. There were other solutions offerred here. But this is by far the simplest.

      I hope you don't mind, but for easy referrence on this forum I have copied your solution here.

      Code:
      ' code by José Roca
      #COMPILE EXE
      #DIM ALL
      #INCLUDE "WIN32API.INC"
      
      GLOBAL szMsgBoxTitle AS ASCIIZ * 256
      
      SUB TimedMsgBoxProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL idEvent AS DWORD, BYVAL dwTime AS DWORD) 
         SendMessage FindWindow("", szMsgBoxTitle), %WM_CLOSE, 0, 0
      END SUB
      
      FUNCTION PBMAIN () AS LONG
      
         LOCAL dwTimerID AS DWORD
         LOCAL lRes AS LONG
         
         szMsgBoxTitle = "Timed Message Box"
         dwTimerID = SetTimer(0, 0, 5000, CODEPTR(TimedMsgBoxProc))
         lRes = MSGBOX("Timed message box test", %MB_OK, szMsgBoxTitle)
         KillTimer 0, dwTimerId
      
      END FUNCTION

      Comment


      • #4
        I had he exact same question. You can find the answer here:

        http://www.powerbasic.com/support/pb...ad.php?t=12978

        and/or

        http://www.powerbasic.com/support/pb...ad.php?t=22967
        There are no atheists in a fox hole or the morning of a math test.
        If my flag offends you, I'll help you pack.

        Comment


        • #5
          I do not agree with the code above, in this case a close may be ok but actually you should 'click' the button.
          Each button has the same id as the output and should be used instead.
          (Imagne a YES/NO box and so on)

          A msgbox has no close button but the source does close it.. the hard way.
          hellobasic

          Comment


          • #6
            Here is one that also works on a command line which is useful within batch files.
            Using a timer will not always work as expected if you use a statement like TCP OPEN which is in a wait state.
            Running in another thread solves that problem.
            Code:
            #DIM ALL
            #COMPILE EXE  'Sleeper.bas  http://www.powerbasic.com/support/pb...ad.php?t=40472
            #INCLUDE "win32api.inc"
            GLOBAL gsMessageBox AS STRING
            FUNCTION PBMAIN AS LONG
               gsMessageBox = "Press Enter, click OK or wait 2000 milliseconds"
               Sleeper 2000  'wait 2000 milliseconds
             END FUNCTION
            FUNCTION Sleeper (milliseconds AS DWORD) AS LONG
               LOCAL hThread AS DWORD
               IF LEN(COMMAND$) THEN                'override passed value
                milliseconds = ABS(VAL(COMMAND$))  'use value in COMMAND$
              END IF
              THREAD CREATE TimedMessageBoxThread(milliseconds) TO hThread
              SLEEP 50
              WaitForSingleObject hThread, milliseconds
              THREAD CLOSE hThread TO hThread
             END FUNCTION
            THREAD FUNCTION TimedMessageBoxThread(BYVAL milliseconds AS DWORD) AS LONG
             #IF %DEF(%PB_CC32)
              ? gsMessageBox
              WAITKEY$
            #ELSE
              ? gsMessageBox
            #ENDIF
            END FUNCTION
            Last edited by Mike Doty; 22 Jul 2009, 07:02 AM. Reason: Did not post correctly. Reposted serveral times.
            CMD shortcut to open any location:
            %windir%\system32\cmd.exe /k " cd\ & x: & cd x:\xxxx
            Change to run as administrator
            How long is an idea? Write it down.

            Comment


            • #7
              If sending that message to a console screen instead of a GUI-type message box would be OK...
              Wait for key, click or clock for PB/WIN and PB/CC

              (You'll never guess from the title what it does!)
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                >WaitForSingleObject hThread, milliseconds

                In the demo shown this may as well be ....
                Code:
                SLEEP milliseconds
                .. because the only reason this 'works' is that you reach the end of PBMAIN when WFSO returns at which point the compiler calls ExitProcess which terminates all threads of execution.

                Which of course means, this function is not usable EXCEPT when it's the last thing the program does.

                If you waited %INFINITE it would wait on the user to Hit a key (PBCC) or click OK (PBWIN).
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  WaitForSingleObject is required.
                  Pressing/clicking would always wait with a SLEEP statement.
                  This code could be improved to return if user performed an action or time was reached.

                  Code:
                  #DIM ALL    'MessageBoxWait.bas
                  #COMPILE EXE
                  #INCLUDE "win32api.inc"
                  GLOBAL gsMessageBox AS STRING
                  FUNCTION PBMAIN AS LONG
                     MessageBoxWait "How now brown cow", 2000
                     ? "Done"
                  END FUNCTION
                  SUB MessageBoxWait (s AS STRING,milliseconds AS DWORD)
                     LOCAL hThread AS DWORD
                     IF LEN(COMMAND$) THEN                'override passed value
                      milliseconds = ABS(VAL(COMMAND$))  'use value in COMMAND$
                    END IF
                    gsMessageBox = s
                    THREAD CREATE TimedMessageBoxThread(milliseconds) TO hThread
                    WaitForSingleObject hThread, milliseconds
                    REM SLEEP milliseconds  'wrong, would always wait
                    THREAD CLOSE hThread TO hThread
                  END SUB
                  THREAD FUNCTION TimedMessageBoxThread(BYVAL milliseconds AS DWORD) AS LONG
                   #IF %DEF(%PB_CC32)
                    ? gsMessageBox
                    WAITKEY$
                  #ELSE
                    ? gsMessageBox
                  #ENDIF
                  END FUNCTION
                  Last edited by Mike Doty; 22 Jul 2009, 12:49 PM.
                  CMD shortcut to open any location:
                  %windir%\system32\cmd.exe /k " cd\ & x: & cd x:\xxxx
                  Change to run as administrator
                  How long is an idea? Write it down.

                  Comment


                  • #10
                    DUH, where is my head today....

                    WFSO is going to return in 'millseconds' or less... whichever occurs first:
                    A. User hits a key (PBCC) or clicks OK on MSGBOX (PB/Win) (ends function, signals the thread object resulting in WFSO returning WAIT_OBJECT_0)
                    B. 'Millseconds' elapse. (WFSO returns WAIT_TIMEOUT)

                    ... which is what you wanted.

                    However, the function TimedMessageBoxThread() is going to execute until "A" occurs or the process ends.

                    MCM
                    Last edited by Michael Mattias; 22 Jul 2009, 04:25 PM.
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment

                    Working...
                    X