' This program detects if a previous instance of itself is already active.
' For example, you may not want the user to load 2 instances of your program.
' If found, the previous instance is brought to the foreground, even if it
' was minimized it should restore itself.

' Run this program twice to see the affect

CONST rqClassName = "TForm"        '-- Classname for all Rapid-Q programs
CONST myFormName = "Whatever"


DECLARE FUNCTION FindWindow LIB "USER32" ALIAS "FindWindowA" _
                 (className AS STRING, windowName AS STRING) AS INTEGER
DECLARE SUB SetForegroundWindow LIB "USER32" ALIAS "SetForegroundWindow" _
                 (HWnd AS LONG)
DECLARE SUB ShowWindow LIB "USER32" ALIAS "ShowWindow" _
                 (HWnd AS LONG, nCmd AS LONG)

DEFINT hWnd = FindWindow(rqClassName, myFormName)

IF hWnd THEN
    ShowMessage ("A previous instance was detected")
    SetForegroundWindow(hWnd)
    ShowWindow(hWnd, 1)
    END
END IF


CREATE Form AS QFORM
    Caption = myFormName
    ShowModal
END CREATE
