Creating Controls

The MS Visual J Plus Plus v6 Toolbox contains a number of Microsoft Windows Foundation Classes for Java (WFC) controls. In fact, the majority of Part II was devoted to demonstrating the power and flexibility of WFC controls. There might come a time, however, when you'll need some exotic control that isn't in the Visual J Plus Plus Toolbox. For example, you might want to create a clock object with hands that sweep out the minute and hour, or a button with rounded edges. Fortunately, Visual J Plus Plus v6 allows you to create your own controls and add them to the Toolbox.

You can create two types of controls in Visual J Plus Plus:

For the remainder of this chapter, I'll refer to these two types of controls as WFC-style controls and ActiveX controls.

NOTE
ActiveX controls are actually no more remote than local WFC-style controls. Unlike local controls, however, they are available outside Visual J Plus Plus.

You can create your own WFC-style control to perform the same types of tasks as a Label control, an Edit control, a Button control, and so on. A user-defined WFC-style control looks and acts like the controls that are already in the WFC Controls section of the Toolbox. In fact, once you've created your own WFC-style control, you can add it to the Toolbox. Essentially, there is no difference between your WFC-style control and the controls that Visual J Plus Plus provides.

You can easily turn WFC-style controls into ActiveX controls. You can then add these ActiveX controls to the Visual J Plus Plus Toolbox, although you'll lose some of the editing capability you had when they were WFC-style controls. You can also add ActiveX controls to the toolbars of other Microsoft Visual Studio 6 languages, such as Microsoft Visual Basic 6.

In this chapter, we'll create our own WFC-style control. This control will have the same type of data and active properties as WFC controls, and will support both a data property and an event that we create. We'll add this control to the Toolbox, and use it in a very simple example program to demonstrate how it works.

Once we've created our WFC-style control, we'll turn it into an ActiveX control. We'll first use this ActiveX control in a Visual J Plus Plus v6 program and then in a Visual Basic 6 program.

NOTE
The WeightEdit control developed throughout this chapter is included on the companion CD in the Custom Controls subdirectory. The control is built in stages, so you'll find each version of the control located in a separate subdirectory within the Custom Controls subdirectory. Because only one copy of the WeightEdit class files can be in the \Windows\Java\Classes directory (explained later), you'll find WeightEditTest.exe in the same directory as WeightEdit for test purposes.
Comments