800 804




Visual Basic 6 Black Book:Connecting To The Windows API And Visual C++
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




We set blnChoose to False when the form first loads:


Private Sub Form_Load()
blnChoose = False
End Sub


To capture the mouse, we’ll need SetCapture and ReleaseCapture. To convert from window to screen coordinates, we’ll 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 program’s 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 that’s it—now we’ve 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 window—resizing it, closing it, changing its style, and more, using API functions. As an example, we’ll see how to get a window’s title bar text in the next topic.

Getting A Window’s Text
In the previous topic, we saw how to get a handle for any window on the screen when the user clicks that window. Here, we’ll 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


Here’s what the arguments to this function mean:


•  hwnd—The handle of the window you want to get the text from.
•  lpszString—The buffer that is to receive the copied string of the window’s title.
•  cch—The maximum number of characters to be copied to the buffer. If the string is longer than the number of characters specified in cch, it is truncated.

This function returns the length of the text it retrieved. Let’s 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 program’s title bar and displays it in a text box, as shown in Figure 23.9. Our program is a success—now we can work with any window on the screen.


Figure 23.9  Getting a window’s title bar text.
The code for this example is located in the windowinfo folder on this book’s accompanying CD-ROM.

Playing Sounds With API Functions
You 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


Here’s what the arguments to PlaySound mean:

•  lpszName—The path and name of the file to play
•  hModule—The module handle of the program, which you usually set to 0
•  dwFlags—Flag settings; see the following explanation for the settings

Here are the flags you can use in the dwFlags parameter of the PlaySound function:

•  SND_SYNC—&H0; play the sound synchronously (the default)
•  SND_ASYNC—&H1; play the sound asynchronously
•  SND_NODEFAULT—&H2; silence is not the default, if sound is not found
•  SND_MEMORY—&H4; lpszName points to a memory file
•  SND_ALIAS—&H10000; name is a win.ini [sounds] entry
•  SND_FILENAME—&H20000; name is a file name
•  SND_RESOURCE—&H40004; name is a resource name or atom
•  SND_ALIAS_ID—&H110000; name is a win.ini [sounds] entry identifie
•  SND_ALIAS_START—0; must be > 4096 to keep strings in same section of resource file
•  SND_LOOP—&H8; loop the sound until next PlaySound
•  SND_NOSTOP—&H10; don’t stop any currently playing sound
•  SND_NOWAIT—&H2000; don’t wait if the driver is busy

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






Previous
Table of Contents
Next






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.



Wyszukiwarka

Podobne podstrony:
NumPath 800 Bick 15V M561 87
Życie umysłowe i kulturalne w Polsce epoki oświecenia ~800
Serwisowka Kody Komputera Rover 100; 200; 400; 600; 800 [D]
800 zł na taką emeryturę może liczyć przedsiębiorca
Catane 800 EC
Fagor 800 TGi [CHC] L841 85m
INSTRUKCJA OBSŁUGI MODEM ASDL SAGEM USB FAST 800 840 PL
2 Sprzętowa i programowa synteza układów sterowania logicznegoid 804
Pilot radiowy do PVR 800 PVR800TCK
MJW 800 karta katalogowa
LECTURE 4 Anglo Saxons@0 800?
NumPath 800 PC50 M584 87
index (804)
7 45 800
Readme (800)

więcej podobnych podstron