controls >> Semi-transparent controls in C#

by trgrek » Wed, 17 Nov 2004 22:39:55 GMT

I'm making a control that must be semi-transparent.

On the form must be some standard control (e.g. button). On the top of
that control there must be my control, and I must be able to see the
other control through it (like through painted glass).

On the address
there is a picture of control that behaves, as I want my control to
behave, but there is no source code available for that control (only a

If there is anyone who can help me (Bob Powell?), please do. I would
be very thankful.


controls >> Semi-transparent controls in C#

by Bob Powell [MVP] » Wed, 17 Nov 2004 22:53:30 GMT

You can use the techniques outlined in WindowsForms Tips and Tricks to
create a fully transparent control and then draw the surface using brushes
or graphics with an alpha component. These transparent controls however can
cause flicker because they force the parent to repaint before they paint
themselves. This is at best a partial solution.

I have been experimenting on and off with using the layeredwindow API's to
create truly transparent controls but i've not had the time to dedicate to
it recently. I'll publish those conclusions as soon as I resolve the

Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks

Answer those GDI+ questions with the GDI+ FAQ

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

Similar Threads

1. Transparent and Semi Transparent panels?

2. Windows Form non rectangular (using semi transparent background image (PNG)) and C# 2005 (EXPRESS)


I'm trying to create a non rectangular windows form using a PNG image 
with transparent zones and I want that this zones appears as transparent 
in the windows form.

Before, on VS.NET 2003, I do that simply putting this background image 
and setting the form with no borders, but now it don't work.

There's any way to do that ?


3. Drawing Opaque controls on top of a semi-transparent form

4. semi-transparent line


What's the simplest way to draw a semi-transparent (alpha blended) line?

I can draw alpha-blended images using the ImageAttributes parameter of the
DrawImage method, but the drawline method doesn't have an overload with an
IA parameter.


5. GDI+ Flat API Semi-Transparent Image Problems

6. Problems trying to paint semi-transparent metafile polygons.

I am creating an app that plays a metafile on top of a image (e.g, jpg). 
Specific polygons within the metafile need to have their original colour 
replaced with a semi-transparent colour of my choice. I have no problem with 
the colour substitution but I cannot get the transparency effect. My approach 
is as follows:

1) I am using EnumerateMetafile and a callback function to identify and 
process the relevant metafile polygons.

2) Prior to enumeration, I am setting up a ColorMap to specify how the 
metafile polygon colours will be replaced.  The new colour is constructed 
using Color.FromArgb() with an Alpha value to control transparency.

3) The ColorMap is then used with SetRemapTable in an ImageAttributes object.

4) The ImageAttributes is provided to EnumerateMetafile

PlayRecord is called for the relevant polygons within the callback method. 
In theory, it should render the polygon and apply the colour substitutions as 
specifed by the ImageAttributes information. The colour substitution is 
working fine,
BUT the new colour is rendered 100% opaque (i.e., no transparency). It does 
not matter what Alpha value I use. It is behaving like the Alpha value is 
ignored. Code snippets follow.

Enumeration Method snippet:

private void UserControl1_Paint(object sender, 
System.Windows.Forms.PaintEventArgs e)
if ( myMetaFile != null )
	Graphics.EnumerateMetafileProc fnCallback = new
		Graphics.EnumerateMetafileProc( MetafileCallback );

	ColorMap[] myColorMap = new ColorMap[1];
	myColorMap[0] = new ColorMap();
	myColorMap[0].OldColor = Color.White;
	myColorMap[0].NewColor = Color.FromArgb(35, 0, 255, 0);	// Note the Alpha 
value (35)

	ImageAttributes imageAttr = new ImageAttributes();

	g.EnumerateMetafile( myMetaFile, 
		new Rectangle( pictureLocation, pictureSize ), 
		imageAttr );

Callback Method snippet:

private bool MetafileCallback(
	EmfPlusRecordType recordType,
	int flags,
	int dataSize,
	IntPtr data,
	PlayRecordCallback callbackData)
	byte[] metafileRecordData = null;
	if (data != IntPtr.Zero)
		metafileRecordData = new byte[dataSize];
		Marshal.Copy(data, metafileRecordData, 0, dataSize);

	if (recordType == EmfPlusRecordType.EmfPolygon16)
		mtfBoxB.PlayRecord( recordType, flags, dataSize, metafileRecordData );

Any help would be appreciated.
Dave Clarkson

7. Form Transparency, click through semi transparent form

8. Semi-transparent context menu using ContextMenuStrip


I use ContextMenuStrip control for context menu, which will be evoked
by right mouse click in my application. Additional requirement is to
create the context menu more "visually appealing" - to be more precise,
the background of the menu should be transparent, while the individual
menu items should be semi-transparent using gradiends.

I am developing my own ToolStripRenderer class, which I then set to
Renderer property of the ContextMenuStrip instance. I was already able
to change the appearance of the menu items to some extent with this
approach. But I am still not able to create the whole context menu
transparent (it means, that if there is a picture behind the menu, it
should be visible). Do you have some hints about how to achieve this,
or whether there are some good articles about this topic? I did a lot
of search yesterday, but there seems to be no much info about Windows
Forms 2.0 yet.

Or is there any completely different approach (like use other classes)
to achieve this behaviour?

Thank you!