Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Solid Background of a Transparent Control on a Form

0.00/5 (No votes)
24 Jan 2014 1  
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 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