'===========================================================================
' Subject: MOVE WINDOW AROUND SCREEN          Date: 03-22-00 (15:40)       
'  Author: Dieter Folger                      Code: PB                     
'  Origin: folger@bnv-bamberg.de            Packet: PB.ABC
'===========================================================================
$IF 0
--------------------------------------------------------------------------
 This program demonstrates how to move a window around the screen using
 the cursor keys. It can be compiled with PowerBasic 3.0 and newer (or
 with 2.1 if you emulate VIEW TEXT).
 Moving a window takes three steps:
   1. Get direction to move
   2. Restore old background at old place
   3. Write window at new position
 We do not restore the whole window-sized background but only the part that
 becomes visible after the window moved. That means, only one row or column
 must be restored. This is more difficult to do for the programmer, but much
 faster. Else the screen could be flickering when the window is being moved.
 You can end the program with ESC or RETURN (try both).
 MOVEWIND.BAS is freeware (c) 1996 by Dieter Folger
--------------------------------------------------------------------------
$ENDIF
DEFINT A-Z
COLOR 7,1: CLS
'Fill screen with "°":
PRINT STRING$(25*80,176);
COLOR 14,3
'To have some text on screen:
LOCATE 12,27
PRINT "   This is the background  "
Col% = 25 : Row% = 5 : Cols% = 30 : Rows% = 5
MovableWindow Row%,Col%,Rows%,Cols%
END
'-----------------------------------------------
SUB MovableWindow(Row%,Col%,Rows%,Cols%) PUBLIC
'-----------------------------------------------
 'Save whole screen:
 DEF SEG=&hB800:FullScreen$=PEEK$(0,4000):DEF SEG
 'Draw window and put something in it:
 VIEW TEXT(Col%,Row%)-(Col%+Cols%,Row%+Rows%)
 COLOR 15,4:CLS
 FOR i% = 1 TO 6
     LOCATE i,1:PRINT STRING$(31,17+i%);
 NEXT
 LOCATE 3,6:PRINT "   Movable Window    "
 LOCATE 5,6:PRINT "   Use cursor keys   "
 'Save window screen:
 SaveWindow Wind$, Row%, Col%, Rows%, Cols%
 DO
    DO : K$= INKEY$ : LOOP UNTIL LEN(K$)
    IF LEN(K$) = 2 THEN K$ = RIGHT$(K$,1)
    K%=ASC(K$)
    SELECT CASE K%
	   CASE 13, 27   'finished
                IF K% = 27 THEN
                   'Remove window:
                   DEF SEG = &HB800
                   POKE$ 0,FullScreen$
                   DEF SEG
                   EXIT SUB
                END IF
                Done% = 1
	   CASE 72       'up
	        IF Row% > 1 THEN DECR Row% : d%= 1
	   CASE 80       'down
		IF Row% + Rows% < 25 THEN INCR Row% : d%=2
	   CASE 75       'left
		IF Col% > 1 THEN DECR Col% : d%=3
	   CASE 77       'right
		IF Col% + Cols% < 80 THEN INCR Col% :d%=4
    END SELECT
    Movewindow FullScreen$, Wind$, Row%, Col%, Rows%, Cols%, d%
LOOP UNTIL Done%
END SUB
'-----------------------------------------------------------
SUB MoveWindow(Scrn$, Win$, Row%, Col%, Rows%, Cols%, d%) PUBLIC
'-----------------------------------------------------------
 F$=Scrn$
 Length%=Cols%*2+2
 DEF SEG = &HB800
 'Restore background:
 SELECT CASE d%
        CASE 1 : 'up
           Start%=(Row%+Rows%)*160+Col%*2-2
           S$=MID$(F$,Start%+1,Length%)
           POKE$ Start%,S$
        CASE 2 : 'down
          Start%=(Row%-2)*160+Col%*2-2
          S$=MID$(F$,Start%+1,Length%)
          POKE$ Start%,S$
        CASE 3 'left
          FOR i%=Row% TO Row%+Rows%
              Start%=(i%-1)*160+Col%*2-2
              S$=MID$(F$,Start%+Length%+1,2)
              POKE$ Start%+Length%,S$
          NEXT
        CASE 4 'right
          FOR i%=Row% TO Row%+Rows%
              Start%=(i%-1)*160+(Col%-1)*2-2
              S$=MID$(F$,Start%+1,2)
              POKE$ Start%,S$
          NEXT
 END SELECT
 'Draw window again:
 W$=Win$
 FOR i%=Row% TO Row%+Rows%
     Start%=(i%-1)*160+Col%*2-2
     Ins$=Left$(W$,Length%)
     W$=MID$(W$,Length%+1)
     POKE$ Start%,Ins$
 NEXT
 DEF SEG
END SUB
'--------------------------------------------------
SUB SaveWindow(Scr$, Row%, Col%, Rows%, Cols%) PUBLIC
'--------------------------------------------------
 DEF SEG=&HB800 : Scr$=""
 Length%=Cols%*2+2
 FOR i%=Row% TO Row%+Rows%
     Start%=((i%-1)*160)+Col% * 2 - 2
     S$=PEEK$(Start%,Length%)
     Scr$=Scr$+S$
 NEXT
 DEF SEG
END SUB
'--- eof ------------------------------------------
