Udostępnij za pośrednictwem


AppActivate Function

Activates an application that is already running.

Public Overloads Sub AppActivate( _ 
    ByVal { Title As String | ProcessId As Integer } _ 
)

Parameters

  • Title
    String expression specifying the title in the title bar of the application you want to activate. You can use the title assigned to the application when it was launched.

  • ProcessId
    Integer specifying the Win32 process ID number assigned to this process. You can use the ID returned by the Shell Function, provided it is not zero.

Exceptions

Exception type

Error number

Condition

ArgumentException

5

Title or ProcessId cannot be found.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

You use AppActivate to bring an application's active window into focus. You might not have a handle or a reference to the active window, or even know which window is active at a given moment. In such a case, you cannot use the Focus method.

The AppActivate function changes the focus to the named application or window but does not affect whether it is maximized or minimized. Focus moves away from the activated application window when the user takes some action to change the focus or close the window. You can use the Shell function to start an application and set the window style.

If you use the Title parameter, AppActivate uses a case-insensitive comparison but otherwise requires an exact match with the contents of the title bar. It first looks through the top-level windows and then through the child windows. If it cannot find a match, it throws an ArgumentException.

You can use AppActivate only with processes that own windows. Most console applications do not own windows, which means that they do not appear in the list of processes that AppActivate searches. When running from a console application, the system creates a separate process to run the application and returns the output to the console process. Consequently, when you request the current process ID, you get the process ID of this separate process, rather than the console application's process ID.

At run time, the AppActivate function activates any running application with a title that matches Title or with a process ID that matches ProcessId. If there is no exact match, it activates any application whose title string ends with Title. If there is more than one application named Title, the AppActivate function arbitrarily chooses one to activate.

Note

The AppActivate function requires UIPermission at the SafeTopLevelWindows level, which may affect its execution in partial-trust situations. For more information, see Requesting Permissions and UIPermission Class.

Example

This example illustrates various uses of the AppActivate function to activate an application window. If a Notepad process is not running, the example throws an ArgumentException. The Shell procedure assumes the applications are in the paths specified.

Dim notepadID As Integer 
' Activate a running Notepad process.
AppActivate("Untitled - Notepad") 
' AppActivate can also use the return value of the Shell function. 
' Shell runs a new instance of Notepad.
notepadID = Shell("C:\WINNT\NOTEPAD.EXE", AppWinStyle.NormalFocus)
' Activate the new instance of Notepad.  
AppActivate(notepadID)

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

**Module:**Interaction

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

Shell Function

Focus

ArgumentException