Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / Windows Forms
Article

Non-transparent controls on a semi-transparent window

Rate me:
Please Sign up or sign in to vote.
4.19/5 (17 votes)
6 Jul 20064 min read 86.6K   4.1K   57   13
The article describes the use and the principle of operation of semi-transparent controls with non-transparent child controls.

Image 1

1. Introduction

There is an opportunity to improve the user’s interface for windows in Windows by setting the transparency. In Windows versions beginning from 2000, it is implemented by setting the WS_EX_LAYERED style for the window. But here, we are faced with a problem – the style can’t be set for the child windows. The semi-transparent window looks showy, but everything that is drawn on it (including controls) will also be semi-transparent. And it considerably worsens the ergonomics of the user’s interface.

The article describes the SkinTooltip control developed by the KB_Soft Group company for its internal needs, that allows the effect of the non-transparent controls on the semi-transparent window to be reached. The approach described below also allows different animations to be implemented on the control while displaying it.

It is necessary to note that the control is not really semi-transparent but it only imitates the transparency. In this connection, some restrictions to its usage appear, that will be described below.

2. The description of the control’s work

The essence of our approach to the solution of the given problem is as follows. The control itself (and all the child controls on it) is not semi-transparent. The semi-transparency of the window is imitated. Two steps are performed for this.

On the first step, the screenshot of the parent window is made using the following function:

C#
public static void GetControlScreenshot( Control control, 
                                         ref Bitmap bitmap )
{
    if (control == null || control.Handle == IntPtr.Zero)
    return;

    if (control.Width < 1 || control.Height < 1) return;

    if (bitmap != null)
        bitmap.Dispose();

    // preparing the bitmap.
    bitmap = new Bitmap(control.Width, control.Height);
    Graphics destGraphics = Graphics.FromImage(bitmap);

    // setting the flag indicating that we
    // need to print both the client's and
    // the non-client's window rectangles.
    int printFlags = (int)(    Win32API.PRF_NONCLIENT | 
                            Win32API.PRF_CLIENT);

    System.IntPtr param = new System.IntPtr(printFlags);
    System.IntPtr hdc = destGraphics.GetHdc();

    // sending the WM_PRINT message to the control window.
    Win32API.SendMessage(control.Handle, Win32API.WM_PRINT, hdc, param);
    destGraphics.ReleaseHdc(hdc);

    destGraphics.Dispose();
}

The function draws the control window to the “bitmap” bitmap. It is made with the help of the SendMessage Win32 function. It sends the WM_PRINT message to the window, its parameters specify the device context for output.

The controls of the parent window are also drawn at the obtained image. Then the image is displayed on the control’s surface, and as a result, the control becomes invisible on the parent window.

On the second step, the background of our semi-transparent window is set to another object of the Bitmap class. The background should present the image with the set semi-transparency (i.e., having the alpha-channel). All child controls are drawn on the image (it is needed to implement the animation). The obtained image is drawn above the background already displayed, and all the controls become visible. As a result, we have the effect of semi-transparent windows with non-transparent controls.

Animation is performed using the image obtained on the second step. To reach the effect of the control’s smoothness appearing on the background of the parent window, the alpha value for each bitmap’s pixel is multiplied according to the timer’s message, on a multiplier in the range from 0 to 1.0; as a result, the image varies from complete transparency to the value initially set in the image that is specified as a control’s background on the second step. To perform this, the .NET Framework library has a standard mechanism basing on the ColorMatrix class. Using the class, you may specify the transformations to be done with the image’s colors before it is displayed on the screen.

Some restrictions concerning the control’s usage result from the described algorithm. Since the screenshot of the state of all the controls on the parent window is made only once before displaying, the changes in their appearance may break the illusion of semi-transparency. Any changes in the child controls on the semi-transparent window after its first initialization may also lead to the control’s malfunction. The given restrictions are not critical for the task KB_Soft Group was faced with while developing the given controls, but the component's elaboration may be necessary for other cases.

3. The description of the control’s usage

The control is inherited from the SkinControl base class. The class usage is described in the “Controls of an arbitrary shape” article.

The most important properties and methods of the class are described below:

  • AlphaAnimation – the property setting whether to use the animation using a smooth transparency change.
  • AnimationInterval – the property setting the time interval between two animation steps.
  • AnimationPosition – the property setting the position to start the control’s animation.
  • AnimationSteps – the property setting the number of animation steps.
  • ExpandAnimation - the property setting the animation type by changing the control’s sizes.
  • ExpandType – the property setting the animation type by changing the control’s sizes (moving, stretching, and so on).
  • FrontImage – the property setting the control’s background image.
  • Labels – the collection of non-transparent labels on the semi-transparent window.
  • Animate() – the function starting the control’s animation.

