Announcement

Collapse
No announcement yet.

Detecting when console window gains focus

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

  • Detecting when console window gains focus

    Hi everybody,

    Is there any way to do this.
    The problem is that I have an GRAPHIC WINDOW that is a static control of the console window, but when I overlap the console window with other window (i.e. Explorer) when I click on the taskbar the console window appear, but the GRAPHIC WINDOW is not redrawn.
    The GRAPHIC WINDOW is redrawn only if something is sent to it. But I don't know when this event occur (CONSOLE GAINING FOCUS) !

    Thanks,
    Gilmar.
    Brazil.
    Happy new year !

  • #2
    You can look for and react to a key or mouse event as demo'd here:

    Wait for key, click or clock for PB/WIN and PB/CC

    But if using PB/CC it might be easier to use INSTAT with mouse support enabled. (If INSTAT is true, your application must have keyboard focus).
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Hi Gilmar,
      Here is some code that use a thread and some windows api.
      If the grafic have focus then the console will get next zOrder,
      if the console have focus then the graphic will get next zOrder.
      If console is minimised then the graphic will be hidden.

      Code:
      #COMPILE EXE '#CC 4.04#
      #DIM ALL
      #INCLUDE "Win32Api.inc" '#2005-01-27#
       
      GLOBAL hCon       AS DWORD
      GLOBAL hWin       AS DWORD
      GLOBAL StopThread AS LONG
      '______________________________________________________________________________
       
      FUNCTION HocusPokusFocus(BYVAL NotUsed AS LONG) AS LONG
       
       DO UNTIL StopThread
         IF GetForeGroundWindow = hWin THEN
           IF GetWindow(hWin, %GW_HWNDNEXT) <> hCon THEN
             SetForegroundWindow hCon
             SetForegroundWindow hWin
           END IF
         ELSEIF GetForeGroundWindow = hCon THEN
           IF GetWindow(hCon, %GW_HWNDNEXT) <> hWin THEN
             SetForegroundWindow hWin
             SetForegroundWindow hCon
           END IF
         END IF
       
         IF IsIconic(hCon) THEN
           ShowWindow hWin, %SW_HIDE
         ELSE
           ShowWindow hWin, %SW_SHOW
         END IF
         SLEEP 200
       LOOP
       
      END FUNCTION
      '______________________________________________________________________________
       
      FUNCTION PBMAIN () AS LONG
       LOCAL hThread AS DWORD
       LOCAL Retval  AS LONG
       LOCAL sKey    AS STRING
       
       hCon = CONSHNDL
       
       GRAPHIC WINDOW "Window", 10, 10, 110, 110 TO hWin
       GRAPHIC ATTACH hWin, 0
       
       THREAD CREATE HocusPokusFocus(0) TO hThread
       
       STDOUT "Type any key to end"
       WAITKEY$
       
       StopThread = 1
       THREAD CLOSE hThread TO Retval
       
      END FUNCTION
      '______________________________________________________________________________

      Comment


      • #4
        but when I overlap the console window with other window (i.e. Explorer) when I click on the taskbar the console window appear, but the GRAPHIC WINDOW is not redrawn.
        I'm not sure it is supposed to be redrawn. If another window is moved over the console window that would have nothing to do with the graphic window would it?
        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Hi guys,

          I forgot to mention that I use an "keyboard checking" loop and that the graphic window is a child control of the console windows.

          Here is what I did.

          Thanks again for your support.

          Btw, does any of you guys know how to EASILY indent the code quoted in this forum ?
          If the code is too big, it will be a hard work putting the flags INDENT and /INDENT, since I already have the code indented by either tabs or spaces...

          Happy new year !!

          Code:
          #COMPILE EXE
          #DIM ALL
          #INCLUDE "WIN32API.INC"
          
          GLOBAL hGraphicWindow AS LONG
          GLOBAL iRedraw AS INTEGER
          
          SUB Check()
              ' This function solves both problems that I had.
              ' 1. When user clicks on graphic window, console loose focus so any key is not detected
              IF GetForeGroundWindow = hGraphicWindow THEN
                  CONSOLE SET FOCUS
              ELSE
                  ' 2. When user overlaps console window the same is redrawn when user clicks on taskbar
                  IF GetForeGroundWindow = CONSHNDL THEN
                      IF iRedraw = 1 THEN
                          GRAPHIC SET POS (0,40)
                          GRAPHIC PRINT "Updating..."; TIME$;
                          GRAPHIC REDRAW
                          iRedraw = 0
                      END IF
                  ELSE
                      iRedraw = 1
                  END IF
              END IF
          END SUB
          
          FUNCTION PBMAIN () AS LONG
              LOCAL A$
          
              PRINT "1. Try clicking on graphic window..."
              PRINT "2. Try overlapping graphic window with other application "
              PRINT "and then clicking on taskbar (Console window)..."
              PRINT
              PRINT "This is not supposed to be seen... ever !"
              PRINT "Press X to exit..."
              PRINT "Press any other key to update graphic window..."
              GRAPHIC WINDOW "", 0, 0, 640, 480 TO hGraphicWindow
          
              CURSOR OFF
              SetParent(hGraphicWindow, CONSHNDL) ' Make Graphic Window a Child/Control of Console
              GRAPHIC ATTACH hGraphicWindow, 0
              GRAPHIC FONT "Courier New", 10, 1
              GRAPHIC COLOR RGB(255,0,0),RGB(255,255,255)
              GRAPHIC SET POS (0,0)
              GRAPHIC PRINT "Initializing... (X) to exit";
                                        
              DO
                  SLEEP 0
                  
                  ' Uncomment this line to see solution !
                  'Check
                  
                  IF INSTAT THEN
                      A$ = INKEY$
                      IF A$ = "X" THEN EXIT DO
                      ' - Updates graphic window so it is redrawn
                      GRAPHIC SET POS (0,20)
                      GRAPHIC PRINT "Testing...("; A$; ") "; TIME$;
                  END IF
              LOOP
              GRAPHIC WINDOW END
          
          END FUNCTION
          Last edited by Gilmar de Paula Fiocca; 1 Jan 2008, 01:49 PM. Reason: Correcting tags

          Comment


          • #6
            Hi Gilmar,
            you could use [C0DE] and [/C0DE] for ident.
            See UBB code

            Also, I did some modification to the Check's sub
            becose, if I had a redraw when clicking on the graphic,
            I had no refresh when clicking on the windows title.

            Code:
            SUB Check()
             
              IF (GetForeGroundWindow <> CONSHNDL) OR (GetForeGroundWindow <> hGraphicWindow) THEN
                iRedraw = 1
              END IF
             
              IF GetForeGroundWindow = hGraphicWindow THEN
                CONSOLE SET FOCUS
              END IF
             
              IF GetForeGroundWindow = CONSHNDL THEN
                IF iRedraw = 1 THEN
                  GRAPHIC SET POS (0, 40)
                  GRAPHIC PRINT "Updating..."; TIME$;
                  GRAPHIC REDRAW
                  iRedraw = 0
                END IF
              END IF
             
            END SUB

            Comment


            • #7
              Btw, does any of you guys know how to EASILY indent the code quoted in this forum
              Code tags. http://www.powerbasic.com/support/pb....php?do=bbcode


              Yes, you can EDIT your previous posts to insert them
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Sorry for the newbie question I used the "QUOTE" tag to edit the reply.
                Thanks again for your help Michael and Pierre !!
                Hope I could help you too sometime.

                Comment

                Working...
                X