July 2007 Archives

I thought it might be interesting to document the recent code modifications to CrystalGradientControl to allow sub-classes (CrystalLabel and CrystalPictureBox) to have transparent backgrounds.

As most Windows Forms programmers know, setting the BackColor to Transparent is the way to allow transparent backgrounds in most controls. I wanted to support the same convention. However, setting the BackColor to Transparent result in a nasty message ("Control does not support transparent background colors"). To fix this, I had to call SetStyles to declare that my color does support transparency:

public CrystalGradientControl()
{
  SetStyle(ControlStyles.SupportsTransparentBackColor, 
           true);
}

Next, I wanted to override the property (from the base class Control), BackColor. Why? Because when BackColor is set to Transparent, I needed to set an internal property called TransparentMode to True. TransparentMode triggers a number of things best described on Bob Powell's article on transparent controls. This was all I needed to do:

public override Color BackColor
{
    get
    {
        return base.BackColor;
    }
    set
    {
        if (base.BackColor != value)
        {
          base.BackColor = value;
          TransparentMode = 
            (base.BackColor == Color.Transparent);
          this.InvalidateEx();
        }
    }
}

Now my controls allowed the transparent color, but the background was still getting painted black. Why? Because in my override of OnPaintBackground, I needed to call the base class when TransparentMode was set to true, to allow the parent Form to repaint the background:

protected override void OnPaintBackground
                    (PaintEventArgs pevent)
{
    if (TransparentMode)
    {
        base.OnPaintBackground(pevent);
        return;
    }

    PaintGradientBackground(pevent.Graphics);
}

There you have it, a few simple fixes. When I first started this toolkit, I didn't know how programmers set the transparent color. I made a mistake in having the TransparentMode property be public and totally ignoring BackColor. Now I've hidden TransparentMode from the Forms designer and allow programmers to do it the traditional way.

CrystalLabel and CrystalPictureBox were not allowing Transparent backgrounds. This is a bug that was fixed in Crystal Toolkit version 0.77. I have to admit that I am more focused on the gradient properties of these controls--doing the transparency thing was a side experiment. I've been tempted to drop the transparent mode altogether (and I have done that in CrystalTrackBar, which has numerous problems). However, I found a pretty simple fix to allow the Label and PictureBox controls to retain a transparent background.

How do you set these controls to have a Transparent background? By default, you have the gradient mode set when you drag CrystalLabel and CrystalPictureBox onto your form:

gradient_control.jpg

To attain this gradient setting, you need to have BackColor set to Control, and then Color1 and Color2 make up the gradient blend of colors:

gradient_control_setting.jpg

Now, if you want the Transparent background, simply set BackColor to Transparent (on the Web tab of the color property dialog). This is the standard way to set transparency with other .NET controls. When I started this toolkit, I was ignorant of that fact. Internally within the CrystalGraidentControl class, setting BackColor to Transparent makes the TransparentMode property true.

transparent_control_setting.jpg

The end result is that both the CrystalLabel and CrystalPictureBox controls have a transparent background and allow the wallpaper in the form to show through. This sample code is in the CrystalToolkit 0.77, called TestAttilanTransparent.

transparent_control.jpg

Crystal Toolkit is a Windows Forms (.NET Framework 2.0) control library written in C#. This is the eighth release and contains the following updates:

Bug fixes.

  1. CrystalGradientControl-derived classes, including CrystalLabel and CrystalPictureBox (but not CrystalTrackBar): Transparent backgrounds were not working previously. To set a transparent background, set BackColor property to the Transparent color. You can do this in design mode or programmatically. This makes TransparentMode=True. When this occurs, Color1 and Color2 are ignored. To get the Gradient color mode to work, set BackColor back to Control (or any other non-Transparent value) and then set Color1 and Color2 to the Gradient color scheme.

  2. CrystalThumbnailer: Exceptions are now caught when an error occurs when thumbnailing images.

  3. CrystalImageGridView: Bug fixes for keyboard navigation. When images were selected and right-mouse click inside the middle of the selection, the selected image list is retained.

This release comes in a ZIP file. Simply unzip the contents to your hard drive, navigate to the root Attilan folder, and double click on CrystalDemo.sln. This solution file contains the Crystal Toolkit plus demo programs. Just build the solution (which compiles the CrystalToolkit library first) and run the demo programs to see how they work. You can run the demo programs without building the source by clicking on "StartDemo.bat" in the root Attilan folder or CrystalDemoLauncher.exe in Attilan\bin.

I've tested the demos under Windows XP SP2 and Windows Vista. Needless to say, buyers beware, this is alpha software and it's free open-source code: you get what you pay for! I'd welcome any feedback, bugs (or bug fixes), and thumb-bitmap replacements. Send email to richard [at] attilan {dot} com.

Download: Crystal Toolkit 077 (zip file, 8 mb)