The simplest way to use the control is to add it to the Toolbox in the Visual Studio environment and to set all its properties. But for more deliberate usage of the control, given below is its manual creation:

Create a new Windows-application project on C#. Add a new resource – the result.png image. It will be our control’s background, and also a pattern for specifying its shape. It is necessary not to forget to specify Build Action = Embedded Resource in the resource's properties.

Add to the application a link to the KBSoft.Components.dll library, and add the corresponding using directives to the beginning of the file containing the form:

C#
using KBSoft.Components;

Now add the class a new item:

C#
private SkinTooltip skinTooltip = new SkinTooltip();
private Button btn = new Button();

Add the following code to the constructor:

C#
//getting the assembly for extracting the resources.
Assembly currentAssembly = Assembly.GetAssembly( this.GetType() );

//setting the pop-up animation.
skinTooltip.AlphaAnimation = true;

//setting the time interval between animation steps.
skinTooltip.AnimationInterval = 40;

//the position to display the control.
skinTooltip.AnimationPosition = new System.Drawing.Point(80, 24);

//the number of animation steps.
skinTooltip.AnimationSteps = 20;

//setting not to use animation by changing the control’s sizes.
skinTooltip.ExpandAnimation = false;

//setting the image of the control’s background.
skinTooltip.FrontImage = (Bitmap)Bitmap.FromStream( 
currentAssembly.GetManifestResourceStream("TestControls.result.png") );

//setting the control’s shape.
skinTooltip.PatternBitmap = (Bitmap)Bitmap.FromStream( 
currentAssembly.GetManifestResourceStream("TestControls.result.png") );

//setting the color of the image parts that 
//are not included to the control’s //region
skinTooltip.TransparentColor = Color.FromArgb( 255, 255, 255 );

//creating the button and adding it to the list 
//of the controls that are child for the skinTooltip control.

btn.Size = new Size(50,30);
btn.Location = new Point(150,30);
btn.Text = "Demo";
btn.FlatStyle = FlatStyle.System;
skinTooltip.Controls.Add( btn );
            
//the call needed for the control to function correctly.
skinTooltip.EndInit();

//necessarily set the control’s parent window.
skinTooltip.Parent = this;

The result is shown in the screenshot below:

Image 2

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
Alexandr Golovanov is a .NET developer at KB_Soft Group, an offshore software development company located in Russia, Novosibirsk. Here he has worked
on various .NET projects.

Comments and Discussions

 
GeneralMy vote of 1 Pin
gfhfhf4g9-Jan-11 1:02
gfhfhf4g9-Jan-11 1:02 
GeneralMy vote of 2 Pin
Ozan Müyesseroğlu12-May-10 1:32
Ozan Müyesseroğlu12-May-10 1:32 
GeneralPs give me Reply Pin
BLekha26-Mar-07 0:02
BLekha26-Mar-07 0:02 
GeneralWell... Pin
Nick Z.11-Jul-06 5:48
Nick Z.11-Jul-06 5:48 
GeneralRe: Well... Pin
Alexandr Golovanov11-Jul-06 20:00
Alexandr Golovanov11-Jul-06 20:00 
GeneralRe: Well... Pin
Nick Z.12-Jul-06 5:45
Nick Z.12-Jul-06 5:45 
GeneralRe: Well... Pin
Alexandr Golovanov12-Jul-06 22:23
Alexandr Golovanov12-Jul-06 22:23 
GeneralWindows Vista Form Pin
TheCardinal7-Jul-06 1:09
TheCardinal7-Jul-06 1:09 
GeneralRe: Windows Vista Form Pin
Nick Z.11-Jul-06 5:51
Nick Z.11-Jul-06 5:51 
GeneralRe: Windows Vista Form Pin
Alexandr Golovanov11-Jul-06 19:56
Alexandr Golovanov11-Jul-06 19:56 
GeneralRe: Windows Vista Form Pin
TheCardinal11-Jul-06 20:27
TheCardinal11-Jul-06 20:27 
GeneralRe: Windows Vista Form Pin
BlackTigerAP11-Jul-06 21:05
BlackTigerAP11-Jul-06 21:05 
GeneralRe: Windows Vista Form [modified] Pin
Nick Z.12-Jul-06 5:51
Nick Z.12-Jul-06 5:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.