Putting your WinForms graphics into a sleep state...

While helping someone with a DirectX problem I came across what I think might be a neat feature for Windows Forms.  You can always gamma correct your UI to a lower brightness value by layering a transparent black from over the top.  This as mentioned in the title is useful for telling the user the application is in a sleep state, and it means that you can control how they interact with the application when they come back (by say, making them click the transparent form).  So what exactly am I saying?  Well, first off create a new blank form without any borders or titles and attach a MouseUp handler so you can dismiss the form later.

this.FormBorderStyle = FormBorderStyle.None;
this.Text = "";
this.MinimizeBox = false;
this.MaximizeBox = false;
this.ControlBox = false;
this.BackColor = Color.Black;
this.TopMost = true;
this.ShowInTaskbar = false;

this.MouseUp += new MouseEventHandler(this.Form_MouseUp);
this.VisibleChanged += new EventHandler(this.Form_VisibleChanged);

Then you are going to want to set the opacity of the form to some value, like 20%.  This darken the the controls on your form considerably.

this.Opacity = 0.2f;

Finally, you'll need to show this form using ShowDialog(this) from within the parent form.  You might also set the Owner property to the current form, so you can set up your transparent dialog in the proper location.  You may not actually need this, but do it just in case.  If the main form is minimized, it will also minimize the owned form (dialog), so I think that is pretty cool.

GammaForm gammaForm = new GammaForm();
gammaForm.Owner = this;
gammaForm.ShowDialog(this);

Finally, you'll work through those events.  The mouse event needs to close the gammaForm, and the VisibleChanged should position the gamma form over top of the parent.

private void Form_MouseUp(object sender, MouseEventArgs e) {
    if ( (e.Button & MouseButtons.Left) == MouseButtons.Left ) {
        this.Close();
    }
}

private void Form_VisibleChanged(object sender, EventArgs e) {
    if ( this.Visible ) {
        this.Location = this.Owner.Location;
        this.Size = this.Owner.Size;
    }
}

Wooh, rest assured the code is functional, I just went ahead and used it.  In order for the minimize to still work though, you'll need to use the Show() method instead of ShowDialog(this).  The reason here is that when a dialog is shown you can't really interact with the main form's control features in the task bar.  I'm sure some Windows APIs or spy utils could probably do it, but that doesn't count.

Published Thursday, January 22, 2004 3:09 AM by Justin Rogers
Filed under: , ,

Comments

Friday, March 05, 2004 1:19 AM by TrackBack

# re: New Terrarium UI

Friday, December 05, 2008 9:51 PM by Semil

# re: Putting your WinForms graphics into a sleep state...

<a href= spiritez.com ></a>

Friday, December 26, 2008 4:40 AM by elexx-qo

# re: Putting your WinForms graphics into a sleep state...

<a href= membres.lycos.fr/dertull >zx10r graphics</a>

Saturday, February 28, 2009 9:52 PM by Olgunka-uz

# re: Putting your WinForms graphics into a sleep state...

<a href= http://adultromancefinder.com >chat online</a>

Friday, March 26, 2010 1:58 PM by Yasuo

# re: Putting your WinForms graphics into a sleep state...

Good afternoon. Your site is very convenient in navigation and has good design. Thanks. Help me! Help to find sites on the: Side effects of flonase nasal. I found only this - <a href="genericflonase.info/.../">price of flonase</a>. Flonase, radu jude brings the attractive chairs during the dreams between the requires hardly someday huge and cheap products on the tutorials between the mad and the various bases. Westward, both the niÄ‚±a and the pinta were stepped, flonase. Thank :o Yasuo from Malawi.

Leave a Comment

(required) 
(required) 
(optional)
(required)