--------------------------------------------------------------------- The information in this article applies to: - Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0 - Microsoft Visual Basic programming system for Windows, version 1.0 -------------------------------------------------------------------- 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: 1. Create a form called Form1. 2. Create two command buttons called Command1 and Command2. 3. Within the Command1 Click event, add the following code: Sub Command1_Click() X% = Shell("Calc.exe") End Sub 4. Within the Command2 Click event, add the following code: 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 it's existence. X& = SendMessage(Handle, WM_SYSCOMMAND, SC_CLOSE, NILL) End Sub 5. 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 "user" (ByVal lpClassName As Any, ByVal lpCaption As Any) Declare Function SendMessage& Lib "user" (ByVal hwnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam As Long) 6. Run the program. Click the Command1 button to bring up an instance of the Calculator program. Click the Command2 button to close the window. Additional reference words: 1.00 2.00 3.00 KBCategory: kbprg kbcode KBSubcategory: APrgOther How to Get a Window's Class Name and Other Window Attributes Article ID: Q112649 --------------------------------------------------------------------- The information in this article applies to: - Standard and Professional Editions of Visual Basic for Windows, version 3.0 --------------------------------------------------------------------- SUMMARY ======= You could use SPY.EXE, which comes with Microsoft Visual C/C++, to get information such as a window's class name. However, this article shows by example how you, the Visual Basic programmer, can create your own Visual Basic application that does the same thing -- displays a window's class name along with several other attributes. You can use this Visual Basic application to find a window's class name anytime you need it. For example, you might need a window's class name for use in a function in the Microsoft Windows Application Programming Interface (Windows API). MORE INFORMATION ================ This example uses several functions from the Windows API to get information about the window the cursor is currently over. First, the routine calls GetCursorPos to get the current position of the cursor. Then it calls WindowFromPoint to get the handle to the window the cursor is currently over. Then it calls several other functions in the Windows API to get specific information pertaining to the window. The example finds the following information about the window: - Window Handle - Window Text - Window Class Name - Window Style - Window ID Number - Parent Window Handle (if applicable) - Parent Window Text (if applicable) - Parent Window Class Name (if applicable) - Module File Name The example can be easily expanded to get other window attributes by calling appropriate functions in the Windows API. Step-by-Step Example -------------------- >>> Continued to next message * SLMR 2.1a * Nothing is so simple that it can't be screwed up. --- SLMAIL v4.5a (#4334) * Origin: TEST FOR ECHO BBS - Middleburg Hts., OH (216) 234-6088 (1:157/438) --------------- FIDO MESSAGE AREA==> TOPIC: 178 VISUAL BASIC Ref: EAY00028 Date: 06/27/97 From: KURT J. TISCHER Time: 05:24am \/To: MIKE JOHNSON (Read 6 times) Subj: VB3 3/7 >>> Continued from previous message This example creates a Visual Basic program that produces results similar to those produced by SPY.EXE. 1. Start a new project in Visual Basic. Form1 is created by default. 2. Add the following declarations to the general declarations section of Form1: ' Enter each of the following Declare statements on one, single line: Declare Sub GetCursorPos Lib "User" (lpPoint As Long) Declare Function WindowFromPoint Lib "User" (ByVal ptScreen As Any) As Integer Declare Function GetModuleFileName Lib "Kernel" (ByVal hModule As Integer, ByVal lpFilename As String, ByVal nSize As Integer) As Integer Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer Declare Function GetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Long Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As Integer Declare Function GetClassName Lib "User" (ByVal hWnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer Declare Function GetWindowText Lib "User" (ByVal hWnd As Integer, ByVal lpString As String, ByVal aint As Integer) As Integer Const GWW_HINSTANCE = (-6) Const GWW_ID = (-12) Const GWL_STYLE = (-16) 3. Add a timer control (Timer1) to the form. 4. Set the interval property of the timer to 200. 5. Add the following code to timer's timer event: Sub Timer1_Timer() Dim ptCursor As Long Dim sWindowText As String * 100 Dim sClassName As String * 100 Dim hWndOver As Integer Dim hWndParent As Integer Dim sParentClassName As String * 100 Dim wID As Integer Dim lWindowStyle As Long Dim hInstance As Integer Dim sParentWindowText As String * 100 Dim sModuleFileName As String * 100 Static hWndLast As Integer Call GetCursorPos(ptCursor) ' Get cursor position hWndOver = WindowFromPoint(ptCursor) ' Get window cursor is over If hWndOver <> hWndLast Then ' If changed update display hWndLast = hWndOver ' Save change Cls ' Clear the form Print "Window Handle: &H"; Hex(hWndOver) ' Display window handle r = GetWindowText(hWndOver, sWindowText, 100) ' Window text Print "Window Text: " & Left(sWindowText, r) r = GetClassName(hWndOver, sClassName, 100) ' Window Class Print "Window Class Name: "; Left(sClassName, r) lWindowStyle = GetWindowLong(hWndOver, GWL_STYLE) ' Window Style Print "Window Style: &H"; Hex(lWindowStyle) ' Get handle of parent window: hWndParent = GetParent(hWndOver) ' If there is a parent get more info: If hWndParent <> 0 Then ' Get ID of window: wID = GetWindowWord(hWndOver, GWW_ID) Print "Window ID Number: &H"; Hex(wID) Print "Parent Window Handle: &H"; Hex(hWndParent) ' Get the text of the Parent window: r = GetWindowText(hWndParent, sParentWindowText, 100) Print "Parent Window Text: " & Left(sParentWindowText, r) ' Get the class name of the parent window: r = GetClassName(hWndParent, sParentClassName, 100) Print "Parent Window Class Name: "; Left(sParentClassName, r) Else ' Update fields when no parent: Print "Window ID Number: N/A" Print "Parent Window Handle: N/A" Print "Parent Window Text : N/A" Print "Parent Window Class Name: N/A" End If ' Get window instance: hInstance = GetWindowWord(hWndOver, GWW_HINSTANCE) ' Get module file name: r = GetModuleFileName(hInstance, sModuleFileName, 100) Print "Module: "; Left(sModuleFileName, r) End If End Sub 6. Save the project files. 7. Run the program, and move the mouse over different windows. You will see the field values change as you move the mouse over different windows. REFERENCES ========== - Microsoft Windows Software Development Kit (SDK) - Microsoft Visual Basic for Windows SDK Help file Additional reference words: ClassName FindWindow 3.00 KBCategory: kbui kbprg kbcode KBSubcategory: APrgWindow How to Get a Window Handle Without Specifying an Exact Title Article ID: Q113475 --------------------------------------------------------------------- The information in this article applies to: - Standard and Professional Editions of Microsoft Visual Basic for Windows Programming Systems, versions 2.0 and 3.0 - Microsoft Visual Basic for Windows Programming System, version 1.0 --------------------------------------------------------------------- SUMMARY ======= The Visual Basic AppActivate command can only activate a window if you know the exact window title. Similarly, the Windows FindWindow function can only find a window handle if you know the exact window title. >>> Continued to next message * SLMR 2.1a * Nothing is so simple that it can't be screwed up. --- SLMAIL v4.5a (#4334) * Origin: TEST FOR ECHO BBS - Middleburg Hts., OH (216) 234-6088 (1:157/438) --------------- FIDO MESSAGE AREA==> TOPIC: 178 VISUAL BASIC Ref: EAY00029 Date: 06/27/97 From: KURT J. TISCHER Time: 05:24am \/To: MIKE JOHNSON (Read 6 times) Subj: VB3 4/7 >>> Continued from previous message This article demonstrates how to search for a window that has a title that is like the title you specify -- but not an exact match. The sample code searches through the available windows, comparing the window titles to a pattern by using the Visual Basic Like operator. You can also use the sample code to find a window based on its class name or ID. This can be extremely helpful when you need to send keystrokes to other Applications. MORE INFORMATION ================ FindWindowLike Function ----------------------- The sample code provides a find window function named FindWindowLike. FindWindowLike does a recursive search for windows matching the description you give it. After completing the search, FindWindow returns the number of windows it found that match your description. It also returns then window handles in an array that you pass to it. Once you have a window handle, you can call many Windows API functions to manipulate it. For example, you could set the focus to it, or move it. The example in this article shows how to set the focus. Parameters Passed to FindWindowLike Function