Using Windows API Functions in Your ApplicationsThere are many ways to use the Windows API functions in your applications. One set of API functions you will use often when you start writing advanced Windows applications are the API functions that deal with getting information about a window or set of windows. Every window in the Windows operating system is identified by a handle. The desktop window has a handle, a Visual Basic form displayed in an application has a handle, and even the controls on a form, which are themselves actually windows, have handles. You can gather a lot of information about the windows in your application after you get the handle of the window that interests you. To get the handle of the active window on your desktop, you need to use the function GetActiveWindow. This function returns the handle of the active window. To use this function in your application, add the following line to a standard module: |
|
Declare Function GetActiveWindow Lib "user32" () As Long
With this function declared, you can now add code to your application to call the function. For example, to use this function in a simple application, create a form with one command button on it. Add the following code to the Command button's Click event: | |
Dim lonHwnd As Long lonHwnd = GetActiveWindow& MsgBox "Active Window Handle: " & Str$(lonHwnd)
When you run this application and click the
Command button, you see the message box shown in Figure 27.5.
Getting the handle of the active window. After you get the handle of a window, you can use that information to do other things with the windows on your desktop using the Windows API. For example, to make a window the active window, you can pass its handle to the SetActiveWindow& function, which makes active the window associated with whichever handle it is passed. The Declare statement for SetActiveWindow& is Declare Function SetActiveWindow Lib "user32" (ByVal hWnd As Long) As Long You can add this function to your code to make a window the active window: Dim lonStatus as Long lonStatus = SetActiveWindow(lonHwnd) The argument lonHwnd is the handle of a window, which was acquired through another Windows API function such as GetActiveWindow&. The variable lonStatus is used because the SetActiveWindow& function returns a long integer that represents the handle of the previously active window.
The preceding examples give only a hint of the power of the Windows API functions. Every aspect of the Windows environment, from the Windows environment to handling hardware such as printers and disk drives, can be controlled to a greater or lesser extent using Windows API functions. Taking the time to learn these functions can help you gain greater control over your applications' environment, and you can often achieve significant efficiency gains using the API functions in your applications.
|
|
![]() |
![]() |
|||