'Always on Top Windows 
'(Jan. 28, 2001)
'By Achilles Mina

'This demonstrates the use of API calls to put parent and child windows 
'always on top and serve as a partial workaround for Rapid-Q's limitation 
'in that regard.

'You can do as you please with this code.

DECLARE FUNCTION GetForegroundWindow Lib "user32" ALIAS "GetForegroundWindow"() As Long
DECLARE SUB SetWindowPos Lib "User32" ALIAS "SetWindowPos"(hWnd As Long, hWndInsertAfter As Long, X As Long, Y As Long, cx As Long, cy As Long, wFlags As Long)
DECLARE FUNCTION GetActiveWindow Lib "user32" ALIAS "GetActiveWindow"() As Long

DECLARE SUB ShowForm1
DECLARE SUB StageLeft
DECLARE SUB TopMost

Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2

DEFBYTE always

CREATE Form AS QFORM
  Center
  Caption = "Click to show child window"
  OnClick = ShowForm1
  OnClose = StageLeft
  Show
  CREATE Form1 AS QFORM
    Height = 100
    Width = 100
    Center
    Visible = 0
    'FormStyle = fsStayOnTop
  END CREATE
  CREATE MENU AS QMAINMENU
    CREATE Menu1 AS QMENUITEM
      Caption = "Now normal"
      OnClick = TopMost 
    END CREATE
  END CREATE 
END CREATE

DO
  DoEvents
  IF always = 1 THEN
    IF GetActiveWindow() <> Form.Handle AND GetActiveWindow() <> Form1.Handle THEN 'GetFocus () <> Form.Handle AND GetFocus() <> Form1.Handle THEN
      SetWindowPos(Form.Handle, -1, 0, 0, 0, 0,SWP_NOMOVE OR SWP_NOSIZE) 'also gives focus
    ELSEIF GetActiveWindow() = Form.Handle OR GetActiveWindow() = Form1.Handle THEN
      IF Form1.Visible = 1 AND GetForegroundWindow() = Form1.Handle THEN 
        SetWindowPos(Form1.Handle, -1, 0, 0, 0, 0,SWP_NOMOVE OR SWP_NOSIZE)
      ELSEIF Form1.Visible = 1 AND GetForegroundWindow() = Form.Handle THEN
        SetWindowPos(Form.Handle, -2, 0, 0, 0, 0,SWP_NOMOVE OR SWP_NOSIZE)
      END IF
    END IF
  END IF
LOOP

SUB StageLeft
  Application.Terminate
END SUB

SUB TopMost
  IF always = 0 THEN
    Menu1.Caption = "NOW top most"
    always = 1 
  ELSE 
    Menu1.Caption = "Now normal"
    always = 0
  END IF
END SUB

SUB ShowForm1
  Form1.Visible = 1
  IF always = 1 THEN
    SetWindowPos(Form1.Handle, -1, 0, 0, 0, 0,SWP_NOMOVE OR SWP_NOSIZE)
  ELSE
    Form1.Show
  END IF
END SUB