Functions and Directives

Function SetAppWindow() Foundation

Sets or returns the application window.

SetAppWindow( [<oXbp>] ) --> oOldWindow
<oXbp> is a window which to be used as application window.

The return value of SetAppWindow() is the window that was passed to the function. If no window is passed, the return value is either NIL (text-mode application), or it is identical with the return value of AppDesktop() (GUI application).


The function SetAppWindow() is called by default at start-up of a program within APPSYS.PRG and determines the default application window. This window is not explicitly referenced in a memory variable but can be retrieved any time in a program by calling this function.

The application window serves as default parent for all Xbase Parts that are created without a specific parent.

If an XbpCrt window is used as application window (hybrid application), it becomes the output device for text-mode screen output. This affects functions like QOut() or DispOut(), for example. In this case it must be emphasized that only one XbpCrt window can have output focus for text-oriented screen output. If multiple XbpCrt windows are required for such output, the desired window must be passed to SetAppWindow() prior to calling an output function.

For device-specific text output, like @...SAY or DevOut(), an XbpCrt window becomes the output device only if SET DEVICE TO SCREEN is set when calling SetAppWindow(). If the printer is defined as output device using SET DEVICE TO PRINTER device-specific output is still directed to the printer.

Determine output focus with multiple XbpCrt windows

// In the example, two windows are created. In the main window a 
// pushbutton is displayed which transfers output focus from one 
// window to the next. This occurs in the UDF ChangeFocus(), 
// which is executed from the Activate code block of the 
// pushbutton. After pressing a key, the event code for the key 
// is displayed in the window which has output focus. 

#include "" 

   LOCAL nEvent, mp1, mp2, oXbp, oButton, aWindow[3], i := 1 

   SetMouse( .T. )                  // process mouse events 
   SetColor( "N/W" )                // fill window pale gray 
   ? "Press any key..." 
                                    // store window in array 
   aWindow[1] := SetAppWindow()     // parent window 
   aWindow[1]:setTitle( "Parent" ) 

   // child window within parent window 
   aWindow[2] := XbpCrt():new( aWindow[1], NIL, ; 
                               { 50, 100 }, 10, 45, "Child A" ) 

   // child window outside of parent window 
   aWindow[3] := XbpCrt():new( NIL, NIL, ; 
                               { 300, 300 }, 15, 45, "Child B" ) 

   oButton := XbpPushButton():new (,, {25, 10}, {130, 30} ) 
   oButton:caption      := "Output focus" 
   oButton:pointerFocus := .F.      // pushbutton to change 
   oButton:create()                 // the output focus 
   oButton:activate := ; 
      {|mp1,mp2,self| ChangeFocus( aWindow, @i ), self:show() } 

   nEvent := 0                      // event loop 
   DO WHILE nEvent <> xbeP_Close 
      nEvent := AppEvent( @mp1, @mp2, @oXbp, 0 ) 

      IF nEvent == xbeK_ESC         // terminate on Esc 

      ELSEIF nEvent < xbeB_Event    // output in the window 
                                    // with focus 
         ? "The key has the event code:", nEvent 

      ELSEIF oXbp <> NIL            // event for pushbutton 
         oXbp:handleEvent( nEvent, mp1, mp2, oXbp ) 


// change output focus 
PROCEDURE ChangeFocus( aWindow, nIndex ) 
   IF ++nIndex > Len(aWindow) 
      nIndex := 1 

   SetAppWindow( aWindow[nIndex] )  // set output focus 
   CLS                              // delete window contents 
   ? "Press any key..." 

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.