10

Draw semi transparent overlay image all over the windows form having some controls such that all its child controls should be visible but you can't click them. It should just like we see some things through some semi transparent black mirror.

I have tried using Transparent control. That is sub-classing Panel control and drawing image over that control, however all the controls are fully visible.

Mike Baxter
  • 6,868
  • 17
  • 67
  • 115
MLS
  • 141
  • 1
  • 2
  • 9

3 Answers3

30

This is going to require another form that you display on top of the existing one. Its Opacity property can create the intended effect. Add a new class to your project and paste the code shown below. Call the Close() method to remove the effect again.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class Plexiglass : Form {
    public Plexiglass(Form tocover) {
        this.BackColor = Color.DarkGray;
        this.Opacity = 0.30;      // Tweak as desired
        this.FormBorderStyle = FormBorderStyle.None;
        this.ControlBox = false;
        this.ShowInTaskbar = false;
        this.StartPosition = FormStartPosition.Manual;
        this.AutoScaleMode = AutoScaleMode.None;
        this.Location = tocover.PointToScreen(Point.Empty);
        this.ClientSize = tocover.ClientSize;
        tocover.LocationChanged += Cover_LocationChanged;
        tocover.ClientSizeChanged += Cover_ClientSizeChanged;
        this.Show(tocover);
        tocover.Focus();
        // Disable Aero transitions, the plexiglass gets too visible
        if (Environment.OSVersion.Version.Major >= 6) {
            int value = 1;
            DwmSetWindowAttribute(tocover.Handle, DWMWA_TRANSITIONS_FORCEDISABLED, ref value, 4);
        }
    }
    private void Cover_LocationChanged(object sender, EventArgs e) {
        // Ensure the plexiglass follows the owner
        this.Location = this.Owner.PointToScreen(Point.Empty);
    }
    private void Cover_ClientSizeChanged(object sender, EventArgs e) {
        // Ensure the plexiglass keeps the owner covered
        this.ClientSize = this.Owner.ClientSize;
    }
    protected override void OnFormClosing(FormClosingEventArgs e) {
        // Restore owner
        this.Owner.LocationChanged -= Cover_LocationChanged;
        this.Owner.ClientSizeChanged -= Cover_ClientSizeChanged;
        if (!this.Owner.IsDisposed && Environment.OSVersion.Version.Major >= 6) {
            int value = 1;
            DwmSetWindowAttribute(this.Owner.Handle, DWMWA_TRANSITIONS_FORCEDISABLED, ref value, 4);
        }
        base.OnFormClosing(e);
    }
    protected override void OnActivated(EventArgs e) {
        // Always keep the owner activated instead
        this.BeginInvoke(new Action(() => this.Owner.Activate()));
    }
    private const int DWMWA_TRANSITIONS_FORCEDISABLED = 3;
    [DllImport("dwmapi.dll")]
    private static extern int DwmSetWindowAttribute(IntPtr hWnd, int attr, ref int value, int attrLen);
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • How exactly is this class called? – Mike Baxter Mar 17 '13 at 17:28
  • 1
    Looks like it's `Utils.Plexiglass(this);` ... ALT+F4 will close it. Any way to get black text on-top of this window, e.g. "Please Wait"? (I just tripled the opacity and it helped with a dynamic label I added to this overlay). – PeterX Apr 12 '13 at 06:52
  • Hey PeterX, just wondering how you got a label to show up on the Plexiglass? Any pointers? :) – PhoenixDev Oct 06 '15 at 10:14
  • @HansPassant, this answer is very helpful however it also seems to make child controls (ie label text) semi-transparent. Is there a way to just have the form semi-transparent while it's children are fully opaque? Thanks. – Adam Milecki Jun 29 '16 at 15:46
  • 1
    Sure, that's what Opacity does. This class is only useful to cover an existing window, "should be visible but you can't click them" as demanded by the OP, it is not useful to display any UI. – Hans Passant Jun 29 '16 at 16:14
3

Create a layered window that is kept on top of your primary form and synced with its location. You can alter the layered window's alpha using a 32-bit RGBA image in order to get your desired effect.

There's a decent codeproject article showing you how to do this here.

Ron Warholic
  • 9,994
  • 31
  • 47
1

I believe a simpler approach is to put a transparent Label control where you set its opacity and also disable its AutoSize feature and resize the label to the size of the surface you want to cover.

Then when you want to overlay the label, you send it to the front (programmatically) and make it visible. When you want to disable the overlay send it to the back and make it invisible.

I have done this with a text label that overlays my whole form. I think it would work just the same if instead of setting the Text property of the Label control, you set a semi-transparent (PNG) image.

Lord of Scripts
  • 3,579
  • 5
  • 41
  • 62