This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.


MIND


java911@microsoft.com         Download the code (9KB)
Jonathan Locke
WFC on My Mind

Does WFC support Win32® common controls, like TreeView and ListView?

Absoposolutely! In fact, the WFC wrapper for TreeView is really quite elegant and easy to use. The sample Shown in Figure 1, TreeViewTest, demonstrates how to create, populate, and display a WFC TreeView control in a WFC form.
      The form's constructor begins by creating a TreeView control through a call to the TreeView class's no-arg constructor. The decision to make TreeView's constructor take no arguments is consistent with the design of WFC. In general, WFC controls are constructed with no arguments; individual properties are set by calling get/set methods on the created object. This two-phase construction of objects makes things more consistent and significantly simpler for higher-level entities like builder tools.
      After constructing the TreeView object, the form's constructor then wires up an event handler for onResize events by calling addOnResize. The event handler, onResize, simply resizes the TreeView control to fill the full client area of the form, as returned by getClientSize. Next, a hierarchy of seven nodes is created and assigned to the TreeView control by calling TreeView.addNode. Each TreeNode object in the hierarchy can have zero or more Tree–Node children. The children of individual TreeNodes are added by calling TreeNode.addNode. Once the TreeView has been populated with TreeNode objects, it is added to the form. The form is then shown as a dialog with an appropriate title.
      The neat thing about just using the existing Win32 APIs as exposed by WFC in Java is how much you get for so little effort. This does cut both ways, though. As you have seen, creating a TreeView control containing text nodes is quite easy (and you can even annotate the text nodes with graphic images in fairly short order). But the underlying Win32 TreeView control is somewhat limited. Most importantly, it is not possible to put arbitrary components (like a checkbox, for example) into a TreeView. You will, generally speaking, find the same to be true across the board with WFC; the Win32 functionality is exposed, wrapped, and often simplified, but it is not particularly enhanced. For those of you who are well initiated in the intricacies of Win32, this will not be much of a problem. But if you are a novice and are trying to do something that the Win32 platform doesn't inherently support, you may run into some difficult issues here and there.


How hard is it to display a static bitmap image in WFC?

It's exceptionally easy to show a bitmap image in WFC. You don't even have to write a custom control to do it. The code in Figure 2 first creates a StaticImage control. It then loads a Bitmap object by constructing it from the file name StaticImageTest.bmp
Figure 3
Figure 3
(see Figure 3). Finally, it passes the bitmap to the control's setBitmap method, adds the control to the form, and shows the form.
      What happens if the bitmap file doesn't exist? The system will throw an Error like this:


 wfc.io.WinIOException: The system cannot find the file specified
 (StaticImageTest.bmp)
              at wfc/io/File.<init>
              at wfc/io/File.openRead
              at wfc/ui/Bitmap.<init>
              at StaticImageTest.<init>
              at StaticImageTest.main
     
 ERROR: wfc.io.WinIOException: The system cannot find the file specified
 (StaticImageTest.bmp)
 
Because the error is not a checked exception, it is not necessary to write code to handle the case where the exception is thrown. Handling the error is strictly optional. I like this design decision because it doesn't force unwanted handling of a condition that may not be a concern in many applications, and yet you can still catch the error if you need to.


Can I create tooltips in WFC?

Yes. The application in Figure 4 shows how easy it is to get standard Windows® tooltip functionality. The code adds two buttons to a form, positioning them side-by-side. It then creates a ToolTip control and sets tooltips for each button by passing the button control reference and a string (for the tooltip text) to the ToolTip.setToolTip method. Can it get any easier than this?


How do I create menus in WFC?

Menus are very nicely and consistently supported in WFC. Figure 5 creates File and Help menus with File | Exit and Help | About menu items. These top-level menus are then added to a MainMenu object and assigned to the Form using Form.setMenu. Event handlers for each menu item are wired up in the usual WFC way by calling MenuItem.addOnClick. Very easy and very simple.


How can I create toolbars in WFC?

Full support of Windows toolbars is provided in WFC. The ToolBarTest class in Figure 6 creates a two-button toolbar complete with images for each button and tooltips.
Figure 7
Figure 7
It does this by creating an ImageList that has two bitmap images (shown in Figure 7). The ImageList is then used to create images for two ToolBarButton objects. Finally, the ToolBarButton objects are added to the toolbar through a call to Tool-Bar.addButton. Once again, event handlers for the toolbar buttons are hooked up in the usual WFC way.


How can I get a list of available fonts using WFC?

The ListFonts example in Figure 8 shows how to go about this. A dummy Graphics object can be constructed by passing in 0 (null) for the Windows device context handle (HDC). This Graphics object can then be used to get a list of font descriptor objects by calling Graphics.getFontDescriptors. ListFonts then uses a hashtable to compose a list of unique font names. Finally, each font is printed out. When run, the program's output looks like this:


 J:\mind\911>jview ListFonts
 Geometric Slabserif 703 Bold Condensed BT
 CityDMed
 Blippo Black BT
 Avant Garde Book BT
 AlgerianD
 Busorama Medium BT
 OkayD
 MotterFemD
 Bookman Old Style
 LatinWidD
 Raleigh Extra Bold BT
 EwieD
 Impact
 Arial
 Architecture
 Lithograph Bold
 Commercial Script BT
 Small Fonts
 CommonBullets
 Revue BT
 
 [...]

From the August 1998 issue of Microsoft Interactive Developer.