简体   繁体   中英

WebBrowser (win form) opacity

I have a system windows forms web browser on top of another control. The web browser has an image set.

On top of the web browser control is an inherited panel where I can control the opacity.

If I make this transparent, it doesn't show the web browser image, it shows the back colour of the control under the web browser.

Ie Layer1 control. Back colour is blue Layer2 web browser with image as HTML Layer3 transparent panel

Layer3 when transparent shows layer1 not layer2

How can I make the web browser opaque so layer3 shows through to layer2 (webbrowser) ?

I have tried setting SetStyles to control opacity on the web browser.

Thanks

Short Answer:

You can't.

Real World Solution:

Have you looked into WPF?

Long Answer:

Opacity is a trick that WinForms does.

When a control is marked to be displayed transparent (or semi transparent), WinForms queries the parent object's visuals to ask "What would you have printed here had my control not existed?". From this result WinForms either displays the pixel of the control, the pixel of the parent, or a combination of the two (if semi transparent).

This becomes really apparent in a simple example:

  1. Create a new winforms project
  2. Create 2 labels, place them over top of each other (slighty offset)
  3. Set both labels backgrounds to Color.Transparent

You'll see immediately that transparency takes a chunk out of the label underneath.

Example Code (All in one file, compile and run):

using System;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication5
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
    public class MainForm : Form
    {
        private Random random = new Random();
        private Button btnRandomBackgroundColor;
        private Label lblBackgroundLabel;
        private Label lblTransparent;

        public MainForm()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, EventArgs e)
        {
            BackColor = Color.FromArgb(random.Next(0, 255),
                                            random.Next(0, 255),
                                            random.Next(0, 255));
        }

        private void InitializeComponent()
        {
            this.btnRandomBackgroundColor = new System.Windows.Forms.Button();
            this.lblBackgroundLabel = new System.Windows.Forms.Label();
            this.lblTransparent = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // btnRandomBackgroundColor
            // 
            this.btnRandomBackgroundColor.Location = new System.Drawing.Point(12, 12);
            this.btnRandomBackgroundColor.Name = "btnRandomBackgroundColor";
            this.btnRandomBackgroundColor.Size = new System.Drawing.Size(144, 23);
            this.btnRandomBackgroundColor.TabIndex = 0;
            this.btnRandomBackgroundColor.Text = "Randomize Background Color";
            this.btnRandomBackgroundColor.UseVisualStyleBackColor = true;
            this.btnRandomBackgroundColor.Click += button_Click;
            // 
            // lblBackgroundLabel
            // 
            this.lblBackgroundLabel.AutoSize = true;
            this.lblBackgroundLabel.BackColor = System.Drawing.Color.Transparent;
            this.lblBackgroundLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblBackgroundLabel.Location = new System.Drawing.Point(41, 49);
            this.lblBackgroundLabel.Name = "lblBackgroundLabel";
            this.lblBackgroundLabel.Size = new System.Drawing.Size(184, 33);
            this.lblBackgroundLabel.TabIndex = 1;
            this.lblBackgroundLabel.Text = "Simple Label";
            // 
            // lblTransparent
            // 
            this.lblTransparent.AutoSize = true;
            this.lblTransparent.BackColor = System.Drawing.Color.Transparent;
            this.lblTransparent.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblTransparent.Location = new System.Drawing.Point(61, 63);
            this.lblTransparent.Name = "lblTransparent";
            this.lblTransparent.Size = new System.Drawing.Size(251, 33);
            this.lblTransparent.TabIndex = 2;
            this.lblTransparent.Text = "Transparent Label";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(341, 114);
            Controls.Add(this.lblTransparent);
            this.Controls.Add(this.lblBackgroundLabel);
            this.Controls.Add(this.btnRandomBackgroundColor);
            this.Name = "Form1";
            this.Text = "MainForm";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM