0

I'm adding a user control as a topmost control inside a form (panel) like this:

        var ct = new UcMessage {
            Dock = DockStyle.Fill,
            Location = new Point(0, 0),
            Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
            Bounds = this.ClientRectangle,
            Size = this.ClientSize
        };
        this.Controls.Add(ct);
        ct.BringToFront();

It will work well and will cover the whole form. I'm using this code in User Control Constructor to set transparency:

        base.CreateParams.ExStyle |= 0x20;
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        BackColor = Color.FromArgb(0x80, 0xFF, 0xFF, 0xFF);

My form has a background image, after adding the user control as the topmost control, the transparency will work and I can see the form's background image, But there is a problem, I can not see other form's control. It seems all of them will be hidden after adding user control.

Where is the problem and how to solve it?

Inside Man
  • 4,194
  • 12
  • 59
  • 119
  • The Problem. In Winforms there is not __real__ but only __faked Transparency__. It will work __if and only if__ the control is __nested__ in another control. It will __not__ work between __overlapping controls__. Solution: There is basically no way around this.. – TaW Sep 07 '20 at 10:22
  • Also note: If you want security threre is no way around disabling the controls as the user can tab to them even without seeing them. – TaW Sep 07 '20 at 10:23
  • @TaW Yes, you can do it. You can overlap other Controls with a transparent/translucent Control. As shown here: [Translucent circular Control with text](https://stackoverflow.com/a/51435842/7444103). You can drag that Control over the Form and overlap any number of other Controls: these all are visible and with their Color shifted towards the Color used by translucent Control background (agreed that it's not true transparency, there's no discussion there). – Jimi Sep 07 '20 at 10:45
  • @Inside Man You need to both override `CreateParams`, setting `WS_EX_TRANSPARENT`, and set the appropriate styles in the Control's constructor: `SetStyle(ControlStyles.Opaque | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);` + `SetStyle(ControlStyles.OptimizedDoubleBuffer, false);` (can't double-buffer this, unless you handle the BufferedGraphics object yourself). Note that **your Control/UserControl doesn't have a BackGround anymore**, so you have to draw it yourself, using a Color with Alpha. Then, you can draw a transparent PNG on the translucent Control's surface. – Jimi Sep 07 '20 at 10:45
  • See the link I posted. => Drawback: drawing an animated Image is **very hard**, since you don't have double buffering. As mentioned, you should handle a [BufferedGraphics](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bufferedgraphics) object yourself (doing this correctly and with good results is the hard part). At Project's Design, you decide whether it's worth it. I prefer overlays. – Jimi Sep 07 '20 at 10:50
  • @Jimi I have found a dirty trick, Before adding the user control I will convert the form(panel) into an image, then I set the image as the form(panel) background image and then I load the user control. After closing the user control, again I will set the background image to null. what to you think? :) – Inside Man Sep 07 '20 at 11:07
  • 1
    Why not? Does it behave as expected and the quality is acceptable (or great :). Then go for it. Note that `Control.DrawToBitmap()` doesn't work for all Controls (RichTextBox, ProgressBar etc.), so you have to avoid those. Or, see this `ControlPrinter` class, in case you need it: [How to use PrintDocument with a scrollable Panel?](https://stackoverflow.com/a/57257205/7444103). It generates Bitmaps from ScrollableControls (Form, Panel) in the *correct order* and also draws the content of RichTextBox Controls. – Jimi Sep 07 '20 at 11:22
  • @Jimi thank you for your helps man :) could you please give it a look? https://stackoverflow.com/questions/63783445/wait-for-new-added-control-event-c-sharp – Inside Man Sep 07 '20 at 19:52

1 Answers1

-1

Try to remove this line of code: ct.BringToFront();

I tried your code and found that it works well without that line.

  • If I remove the `ct.BringToFront();`, then you can see all of the form's control and you can interact with them which I do not want. I want the user control to cover the whole form and you can not interact with the form's controls. just think the user control is a waiting bar. – Inside Man Sep 07 '20 at 05:34
  • @InsideMan as the user control is top most, the win form will not paint the other controls. – Basem Al-Bizreh Sep 07 '20 at 08:45
  • if you are trying to apply security on you project, I recommend creating a read-only form (by using labels to display the values) alongside the writable form. – Basem Al-Bizreh Sep 07 '20 at 09:06