WinForm control with opacity

I have a form that has some controls (btnCreateReport, pnlDarkLayer). I have a panel that is suitable for shaping (Dock = Fill), and it is on the back of all controls. When the user clicks the btnCreateReport button, I call the pnlDarkLayer BringToFront method, and after some calculations, I call the SendToBack () method of the button. I want to draw a dark layer on the form elements and disable all the controls on the form. Is it possible? Thanks.

Perhaps this code will help you understand my purpose:

private void btnCreateReport_Click(object sender, EventArgs e) { pnlDarkLayer.BackColor = Color.FromArgb(100, Color.Gray); pnlDarkLayer.BringToFront(); btnCreateReport.Enabled = false; Thread ProcessReport = new Thread(new ThreadStart(ProcessingReport)); ProcessReport.Start(); while (ProcessReport.IsAlive) { Application.DoEvents(); } pnlDarkLayer.SendToBack(); btnCreateReport.Enabled = true; } 

This code hide all controls, but I do not want to hide form controls. I want to draw a dark layer on them. And the user should see the controls. I need something like the opacity property of forms for their controls.

I have a test:

pnlDarkLayer.CreateGraphics().CompositingMode=System.Drawing.Drawing2D.CompositingMode.SourceOver;

Update : I have a test: (use the form instead of the panel)

 private void btnCreateReport_Click(object sender, EventArgs e) { btnCreateReport.Enabled = false; frmProgress ProgressForm = new frmProgress(); ProgressForm.TopLevel = false; ProgressForm.Parent = this; ProgressForm.BringToFront(); this.Controls.Add(ProgressForm); ProgressForm.Show(); Thread ProcessReport = new Thread(new ThreadStart(ProcessingReport)); ProcessReport.Start(); while (ProcessReport.IsAlive) { Application.DoEvents(); } ProgressForm.Close(); btnCreateReport.Enabled = true; } 

But I can not see the ProgressForm in my form.

+7
source share
2 answers

From http://support.microsoft.com/kb/943454

Transparent controls in WinForms are transparent with respect to their parents, and not to other controls. Transparency in WinForms is more akin to camouflage than true transparency. Transparent control does not actually allow you to see control over it through the form. He asks his parent his own background on a "transparent" control. This is why transparent control shows the form behind it, but covers any other controls.

In order to implement transparency with respect to other controls, the same thing needs to be done on a larger scale: instead, just ask the parent to draw the background of the control's foreground, the control must request all the controls behind it to build on its background. This will only work for controls that provide some method for the request they will be drawn and will not be automatically updated when the background controls image changes.

The page also provides sample code (in vb, unfortunately) to show how this is done.

+6
source

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.

+5
source

All Articles