I would like to overlay a gray, translucent area over the entire screen through C#. Is this possible to do through Windows Forms and how would I go about doing this?
Sure, just create a borderless, translucent window that covers all of the desktop screens.
You can find the right Rectangle to cover all of the screens with the following LINQ:
Rectangle bounds = Screen.AllScreens
.Select(x => x.Bounds)
.Aggregate(Rectangle.Union);
Then set the Left, Top, Width and Height of the Window from bounds
In addition to using Johannes' suggestion to set the 'FormBorderStyle property to 'None, I'd also set the following properties on this Form used to "dim-out" the screen :
I'd set the "dim-out" Form's size in the Load event of the Form : I'd use the elegant code in Rob's answer to set the bounds of a Form added to a project if I wanted to handle the case of multiple monitors. If I just wanted to handle only one monitor, I'd just do something simple like :
// in the Load Event of the "dim-out" Form
this.Bounds = Screen.PrimaryScreen.Bounds;
Then, of course, you can show this "dim-out" Form when you need to in response to whatever on your visible Forms.
Showing the "dim-out" Form will make it appear on top of your Application's other visible Forms (unless one of those is has TopMost or TopLevel properties set).
But a nice effect you can achieve is to show your "dim-out" Form just before a MessageBox (or a Form shown modally) is shown : that means that you will then have the MessageBox dialog (or modal form) "in front" with everything else behind it "dimmed."
So here's how your code to show the "dimmed" form might look :
dimmedForm.Show();
// change these to suit your taste or purpose
// this.BringToFront();
// dimmedForm.BringToFront();
// example of showing a MessageBox over the dimmedForm
// which will block the current thread
MessageBox.Show("why not ?");
// now hide the dimmedForm
dimmedForm.Hide();
You might want to take a look at the 'TopLevel property (which is not exposed at design-time) and refresh your knowledge of how that property can affect Form order on the screen, as well as examining the 'TopMost property of a Form (which is exposed at design-time).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With