773 776




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




Chapter 23Connecting To The Windows API And Visual C++


If you need an immediate solution to:
Getting Or Creating A Device Context (Including The Whole Screen)
Drawing Lines In A Device Context
Drawing Ellipses In A Device Context
Drawing Rectangles In A Device Context
Setting Drawing Colors And Styles (Using Pens)
Setting Drawing Modes (ROP2)
Handling The Mouse Outside Your Program’s Window
Copying Bitmaps Between Device Contexts Quickly
Capturing Images From The Screen
Getting A Window Handle For Any Window On The Screen
Getting A Window’s Text
Playing Sounds With API Functions
Allocating Memory And Storing Data
Reading Data From Memory And Deallocating Memory
Making A Window Topmost
Determining Free And Total Disk Space
Determining The Windows Directory
Connecting To Visual C++

In Depth
This is our chapter on connecting Visual Basic directly to the Windows Application Programming Interface (API) and to Visual C++. There are literally thousands of functions and subroutines waiting for us to use in Windows, and we can reach them with the techniques in this chapter. With these Windows procedures, you can do things you just can’t do in Visual Basic any other way. For example, we’ll see how to draw anywhere on the screen (including outside our program’s window), capture the screen, capture the mouse (so we get all mouse events even when the mouse is outside our window), play sounds directly, allocate and use memory, make fast bitmap copies, interrogate other windows about their contents, determine free space on a disk drive, make a window “topmost” (so it stays on top of all other windows), and much more.

We can connect to the Windows API because the procedures that make up that API are in dynamic link libraries in the windows\system directory, and we can call them directly from those DLLs. Here’s a list of the core Windows DLLs of the kind we’ll be using in this chapter:

•  Advapi32.dll—Advanced API Services library supporting numerous APIs including many security and Registry calls
•  Comdlg32.dll—Common Dialog API library
•  Gdi32.dll—Graphics Device Interface API library
•  Kernel32.dll—Core Windows 32-bit base API support
•  Lz32.dll—32-bit compression routines
•  Mpr.dll—Multiple Provider Router library
•  Netapi32.dll—32-bit Network API library
•  Shell32.dll—32-bit Shell API library
•  User32.dll—Library for user interface routines
•  Version.dll—Version library
•  Winmm.dll—Windows Multimedia library

Besides connecting our code to the Windows API, we’ll also see how to connect our code to Visual C++. In fact, that process works much like connecting to the Windows API, because in order to reach Visual C++ code, you place that code into a dynamic link library and then call it in the same way you call Windows API code.

So how do you actually connect code in a DLL to Visual Basic? We’ll find out in the next section.
Declaring And Using DLL Procedures In Visual Basic
Let’s say you want to play sounds directly, without using the Visual Basic multimedia control. You can do that with the Windows API PlaySound function. To inform Visual Basic where to find this function (it’s stored in the winmm.dll dynamic link library), what arguments it takes, and what arguments it returns, you declare that function like this in the (General) declarations 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


Note that we’ve declared this function as private to be able to declare it in a form. If you omit the Private keyword, you must make declarations in a module. After you’ve declared the function, you’re free to use it, like this where we play the file C:\windows\media\Tada.wav (which is one of the files that come with Windows):


Private Sub Command1_Click()
retVal = PlaySound("c:\windows\media\Tada.wav", 0&, &H20000)
End Sub


There are a number of points to notice here; because most Windows procedures are functions, you have to provide a way to handle the return value, which we do by storing it in the retVal variable shown in the preceding code. We’ll discard most of these return values, but Visual Basic will give you an error unless you handle functions as we’ve done in the preceding code. For subroutines, which do not return a value, you use the Call keyword like this:


Call MoveMemory(hMemoryPointer, outbuffer, DataLength)


Now look at the declaration we’ve made for PlaySound:


Private Declare Function PlaySound Lib "winmm.dll" Alias _
"PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, _
ByVal dwFlags As Long) As Long


This declaration comes from a file that comes with Visual Basic (in the Common\tools\winapi directory) named win32api.txt, which we’ll see more about in a minute. Here, that declaration indicates to Visual Basic that this function is to be found in the winmm.dll file (which is in the windows\system directory).

The Alias clause, if there is one, gives the actual name of the Windows function. Here, what Visual Basic declares as PlaySound is actually the Windows function PlaySoundA. This function, PlaySoundA, uses ANSI text strings (as opposed to other versions of PlaySound, which can use other text formats like Unicode), which is what you need to work with Visual Basic. In general, you should use the Windows functions as declared in the win32api.txt file, because the designers of Visual Basic have already selected the functions that will work with Visual Basic in that file.



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:
index (776)
771 773
776 778
776 778
776 779
mbdch20 776
773 778
Dz U 2010 115 773 zmiana z dnia 2010 06 22
mbdch20 773

więcej podobnych podstron