Click here to Skip to main content
Click here to Skip to main content

Solid Background of a Transparent Control on a Form

, 24 Jan 2014
Rate this:
Please Sign up or sign in to vote.
Solid background of a transparent control on a form

Introduction

Showing a dialog with overlay looks cool on Windows. For this TransparencyKey is set to a color and color of form is changed to that color and in result form becomes 100% transparent. The problem starts when color of any control on form matches with TransparencyKey; that part also becomes transparent. This tip will help you to solve this problem.

Background

I was developing an application with transparent forms where I have to show a picturebox with a little transparency. I have set the TransparentKey to color X and when a picture with X color is shown that part becomes 100% transparent and by clicking that part, the user can click on the window behind the control.

Using the Code

I have used Visual Studio 2010, C# and .NET Framework 4.0 for this demo. There are two forms frmForeground is transparent form and frmBackground is a solid form that hides the transparency of image on foreground form.

Now user should show background form instead of foreground. In background forms' load event handler, it will load foreground form. The important point is to register a close event handler of foreground form so that background/owner form is notified about the closing of foreground form. To avoid recursion & stack overflow, this event handler must be unregistered when its notification is called. See frontFormCloseNotify().

private void frmBackground_Load(object sender, EventArgs e)
{
	frontForm = new frmForeground();
	frontForm.StartPosition = FormStartPosition.Manual;
	frontForm.FormClosed += new FormClosedEventHandler(frontFormCloseNotify);
	frontForm.Show(this);
	frontForm.BringToFront();
}

public void ChangePosition(Point position)
{
	System.Diagnostics.Trace.WriteLine("frmBackground Change Position: " + position.ToString());

	this.StartPosition = FormStartPosition.Manual;
	this.Location = position;
}

private void frontFormCloseNotify(object sender, FormClosedEventArgs e)
{
	System.Diagnostics.Trace.WriteLine("frontFormCloseNotify in frmBackground ");
	// If sender is the form that we have created then 
	// unregister event notifier to avoid recursion and call close.
	if (sender is frmForeground)
	{
		frontForm.FormClosed -= new FormClosedEventHandler(frontFormCloseNotify);

		this.Close();
	}
}   

Form foreground should set position of its owner when it is loaded and should also set owner's position when it is moved. When this form is closed, only this.Close() has to be called and owner/background form will be notified because it has also registered a handler for Close event. Hiding parent form is different than closing.

private void frmForeground_Load(object sender, EventArgs e)
{
	frmBackground owner = this.Owner as frmBackground;
	if (owner == null)
		return;

	// Set size of owner/background form to cover the background of control.
	owner.Location = this.PointToScreen(this.pictureBox1.Location);
	owner.Size = new Size(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height);
}

private void frmForeground_Move(object sender, EventArgs e)
{
	// Move owner/background so that the control is always covered.
	frmBackground owner = this.Owner as frmBackground;
	if (owner == null)
		return;

	owner.ChangePosition(this.PointToScreen(this.pictureBox1.Location));
}

private void buttonClose_Click(object sender, EventArgs e)
{
	this.Close();
}

private void buttonHide_Click(object sender, EventArgs e)
{
	this.Hide();

	if (this.Owner != null)
		this.Owner.Hide();
}  

History

  • Published on 24-Jan-2014

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Omer Aziz
Software Developer (Senior)
Pakistan Pakistan
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 24 Jan 2014
Article Copyright 2014 by Omer Aziz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid