Topic: Behaviour of SetCurrentWindow() in Appgraphics

I'm trying to restore an application from the past millennium, which includes handling input/output on a console, as well as intermittent plotting. In other words, it is necessary to switch back and forth (changing focus) between console and plot windows several times.

Unfortunately, the SetCurrentWindow() subroutine does not seem to work quite as described in the documentation, as can be seen from the example below. Upon initiation, the graphic window becomes active as expected, but after having intermittently activated the console by clicking on it, a subsequent call to SetCurrentWindow() will NOT give full focus back to the graphical window. Although the window accepts output, the title bar remains gray and a call to Getch() to read input does not work, without clicking it. Am I missing something here?

Also, it would be nice if there was a call to give focus back to the console, without clicking on the window. 

! ---------------------------------
! Testing windowing functions
! ---------------------------------
      USE Appgraphics
      INTEGER     :: GraphID,PlotWidth,PlotHeight,PXpos,PYpos,IOWait
      CHARACTER   :: GraphHeader*18
      LOGICAL     :: CloseFlag
      DATA GraphHeader/' * Plot window * '/
      DATA PlotWidth/800/,PlotHeight/600/,PXpos/600/,PYpos/50/ ! Size and location in pix
      DATA CloseFlag/.FALSE./ ! I want to close myself
!           Ext. console selected in project options. Write something.
      WRITE (*,1000)
!           Now open plot window and plot something
      GraphID = InitWindow(PlotWidth, PlotHeight, GraphHeader,PXpos,PYpos,Closeflag)
      CALL Setcolor(GREEN)
      CALL Circle(400,200,100)
!           Now I would want to bring ext. console to front again and write ID of plot window
!     CALL SetCurrentWindow(How?)
      WRITE (*,2000) GraphID
!           Userwait in ext. console     
      CALL SetCurrentWindow(GraphID) ! Re-focus, bring plot window to front and plot some more
      CALL Circle(400,200,150)
!     Use Getch for Userwait in plot window
      CALL OutTextXY(300,500,' Hit a key to close this window ')
      CALL CloseWindow(GraphID)
      READ(*,*) ! To ensure pause when run as executable
1000 FORMAT(//,20X,' Hello world!',///)
2000 FORMAT(   20X,' GraphID = ',I3,' Hit a key...')

Re: Behaviour of SetCurrentWindow() in Appgraphics

You're correct, setcurrentwindow() is documented as bringing the window to the foreground if the application is the current foreground application.  I'm not surprised that issues arise when trying to use the feature with a console window, however.   The console is created and managed by the operating system, so it does represent a "special case," and AppGraphics isn't actually aware of it.

Regardless, I think the graphics window should still move to the foreground.  I'll have to see why it isn't happening.

Bringing the console to the foreground, though, is a little more complicated.  We'll need to use two Windows API calls to make it happen.  This routine should bring the console to the foreground:

    subroutine console_to_foreground()
    use iso_c_binding
    implicit none
            function GetConsoleWindow() bind(c, name="GetConsoleWindow")
            use iso_c_binding
            end function GetConsoleWindow
        end interface
            function SetForegroundWindow(ptr) bind(c, name="SetForegroundWindow")
            use iso_c_binding
                type(c_ptr), value::ptr
            end function SetForegroundWindow
        end interface
        if(SetForegroundWindow(GetConsoleWindow())) then
            Print *, "Console in front"
            Print *, "Console to front failed"
        end if
    end subroutine console_to_foreground

It compiles fine in 64-bit mode, but there is some unpleasantness needed for 32-bit mode.

I'll look into why setcurrentwindow is behaving the way it is, and we'll get a fix in this week.

Jeff Armstrong
Approximatrix, LLC

Re: Behaviour of SetCurrentWindow() in Appgraphics

We've got a fix in for cases where the foreground window is the console.  It should be in the next release, which will hopefully be within the next week or so.

Jeff Armstrong
Approximatrix, LLC

Re: Behaviour of SetCurrentWindow() in Appgraphics

Great! Thanks for the help!