Microsoft KB Archive/142817

= How to Kill an Application with System Menu Using Visual Basic =

Article ID: 142817

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition

-



This article was previously published under Q142817



SUMMARY
Microsoft Visual Basic for Windows can use the Microsoft Windows API SendMessage function to close any active window that has a system menu (referred to as the Control Box within Visual Basic for Windows) with the Close option.



MORE INFORMATION
You can use the Windows API SendMessage function to post a message to any window in the environment as long as the handle to the window is known. You can use the API FindWindow function to determine the handle associated with the window the user wants to close.

Query on the following words in the Microsoft Knowledge Base for more information on the FindWindow function:

FindWindow and Visual Basic

To create a program to close an occurrence of the Microsoft Windows version 3.0 Calculator program, do the following:

 Create a form called Form1. Create two command buttons called Command1 and Command2.  Within the Command1 Click event, add the following code: Private Sub Command1_Click X% = Shell("Calc.exe") End Sub

  Within the Command2 Click event, add the following code: Private Sub Command2_Click Const NILL = 0& Const WM_SYSCOMMAND = &H112 Const SC_CLOSE = &HF060

lpClassName$ = "SciCalc" lpCaption$ = "Calculator"

'* Determine the handle to the Calculator window. Handle = FindWindow(lpClassName$, lpCaption$)

'* Post a message to Calc to end its existence. X& = SendMessage(Handle, WM_SYSCOMMAND, SC_CLOSE, NILL)

End Sub

  In the Declarations section, declare the following two API functions: ' Enter each of the following Declare statements on one, single line: Private Declare Function FindWindow% Lib "user" _ (ByVal lpClassName As Any, ByVal lpCaption As Any) Private Declare Function SendMessage& Lib "user" _ (ByVal hwnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam As Long)

 Run the program. Click the Command1 button to bring up an instance of the Calculator program. Click the Command2 button to close the window.</ol>

Additional query words: 1.00 2.00 3.00 4.00 vb4win vb416

Keywords: KB142817

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.