If I understand correctly, you want to "darken" the contents of the form during the operation.
As someone said before, it is very difficult to do it right. But there is a way to do this easily, with one caveat (see below).
Take a look at this source code:
public partial class Form1 : Form { private Bitmap _background; private bool _isShrouded; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (true == _isShrouded && null!=_background) e.Graphics.DrawImage(_background, 0, 0); } public void Shroud() { if (false == _isShrouded) { CreateScreenshot(); HideControls(); _isShrouded = true; this.Invalidate(); } } public void Unshroud() { if (true == _isShrouded) { ShowControls(); _isShrouded = false; this.Invalidate(); } } private void HideControls() { foreach (Control control in this.Controls) control.Visible = false; } private void ShowControls() { foreach (Control control in this.Controls) control.Visible = true; } private void CreateScreenshot() { Rectangle area = this.RectangleToScreen(this.ClientRectangle); Bitmap screenGrab = new Bitmap(area.Width, area.Height); Brush dark = new SolidBrush(Color.FromArgb(128, Color.Black)); Graphics g = Graphics.FromImage(screenGrab); g.CopyFromScreen(area.Location, Point.Empty, area.Size); g.FillRectangle(dark, 0, 0, area.Width, area.Height); g.Dispose(); _background = screenGrab; } }
The Form1 class has two main methods: Shroud () and Unshroud ().
The Shroud () method takes a snapshot of the form and copies it into a bitmap, which is then darkened. The controls are then hidden and the bitmap is drawn on the form.
The UnShroud () method restores the controls and tells the form to no longer draw a bitmap.
This requires two private variables: one for storing the bitmap image and a flag that maintains the current state.
It also overrides OnPaint () because it needs to draw a background image when it is βshroudedβ.
Note: Cladding works by taking a screenshot of the form. This means that the form MUST BE the uppermost form at the wrapping point. If the form is closed by other forms, they will be included in the screenshot. I hope this will not be a problem for you.
Note. . As already mentioned, the only way to achieve transparency in Windows is to fully cooperate with all the controls involved and a difficult task. Everything else (including this solution) is really just a hoax.
Edwin groenendaal
source share