|
Microsoft Visual Basic 4.0 is the classy new release of Visual Basic. We’ll introduce you to it here. Then we’ll tell you what this book, Visual Basic 4 How-To, is all about and how to make the best use of it. We’ll explain what’s in the book and what’s on the CD-ROM. Finally, we’ll tell you how to load and run, browse, install, and use the projects on the CD, how to install the system files, and how to register the custom controls.
About Visual Basic 4
Creates Great Windows Applications
You probably already know that Visual Basic is a great tool for creating robust and useful Windows applications. Its productivity-enhancing tools for graphical user interface (GUI) development are highly regarded throughout the industry. Visual Basic allows you to create databases and front-end applications for most popular database formats. Its object linking and embedding (OLE) features allow you to use the functionality provided by other applications, including Microsoft Word for Windows, Microsoft Excel, and Visio. Its ability to access the Windows API and other dynamic link libraries (DLLs) makes it an extremely flexible and open development language.
Comes in Three Flavors
There are three editions of Visual Basic 4.0. With the Standard Edition you can create 32-bit applications for Windows NT (starting with version 3.51) and Windows 95. You can create both 16-bit and 32-bit applications with the Professional Edition, which includes everything in the Standard Edition along with additional custom controls and the Crystal Report Writer. The Enterprise Edition provides the tools you’ll need to create robust client/server applications in an enterprise environment. It includes all the features of the Professional Edition along with an automation manager, a component manager, a set of database management tools, and Visual SourceSafe, a project-oriented version control system.Includes Many New Features
Visual Basic, Applications Edition, version 2.0 (VBA 2.0) is the new language engine in Visual Basic. It’s compatible with earlier versions of Visual Basic, as well as VBA 1.0, the language engine in Microsoft Excel 5.0 and Microsoft Project 4.0. This new language engine makes it easier to program OLE Automation objects and to exchange code with other applications that support OLE Automation. OLE Automation has been enhanced in Visual Basic 4.0. Now you can create both in-process and out-of-process OLE Automation Servers, and you can negotiate menus and provide in-place editing of OLE application objects. Visual Basic 4.0’s new class modules allow you to create reusable objects, with their own properties and methods, and assemble them into an object model. You can also create your own collections, built using Visual Basic’s new Collection object.
In the enhanced development environment, a right mouse click will pop up context-sensitive menus for forms, controls, and code. You can attach add-ins for source code control and other features. If you have the Professional Edition, you can even create your own add-ins. Forms have become full-fledged objects, with their own public and private property procedures, methods, and events. A new Object Browser lets you cut and paste from a hierarchical display of all the classes, properties, and methods available to your application from Visual Basic and other OLE components. Conditional compilation allows you to selectively build 16-bit and 32-bit versions of your application from one code base. Long filenames are supported in the 32-bit version of Visual Basic 4.0. And new custom controls let you add a Windows 95 look and feel to your 32-bit applications.
DBList, DBCombo, and DBGrid are new data-bound controls. The OLE container control is also data-bound. The Microsoft Jet database engine is upgraded to 2.5 (16-bit) and 3.0 (32-bit). You use the new Data Access Objects (DAO) programming model for cascading updates and deletes, programmatic access to referential integrity, and improved speed for many queries. You can also establish and modify security settings in a Microsoft Access database. With Jet 3.0 you can replicate a database and direct- manipulate it from within Visual Basic code.
The Enterprise Edition provides Remote Automation so you can create shared, reusable OLE Automation components on a network. A number of tools assist client/server development, including tools to catalog, install and configure components, and to manage source code access. You can use Remote Data Objects (RDO) and the Remote Data Control (RDC) to access any ODBC data source, without using a local query processor. This technique promises significantly higher performance and more flexibility when accessing remote client/server database engines such as Microsoft SQL Server and Oracle.
About This Book
Takes Visual Basic to its Limits
Following in the footsteps of the Visual Basic How-To, Second Edition, this all-new book has a plethora of tips, tricks, and techniques for taking Visual Basic 4.0 to its ultimate limits. You’ll learn, for example, to create splitter bars, rollup tools, and animated splash screens. You’ll build a game, full of explosive sound and animation. You’ll use object-oriented class files to build in-process and out-of-process OLE automation servers. How to create Quicken-like combo boxes, multiselect lookup tables, and sound-alike searches are just some of the database tricks you’ll learn. In addition, there are details on how to extract icons from executable files, write debugging information to the Windows Notepad, and even recursively search an entire disk or directory
tree.
Uses Windows APIs Extensively
Visual Basic has some great new commands that make the Windows Application Program Interface (API) routines a bit less necessary. Did you know, for example, about the new PaintPicture method? We’ll show you how to use it, in place of the BitBlt Windows API function, to perform a wide variety of bit operations for speedy graphics. But we’ve still found occasion to use dozens of Windows API calls in our How-Tos. We’ll show you how to use Windows APIs to send messages to forms and controls, make another application the parent of your program, and change the Windows Wallpaper. We’ll even show you how to determine which operating system is running, quite a complex feat now that the choice of Windows operating systems is so
varied!
Provides 32-bit and 16-bit Code
And speaking of Windows operating systems, we’ve focused our graphics on Windows 95 and our discussion on 32-bit issues. Nevertheless, a large number of the projects can be compiled into either 16-bit or 32-bit code. Many of these make use of Visual Basic 4.0’s new conditional compilation features, allowing them to be compiled with either the 16-bit or the 32-bit Visual Basic compiler. Our use of Windows APIs made that a necessity-the Declare statements for every API have changed in 32-bit Windows, and most parameters that were integer are now long. Don’t worry; by the time you finish this book, adjusting the Declares from 16-bit to 32-bit will be a piece of cake. A few projects (those that don’t rely on Windows API commands) can be compiled unchanged in either environment. For yet another set of How-Tos, we felt that enabling both 16-bit and 32-bit in one project would get in the way of what we wanted to illustrate. In those cases, we created and explained a 32-bit-only version for the book. Often, though, we were also able to include a 16-bit version of the project on the
CD.
Question and Answer Format
Don’t worry, we wouldn’t consider changing the format that worked so well in the best-selling Visual Basic How-To, Second Edition. You’ll find questions and answers arranged by categories: Visual Basic 4.0 class fundamentals, forms, controls, sounds, graphics, object linking and embedding (OLE), system services, database, tips and tricks. Each How-To contains a program solution with complete construction details. All the code, bitmaps, icons, forms, OCXs, and DLLs are contained on the enclosed
CD.
Free Custom Controls and DLLs
You probably would have been quite satisfied just to get a book that’s chock-full of example projects, covering a wide variety of tips, tricks, and techniques. Well, you might have been, but we weren’t! So the bundled CD also contains a number of free custom controls and DLLs. The custom controls are all OCXs, and we’re providing 16-bit and 32-bit versions of each. There’s a MsgHook custom control, MSGHOO16.OCX and MSGHOO32.OCX, that lets you intercept Windows messages sent to any control. You’ll see it used, for example, to intercept a “Done” message sent by the Multimedia subsystem in Windows.
Additionally, there’s a FireEvent custom control, FIREVE16.OCX and FIREVE32.OCX, that allows you to fire events within your application from the outside via OLE Automation. It’s used, for example, to keep a Sprite bouncing within a boundary box by creating an event when the Sprite approaches the boundary.
Also, you get a HiTime custom control, HITIME16.OCX and HITIME32.OCX, that lets you set values as small as 1 millisecond for the timer interval, then it fires events every millisecond. The Visual Basic Timer lets you set millisecond values for the Interval property, but actually only uses multiples of 55 milliseconds. In one How-To, for example, the HiTime custom control is used to control the drum beats in a multitrack drum machine.
We are also including WinG, Microsoft’s powerful and fast new graphics API. WinG is a set of DLLs, written and distributed for free by Microsoft. And we’re providing some free WinG Helper DLLs (WINGHELP.DLL and WINGEX32.DLL) to provide functions for copying bitmaps with and without transparency. In Chapter 5, Graphics, you’ll see WinG and the Helper DLLs used to animate sprites over a fast, smoothly scrolling background. Finally, WAVEMIX.DLL is included, also written and distributed for free by Microsoft. It makes it possible to play up to eight wavefiles simultaneously. In Chapter 4, Sound, you’ll see how we’ve encapsulated it in a class file that you’ll find easy to use in your own applications.
A system file archive (409K) is available for download that includes all the VBX, OCX, and DLL files used by the How To projects in this book. Be sure to see the section,
"Installing the System Files,"
below.
Expected Level of Reader
This book is for you, no matter what your level of expertise with Visual Basic. We’ve marked each How-To with a complexity level: Easy, Intermediate, or Advanced. If a How-To is marked Easy, it should be quick and simple to follow. Try these first if you’re just starting out with Visual Basic. The ones marked Intermediate will be a bit longer, and will often use a few simple Windows API commands. If you’re more experienced using Visual Basic, you’ll want check these out. If you’re very comfortable in Visual Basic and want to see it and the Windows API really get a workout, check out some of the Advanced-complexity
How-Tos.
What You Need to Use This Book
In order to use this book, you’ll need a computer running Windows 3.1, Windows for Workgroups, Windows 95, or Windows NT 3.51. And, of course, you’ll need Visual Basic 4.0. Most of the projects will run with the Standard Edition of Visual Basic 4.0. A few, particularly in Chapter 8, Database, require the Professional Edition. A few of the projects will run also in Visual Basic 3.0, and these are noted in the book. You’ll get the most good out of the book if you’re running a 32-bit version of Windows (95 or NT 3.51). But as we’ve noted above, many of the projects will compile successfully with both the 16-bit and the 32-bit versions of Visual Basic. You’ll need to have either the Professional or Enterprise Edition of Visual Basic 4.0 in order to compile 16-bit versions of the
code.
How This Book is Organized
This book is divided into 9 chapters as follows:
Chapter 1: Visual Basic 4 Class Fundamentals
This chapter introduces Visual Basic 4.0’s classes, properties, and objects. It will prepare you for the extensive use of these object-oriented techniques throughout the rest of the book. It starts by showing how to set up the simplest of classes, which it compares to a regular procedure or module. Step by step, the How-Tos in this chapter will show you how to add private and public variables, methods, and properties to your classes. You’ll learn how to provide output from your class to a variety of objects, including the debug window, a form, and a picture control. Along the way you will learn how Visual Basic’s implementation of classes compares to a true OOP language such as C++. Since Visual Basic 4.0 classes lack an event property, we’ve provided the FIREVENTS.OCX control to help create class events. In the final How-To, you’ll build an intelligent sprite object. You’ll program properties, methods, and events for your sprite that will allow you to create a simple demo that bounces sprite-shaped “balls” around on a
picture.
Chapter 2: Forms
This chapter makes use of more than 30 Windows API functions and subroutines in a variety of techniques you can use to add sophistication to your forms. You’ll learn how to display animated and dynamic information on a splash screen while your application loads. You’ll find out how to use a new feature of Visual Basic 4.0 to create animated cursors. You’ll learn techniques to turn plain-vanilla forms into slick widgets just like those used in commercial applications. For example, you’ll learn to a create rollup tool (similar to those used in CorelDRAW!), drag a captionless form (such as the Windows 3.1 Clock), and make a floating toolbar with a mini-title bar (like Visual Basic’s toolbar). You’ll learn to make MDI forms more sophisticated by painting an interesting background on an MDI parent form, and by saving and restoring the positions of MDI child forms. You’ll gain new control over the Windows desktop, learning to place a minimized icon on the desktop wherever you want it positioned, and learn to automatically reposition and resize controls to fit their containers. Finally, thanks to the new object-oriented nature of Visual Basic 4.0, forms have become fully functioning objects, with public and private properties and methods. In the last How-To, you’ll learn to take advantage of this to make your forms more modular and much more reusable. Specifically, you’ll discover how to start and terminate a reusable form using the new form properties and
methods.
Chapter 3: Controls
Visual Basic 4.0 provides an incredible array of controls, allowing you to create great Windows applications just by dragging and dropping controls onto forms and setting their properties at design time. But no matter how much you can do this way, you’ll make the controls in your application even smarter with a bit of code. This chapter is all about showing you how to make the controls in your programs more sophisticated using Visual Basic code and the Windows API. You’ll learn to support dragging an item from one list box and dropping it (where you want it inserted) in another list box. You’ll learn to make a more flexible PictureBox that can shrink, stretch, and scroll the picture it contains. You’ll find out how to add undo to an edit box, automatic dropdown to a combo box, and a horizontal scroll bar to a list box. You’ll learn to go beyond the capabilities of Visual Basic 4.0’s 3D setting in the new Appearance property, using 3D effects to show which control has the focus, and adding more flexible 3D effects to all controls. Looking for more? You’ll also find out how to create a splitter bar for text, clear a multiselect list box with a single call, use long values with scroll bars, guarantee fixed width font availability on any machine, and build your own toolbar without using a VBX or OCX. This chapter will even unlock the secret of LostFocus, a mystery that has baffled many a Visual Basic programmer. You’ll learn to use LostFocus correctly for data validation no matter what the user does to trigger the lost
focus.
Chapter 4: Sounds
This chapter is all about adding sound to your Visual Basic programs. You’ll learn to read and write Resource Interchange Format Files (RIFF). RIFF files are used to store wave-audio data, palettes, AVI files, and many other types of multimedia data. You’ll also learn to create, read, and write Multimedia Memory files-files that hold the key to getting good performance in your multimedia programs. You’ll learn to use the Windows MCI command string interface to access virtually anything that can be done with the Multimedia subsystem in Windows. We’ve provided the free WAVEMIX.DLL, and use it in a project that lets you play up to eight wavefiles at once. As you explore ways to use wavefiles in your programs, you’ll learn to display wavefiles, play them using low-level API calls, change their volume, and even merge them. Class files are used throughout this chapter to make it easy for you to add sound to your own programs. Everything comes together in the final project, which shows how to make a drum machine. It provides some interesting graphical effects and digital drum sounds that you can use in your own
projects.
Chapter 5: Graphics
In this chapter you’ll learn how to create a number of wonderful special effects with graphics. In the first How-To, you’ll learn about color palettes, as you begin to build a color palette class and add methods for cycling colors in a fractal image. Moving on, you’ll learn to create, read, and display information in Device Independent Bitmap (DIB) files, the format used in almost all Windows .BMP files. In subsequent How-Tos you’ll learn to fill backgrounds with a smooth transition of colors, similar to what is often used in setup programs. You’ll find out how to change background colors without repainting the form, and how to fade text titles in and out. Other How-Tos provide techniques to create the effect of moving into a tunnel, to display twinkling stars in a night sky, and to fade from one picture to another as you might do with a pair of slide projectors. Moving on, you’ll learn to use WinG, a set of DLLs that Microsoft distributes for free, which provide high-performance, device-independent graphics capabilities. With the aid of WinG you’ll learn to create animated sprites, those irregularly shaped bitmaps that move across a background without flickering. You’ll also use WinG to create a fast, smoothly scrolling background, as well as a number of ray-traced frames to create a realistic-looking spaceship that you can fly around the scrolling universe. You’ll put it all together in the final How To, creating a shoot’em-up space game with enemy ships that explode in balls of flame when hit. No game would be complete without sound effects: a continuous background music track, laser sounds, and explosive sounds-all of which you’ll incorporate in this
game.
Chapter 6: Object Linking and Embedding
Visual Basic 4.0’s new OLE automation features allow you to create in-process and out-of-process OLE automation servers. Just as in version 3.0, with Visual Basic 4.0 you can control OLE automation servers. Now, not only can you control those available in other programs such as Visio and Word for Windows 6.0, but you can also control those you create yourself with Visual Basic 4.0. In one How-To, you’ll learn to create an in-process OLE automation server that can perform long running background operations while your program continues other processing. You’ll also create a utility for viewing OLE application objects. Along the way you’ll learn how to activate the objects in-place and how to negotiate the OLE object’s menus with your own application menus. In the final How-To, you’ll learn to create an OLE automation server that facilitates communications between applications-cleanly and without resorting to Dynamic Data Exchange (DDE) custom controls or API functions. But DDE isn’t dead yet. The only way to control many useful programs is still through DDE. The Windows 3.1 Program Manager, for example, offers a DDE interface which is also supported by Windows for Workgroups 3.11, Windows 95, and Windows NT. Using DDE to communicate with the Program Manager, this chapter’s first How-To creates a useful utility to browse Program Manager’s groups and items. Another How-To uses both DDE and OLE Automation to assemble a document in Word for Windows using data provided by a Visual Basic
program.
Chapter 7: System Services
This chapter will show you how to harness the power of more than two dozen API functions and subroutines to incorporate a variety of Windows system services in your programs. You’ll learn to use API calls to submit a file directly to the print spooler, store and retrieve information in .INI files, and get the windows handle of another application when you have little information about it. You’ll also learn how to determine whether your application is compiled or running in the Visual Basic design environment. In other How-Tos, you’ll learn techniques to determine a drive type, check for a file’s existence, set a file’s date and time, detect which operating system is running, and extract and view icons embedded in executable
files.
Chapter 8: Database
This chapter scratches the surface of Visual Basic 4.0’s database enhancements. These enhancements provide access to new versions of the Jet database engine (version 2.5 for 16-bit applications, and version 3.0 for 32-bit applications) along with a new interface to data access objects DAO. You’ll encapsulate logic in a class file to create a Quicken-style Combo Box from a text box and the new DBList control. You’ll learn how to make multiple selections from a lookup table using a technique that combines the data control, DAO code, and an ordinary ListBox. You’ll also learn to use the new, object-oriented Data Definition Language (DDL) to extract the definitions of tables and fields from an Access database. Navigating through your database is covered in a couple of other How-Tos, where you’ll learn to build a better data control and to display values from any database. Other techniques covered include creating a database based on definition information in a text file, importing text records into the new database, and finding incorrectly spelled names in a database. This last technique uses the Soundex algorithm to help find not only misspelled, but also sound-alike and similarly spelled
words.
Chapter 9: Tips and Tricks
This chapter provides a variety of tips and tricks you’ll find useful in your Visual Basic programs. You’ll learn how to return an ordinal number for any number, compute the difference in years between two dates, and combine the techniques in special anniversary messages to commemorate weddings, birthdays, and years on the job. You’ll learn to generate dates in a series- useful for scheduling meetings, for example. Other tips and tricks include setting the Windows Wallpaper from your Visual Basic code and using the Windows Notepad to capture debugging information remotely from an installed version of your program. You’ll learn the power of recursive programming in a How-To that searches an entire disk or directory tree for desired files and directories. In the last two How-Tos of this book, you’ll find some general-purpose routines to parse a string into separate elements and to manipulate bits, bytes, words, and double
words.
About the CD-ROM
Packed with Projects, Classes, and Controls
The included CD-ROM is packed with all the files you’ll need to run the 80 projects in this book. Actually, there are more than 80 projects on the CD. Most projects will run in either the 16-bit or the 32-bit version of Visual Basic 4.0, often using conditional compilation. In some cases we’ve provided separate 16-bit and 32-bit versions, which brings the project count in at well over 80. Note that a handful of projects are written to run only in the 32-bit version of Visual Basic.
You’ll find that the projects on the CD are organized by Chapter and How-To. There’s a folder for each Chapter and, within each Chapter, a folder for each How-To. In the How-To folder, you’ll find the Visual Basic Project file and all the Forms, Classes, and General modules needed to run the project. If the project also uses a custom control or dynamic link library unique to this book, it will be available in another folder. More about that coming up next!
The CD is also loaded with reusable class files—45 to be exact! As you work through the projects in this book, you can expect to find a few class modules (or more than a few) that you’ll want to drop into your own projects and reuse. And we expect you’ll feel the same way about reusing the general modules, custom controls, and dynamic link libraries (DLLs). We’ve made it easy by organizing them into an additional set of folders. We’ve gathered up all the class modules (.CLS), general modules (.BAS), custom controls (.OCX) and dynamic link libraries (.DLL) into their own folders. The class modules are all together in a folder called CLASSES, the general modules are in a folder called MODULES, and the OCXs and DLLs are in a folder called
SYSTEM.
Ready to Load and Run in Visual Basic 4.0
Most of the projects are ready to run direct from the CD, without any preparation on your part. You can just start Visual Basic, open a Project file (.VBP), and run it. But be aware that if you load and run a project from the CD without copying it to your hard drive, you won’t be able to save changes. Some How-To’s may attempt to write to the project folder in order to demonstrate a concept. Of course, this will result in a run-time error, and you’ll need to copy these projects to your hard drive to get them to run successfully. Having said all this, if you’ve got the disk space for it (about 17MB), the best way to use the CD is to just to copy everything to your hard drive. Using the Windows 95 Explorer or your Windows File Manager, you can easily drag the VB4HOWTO folder from the CD to your hard drive. Or if you want to conserve on disk space, drag a How-To folder to your hard drive when you’re ready for it, as you work through the How-To in the book. Note that the files on the CD are all marked Read-only. When you copy them to your CD, you’ll want to remove the Read-only attribute so you can change them as needed. If you’re using the Windows 95 Explorer, it’s easy to highlight all the files, click the right mouse button to view their properties, and uncheck the Read-only property. The Read Me file on the CD provides information about other ways to copy the files and remove the Read-only
attribute.
Installing the System Files
Before you run a project that uses a custom control or dynamic link library, you’ll need to copy it to your Window’s System folder. You only need to do this once, of course, before you use it for the first time. The simplest method may be just to copy all the custom controls and dynamic link libraries at once, using the Window 95 Explorer or the Windows File Manager. First highlight the files in the SYSTEM folder on the CD, then drag them all to your Windows System folder.
Where exactly you need to place them will depend on which operating system(s) you’re using. If you’re using either Windows NT or Windows 95, drag all of them to the System folder located in your Windows folder. If you’re dual booting NT and Windows 3.x, drag the 16-bit files to your \WINDOWS\SYSTEM directory, and the 32-bit files to your \WINDOWS\SYSTEM32 directory. If you’re dual booting Windows 95 and Windows 3.x, drag the 16-bit files to your Windows 3.x \WINDOWS\SYSTEM directory, and drag all of the system files to the System folder located in your Windows folder. If you’re running Windows 3.x only, drag the 16-bit files to your \WINDOWS\SYSTEM directory. The following table shows which system files are 16-bit and which are
32-bit.
| 16-Bit System Files |
32-Bit System Files |
FIREVE16.OCX
MSGHOO16.OCX
WAVEMIX.DLL
WINGHELP.DLL
HITIME1.VBX
HITIME.LIC
|
FIREVE32.OCX
MSGHOO32.OCX
WAVMIX32.DLL
WINGEX32.DLL
HITIME32.OCX
|
After you copy the System files to your hard drive, you’ll need to register the custom controls. The easiest way to do this is to open Visual Basic and choose Custom Controls from the Tools menu. From this dialog, press the Browse button, highlight each of the OCX files in turn, and press OK. The selected OCX will register itself with your system. When you finish, you should see them listed with your other Custom Controls. You may now check and uncheck the references to include and exclude them from your projects. Of course, only the 32-bit OCXs will load into the 32-bit version of Visual Basic, and only the 16-bit OCXs and VBXs will load into the 16-bit version of Visual Basic.
A system file archive (409K) is available for download that includes all the VBX, OCX, and DLL files used by the How To projects in this
book.
For the Final Word, Read the Read Me!
You’ll find a Read Me file at the root level of the CD-ROM. Please read it first to review your installation options and for some alternative ways to copy files and remove their Read-only attributes. The Read Me file will also identify which How-To’s include only 32-bit code. As of this writing, they are: 2-6, 3-10, 7-1, 7-2, 7-3, 7-4, 7-6, 7-8, 7-10, and 9-3. We needed to make some minor code changes to a few projects after the book had gone to print. In each of those projects’ folders, you’ll find a README.TXT file describing the changes that were made.
Main ·
Authors ·
Foreward ·
Contents ·
Introduction · Index ·
ReadMe ·
Errata
|