简体   繁体   English

Web浏览器(胜利形式)的不透明度

[英]WebBrowser (win form) opacity

I have a system windows forms web browser on top of another control. 我在另一个控件的顶部有一个系统Windows窗体Web浏览器。 The web browser has an image set. Web浏览器有一个图像集。

On top of the web browser control is an inherited panel where I can control the opacity. Web浏览器控件的顶部是一个继承的面板,我可以在其中控制不透明度。

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. 如果将其设为透明,则不会显示Web浏览器图像,而是显示Web浏览器下控件的背景色。

Ie Layer1 control. 即Layer1控件。 Back colour is blue Layer2 web browser with image as HTML Layer3 transparent panel 背面为蓝色Layer2 Web浏览器,图像为HTML Layer3透明面板

Layer3 when transparent shows layer1 not layer2 当透明显示Layer1而不是Layer2时的Layer3

How can I make the web browser opaque so layer3 shows through to layer2 (webbrowser) ? 如何使Web浏览器不透明,以便layer3可以显示到layer2(webbrowser)?

I have tried setting SetStyles to control opacity on the web browser. 我尝试设置SetStyles来控制Web浏览器上的不透明度。

Thanks 谢谢

Short Answer: 简短答案:

You can't. 你不能

Real World Solution: 实际解决方案:

Have you looked into WPF? 您是否研究过WPF?

Long Answer: 长答案:

Opacity is a trick that WinForms does. 不透明度是WinForms的一个技巧。

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?". 当控件被标记为显示为透明(或半透明)时,WinForms会查询​​父对象的视觉效果,询问“如果不存在我的控件,您将在此处打印什么?”。 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). 从此结果中,WinForms要么显示控件的像素,要么显示父级的像素,要么显示两者的组合(如果是半透明的)。

This becomes really apparent in a simple example: 在一个简单的示例中,这确实变得显而易见:

  1. Create a new winforms project 创建一个新的winforms项目
  2. Create 2 labels, place them over top of each other (slighty offset) 创建2个标签,将它们彼此叠置(略有偏移)
  3. Set both labels backgrounds to Color.Transparent 将两个标签的背景都设置为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();

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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