|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
We set blnChoose to False when the form first loads:
Private Sub Form_Load()
blnChoose = False
End Sub
To capture the mouse, well need SetCapture and ReleaseCapture. To convert from window to screen coordinates, well need ClientToScreen, so we declare those functions as well:
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As _
Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As _
Long, ByVal yPoint As Long) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long,_
lpPoint As POINTAPI) As Long
We also need to declare the POINTAPI type because ClientToScreen uses that type, so we add that declaration to a module we add to the program:
Type POINTAPI
x As Long
y As Long
End Type
Now when the user clicks the command button, we capture the mouse and set the blnChoose flag to True:
Private Sub Command1_Click()
blnChoose = True
intRetVal = SetCapture(hwnd)
End Sub
When the user clicks a window, our programs MouseDown event handler will be called. To determine the screen location at which the mouse went down, we use ClientToScreen:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As _
Single, y As Single)
Dim ptPoint As POINTAPI
If blnChoose Then
ptPoint.x = x
ptPoint.y = y
retval = ClientToScreen(hwnd, ptPoint)
...
End If
End Sub
Now we can get a window handle for the window the user clicked with WindowFromPoint:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y_
As Single)
Dim window As Long
Dim ptPoint As POINTAPI
If blnChoose Then
ptPoint.x = x
ptPoint.y = y
retval = ClientToScreen(hwnd, ptPoint)
window = WindowFromPoint(ptPoint.x, ptPoint.y)
...
End If
End Sub
And thats itnow weve gotten a window handle for any window on the screen the user wants to click. Using that handle you can perform all kinds of operations on the windowresizing it, closing it, changing its style, and more, using API functions. As an example, well see how to get a windows title bar text in the next topic. Getting A Windows TextIn the previous topic, we saw how to get a handle for any window on the screen when the user clicks that window. Here, well use the GetWindowText function to get the title bar text of a window:
Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd _
As Long, ByVal lpszString As String, ByVal cch As Long) As Long
Heres what the arguments to this function mean:
This function returns the length of the text it retrieved. Lets see an example. Here, we use the window handle we got in the previous topic with GetWindowText to get the title bar text of any window the user clicks and display that text in a text box, Text1:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As _
Single, y As Single)
Dim window As Long
Dim buffer As String * 1024
Dim ptPoint As POINTAPI
If blnChoose Then
ptPoint.x = x
ptPoint.y = y
retval = ClientToScreen(hwnd, ptPoint)
window = WindowFromPoint(ptPoint.x, ptPoint.y)
lngRetVal = GetWindowText(window, buffer, 1024)
Text1.Text = buffer
End If
End Sub
Run the program now, as shown in Figure 23.9. When you click the Choose Window button and then click another window, the program reads the text in the clicked programs title bar and displays it in a text box, as shown in Figure 23.9. Our program is a successnow we can work with any window on the screen.
The code for this example is located in the windowinfo folder on this books accompanying CD-ROM. Playing Sounds With API FunctionsYou can use the Windows API PlaySound function to play sounds:
Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As _
String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Heres what the arguments to PlaySound mean:
Here are the flags you can use in the dwFlags parameter of the PlaySound function:
Declaring this function as a Private function lets us declare this in the (General) section of a form:
Private Declare Function PlaySound Lib "winmm.dll" Alias _
"PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |