Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw semi transparent overlay image all over the windows form having some controls

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.

like image 741
MLS Avatar asked Dec 21 '10 19:12

MLS


2 Answers

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.

like image 179
Lord of Scripts Avatar answered Sep 26 '22 02:09

Lord of Scripts


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);
}
like image 33
Hans Passant Avatar answered Sep 24 '22 02:09

Hans Passant