So far you’ve been programming within the limits of Visual Basic. You’ve designed with point-and-click operations and used Visual Basic statements to program the controls on the Forms. You’ve seen how far you can go with Visual Basic, and there’s a lot you can do with straight Visual Basic code. Sometimes, however, you must get into the core of the operating system and access of its many functions. To do advanced programming, such as detecting mouse movements outside a Form, you need some additional functions. These functions are provided by the Win32 Application Programming Interface (APT).
The Win32 API is a set of functions developed primarily for C programmers, but there’s no reason why you can’t use it from within your Visual Basic applications. Many Visual Basic programmers resort to the Win32 API functions to accomplish what is simply impossible (or too complicated) from within Visual Basic. As you’ll see in Chapter 12, Optimizing VB Applications, API calls can also significantly speed up certain operations that are inherently slow with Visual Basic (especially graphics operations).
The API functions aren’t complicated and they’re not as exotic as Visual Basic programmers might think. The problem is that the ;\PI is documented for C programmers, so you may find it difficult to map the function and data type declarationg from C jargon to Visual Basic.
Ail Win32 API functions are available from within the Visual Basic environment. In fact, the graphics methods of Visual Basic are masqueraded API functions. In Chapter 7, Manipulating Color and Pixels with Visual Basic, we looked at the PaintPicture method, which copies bitmaps from one control to another. The PaintPicture method was introduced with Visual Basic 4. Before that, Visual Basic programmers had to use an API function to achieve similar functionality. The Bit- Blt() function can copy pixels to and from Visual Basic controls from anywhere on the Desktop. To copy part of the Desktop to a Picture Box control (for instance, to implement a custom screen capture utility) you have to use the BitBlt() function, not the PaintPicture method.
Many Windows applications use the API to some extent. Fortunately, Visual Basic hides a lot of the’ complexities of Windows programming from you while providing access to the API. This chapter will not even attempt to explain all API functions (there are more than 1,000 of them), but only a few that I think you’ll find useful.
The goal of this chapter is to introduce the basics of calling- the API functions and to explain how to use the API viewer to add API function declarations to your code. Instead of quickly presenting many API functions, I will present a few useful ones and build small applications that use them. These applications will demonstrate not only how to call the corresponding functions, but how to use them in the context of a Visual Basic application as well. The functions I’ve chosen to discuss and demonstrate with examples are not among the simpler ones. Many of the API functions are quite easy to understand and use, and although they would make for simple examples, these functions have Visual Basic counterparts, so there’s no need to learn them.