Examples: %If directive

This example compiles and runs in either Windows 3.1, Windows NT, or Windows 95. Depending on whether the application is compiled and run under 16-bit Windows (Windows 3.1) or 32-bit Windows (Windows 95 or Windows NT), you should declare and use an appropriate Windows handle variable and the appropriate version of two Windows API functions.

GetActiveWindow returns the handle (an Integer in 16-bit Windows, a Long in 32-bit Windows) of the currently active window. GetWindowText returns the text in the window title bar.

Dim winTitle As String * 80
%If WIN16                      ' 16-bit Windows
   Dim activeWin As Integer    ' Window handles are Integer.
   Declare Function GetActiveWindow% Lib "User" ()
   Declare Function GetWindowText% Lib "User" _
         (ByVal hWnd%, ByVal lpstr$, ByVal i%)
%ElseIf WIN32                  ' 32-bit Windows
   Dim activeWin As Long       ' Window handles are Long.
   Declare Function GetActiveWindow& Lib "User32" ()
   Declare Function GetWindowText% Lib "User32" _
          Alias "GetWindowTextA" _
      (ByVal hWnd&, ByVal lpstr$, ByVal i&)
%End If
' Print the name of the currently active window.
activeWin = GetActiveWindow()  ' Returns an Integer or a Long.
Call GetWindowText(ActiveWin, winTitle$, 80)
Print winTitle$