Q. See Through Windows
I just saw a utility app that had a floating dialog that turned translucent when it lost focus. How did the author do that?
ABOUT THIS COLUMN
Ask the VB Pro provides you with free advice on programming obstacles, techniques, and ideas. Read more answers from our crack VB pros. You can submit your questions, tips, or ideas on the site, or access a comprehensive database of previously answered questions.
|
|
A. Windows 2000 offers some interesting new special effects. Layered windows are one of the most dramatic, but they also are highly open to misuse (see Figure 1). They offer an opportunity to either impress or incite your users, depending on how restrained you are in using them.
Layered windows comprise two types. One type forces all pixels of a specified color to become fully transparent. This effect can be useful for irregularly shaped windows, because the operating system is optimized to repaint both the layered window and the windows beneath it, eliminating the flicker associated with complex window regions.
| |
Figure 1 Beware of Stupid Windows Tricks
Click here.
|
The second type uses an alpha blend (a method of combining images using pixel colors and their alpha values) to combine every pixel of a layered window with the ones underlying it, producing the translucent effect you asked about. To create a layered window, toggle the WS_EX_LAYERED extended style bit on. Flip that bit by first obtaining the current extended style bits using GetWindowLong, Or'ing WS_EX_LAYERED with the current bits, and finally calling SetWindowLong to assign the new style (see Listing 1).
Windows 2000 introduced the SetLayeredWindowAttributes API, and to date it's the only operating system that supports the API. This API assigns either the transparency color key or the opacity level of a window. Make sure you're running in Windows 2000 before calling SetLayeredWindowAttributes, or expect error number 453, "Can't find DLL entry point."
Set the dwFlags parameter of SetLayeredWindowAttributes to LWA_ALPHA; then you can assign transparency ranging from completely invisible (0) to fully opaque (255), based on the bAlpha parameter. Try values in the 150-175 range to achieve a readable but fairly translucent effect. Dropping much below 150 makes distinguishing the window difficult.
What's this translucent effect good for? It's tailor-made for floating toolbox windows, because they no longer obscure what's under them when they don't have focus. I've had good success assigning an opacity of around 160 to tool windows when their Form_Deactivate event fires, then cranking the opacity back up to 255 on Form_Activate. If you have a novel use for this functionality, I'd like to hear about it.
Q. Handle Fractional Math
I just landed a position writing applications for the construction industry, and the greatest obstacle I see coming is its reliance on fractions for everything. Are you aware of any existing routines I can use to do math with fractions?
A. You're in luck! When VB4 first went to beta, Zane Thomas wrote one of the first VB classes I ever played with. It's still a classic (despite my tweaking it over the years), and I'm happy to be able to pass it along. I'd also encourage anyone who's "just never gotten into classes" to keep reading; this one helped make the whole concept clear to me from the start.
Browsing the source of CRat.cls, or even reading my description of how it operates, isn't enough to fully appreciate what this class does (see Listing 2). Conceptually, CRat defines a new datatype, whose primary properties are the Numerator and Denominator. CRat also exposes an AutoReduce property; when this property is set, it instructs the class to reduce all fractions by dividing both the numerator and denominator by their greatest common denominator.
To use this new datatype, create a new instance of the class, and assign values to the Numerator and Denominator properties. Perform simple math by passing one CRat instance to the Add, Subtract, Divide, or Multiply method of another CRat class, all of which return a third CRat. For example, use this code to multiply 7/4 by 1/2 and display the equation with its results:
Dim rat1 As New CRat
Dim rat2 As New CRat
rat1.SetValues 7, 4
rat2.SetValues 1, 2
' Print rat1 * rat2
txt = rat1.AsString & " * " _
& rat2.AsString & " = " _
& rat1.Multiply(rat2).AsString()
Debug.Print txt
My best advice is to load up the CRat class, along with its accompanying demo (see Listing 3), and step through it using the F8 key in the IDE. It's really one of the more fun ones to walk through that I've ever seen.
Karl E. Peterson is a GIS analyst with a regional transportation planning agency and serves as a member of the Visual Basic Programmer's Journal Technical Review and Editorial Advisory Boards. Online, he's a Microsoft MVP and a section leader on several VBPJ forums. Find more of Karl's VB samples at
www.mvps.org/vb.