PRB: VB 3.0 AppActivate Fails on 32-Bit Windows NT Application

Last reviewed: June 21, 1995
Article ID: Q109262
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SYMPTOMS

Visual Basic's AppActivate statement fails to make a 32-bit application the active window under Windows NT.

To reproduce this behavior under Microsoft Windows NT, run Notepad (NOTEPAD.EXE). Then run the following code in Visual Basic:

Sub Form_Load ()
   AppActivate "Notepad - (Untitled)"
End Sub

Visual Basic fails to give focus to the Notepad session.

CAUSE

Under the 32-bit Windows NT system, 16-bit Windows-subsystem applications may not be fully available to other 16-bit programs. Visual Basic version 3.0 is a 16-bit program originally designed for the 16-bit Windows operating environment, so this behavior is by design.

WORKAROUND

To work around this behavior, use the FindWindow and SetWindowPos Windows API functions as follows:

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Double-click the form to open the code window. Select (general) from the Object box. Enter the following in the (general) (declarations) window:

       Declare Function FindWindow% Lib "USER" (ByVal Class&, ByVal Caption$)
       ' The following Declare statement must be on one, single line:
       Declare Sub SetWindowPos Lib "user" (ByVal hwnd%, ByVal hwndAfter%,
          ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal swp%)
    
    

  3. Select form from the Object box. Add the following code to the Form Click event:

       Sub Form_Click ()
    
          Const SWP_NOSIZE% = &H1
          Const SWP_NOMOVE% = &H2
          AppActivate "Notepad - (Untitled)"
          x = FindWindow(0, "Notepad - (Untitled)")
          SetWindowPos x, 0, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
          Debug.Print Hex$(x) ' Print return code from FindWindow API function.
    
       End Sub
    
    

  4. Start Notepad in Windows NT version 3.1.

  5. Start the Visual Basic program, or press the F5 key. Click the form to activate Notepad. When finished, close the form to end the Visual Basic program.

STATUS

This behavior is by design. It is under review and will be considered for enhancement in a future release of Visual Basic.


Additional reference words: 3.00
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.