Getting Windows string data
You’re already familiar not only with the Windows Way of getting string data but also with the Basic Way of getting around the Windows Way (explained in “Dealing with Strings,” page 72). For example, you can get the class name of a window with GetClassName; to make this look Basic, wrap the access in ClassNameFromWnd:
Function ClassNameFromWnd(ByVal hWnd As Long) As String
Dim sName As String, cName As Integer
BugAssert hWnd <> hNull
sName = String$(80, 0)
cName = GetClassName(hWnd, sName, 80)
ClassNameFromWnd = Left$(sName, cName)
End Function
The window class indicates the kind of window. The main use for the class name is passing it to FindWindow to find other windows of the same class. (The VBFindWindow function was discussed in “Wrapping String Functions,” page 78, as an example of how to pass strings to Windows functions.) You can also pass the class name to GetClassInfo to obtain detailed class information that you aren’t likely to use in Visual Basic. The following sidebar has more information on classes.
In the section “Wrap It Up” in Chapter 2, you saw an example of the technique for getting strings, using WindowTextFromWnd. Actually, WinWatch uses a slightly different version of this function, named WindowTextLineFromWnd. It’s the same function except that it truncates the window text at the first line break. I originally used the WindowTextFromWnd function in WinWatch, but I wasn’t counting on windows that use the window text as the content of the window. The WinWatch display needs to truncate multiline window text, but other applications might need to display the entire text, breaks and all.
The window title is useful not only as information but also to pass to the AppActivate statement. This is the Basic Way to set the focus to a particular window. The Windows Way is to pass the window handle to SetForegroundWindow.