Customizing the Style of the Measurement Studio .NET Switch control

Publish Date: Sep 06, 2006 | 2 Ratings | 2.00 out of 5 | Print | Submit your review
This example demonstrates how to emulate the 3D style of the Measurement Studio 6.0 switch with the .NET switch in Measurement Studio 7.0. This process requires two steps:

1. Create images (preferably metafiles) of the OFF and ON states of the 3D switch in Measurement Studio 6.0.

2. Create a class that derives from NationalInstruments.UI.SwitchStyle and create a custom style that uses the 3D images to display the style in Measurement Studio 7.0.

The attachment contains a project that generates the images described in the first step. The attachment contains a project that demonstrates how to create the custom style in the second step. The CustomSwitchStyle project can be run on its own as it already contains the metafiles necessary to customize the .NET switch ON and OFF image.

The easiest way to create the images from the Measurement Studio 6.0 controls is to create a separate project that uses interop assemblies for the Measurement Studio 6.0 ActiveX controls, add an ActiveX switch to a Windows Form, then use the ActiveX control's ControlImage method to get an image for the control. The image that is returned by ControlImage is a metafile, so if you call this once for each state, you can easily get metafiles for when the value is false and for when it is true. Once you have the metafiles you can save them and then copy the saved metafiles to the project where you will create the custom switch style. Refer to the attached SaveCWMetafile project for more information.

Add the saved metafiles to your project and specify that they are embedded resources in the Properties Window. Create a class that derives from NationalInstruments.UI.SwitchStyle. In the class, you can create the two metafiles at run time from the resources in the assembly manifest. Override the Draw method and then draw the appropriate bitmap according to the state of the switch control. Once you have the custom style created, instantiate it and assign it to the switch's SwitchStyle property. Your .NET switch should then be drawing like the 3D switch in Measurement Studio 6.0. This process is much easier than it sounds. Refer to the attached CustomSwitchStyle project for more information.

The background and foreground colors of the control are hard coded into the image, so you would have to create additional images if you wanted other colors. The background color can especially be a problem if you're running the application on a machine that uses non-standard system colors.

Back to Top

Bookmark & Share








Rate this document

Answered Your Question?
Yes No