Microsoft KB Archive/80186

= VB3 Killing an Application with System Menu Using VB =

Article ID: 80186

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q80186



SUMMARY
Visual Basic for Windows can use the Windows API SendMessage function to close any active window that has a system menu (referred to as 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 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: Sub Command1_Click X% = Shell(&quot;Calc.exe&quot;) End Sub

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

lpClassName$ = &quot;SciCalc&quot; lpCaption$ = &quot;Calculator&quot;

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

'* Post a message to Calc to end it's 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: Declare Function FindWindow% Lib &quot;user&quot; (ByVal lpClassName As Any,     ByVal lpCaption As Any) Declare Function SendMessage& Lib &quot;user&quot; (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: 2.00 3.00

Keywords: kbcode KB80186

-

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

© Microsoft Corporation. All rights reserved.