WebBrowser(胜利形式)不透明度

时间:2013-02-13 21:59:18

标签: c# webbrowser-control opacity

我在另一个控件之上有一个系统Windows窗体Web浏览器。 Web浏览器具有图像集。

在Web浏览器控件之上是一个继承的面板,我可以在其中控制不透明度。

如果我将其设为透明,则不显示Web浏览器图像,它会在Web浏览器下显示控件的背景颜色。

即 Layer1控件。背面颜色为蓝色 Layer2 Web浏览器,图像为HTML Layer3透明面板

透明时的Layer3显示layer1而不是layer2

如何让网页浏览器不透明,以便第3层显示到第2层(webbrowser)?

我尝试过设置SetStyles来控制网络浏览器的不透明度。

由于

1 个答案:

答案 0 :(得分:0)

简答:

你不能。

真实世界解决方案:

你看过WPF吗?

长答案:

不透明度是WinForms的一个技巧。

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

这在一个简单的例子中变得非常明显:

  1. 创建一个新的winforms项目
  2. 创建2个标签,将它们放在彼此的顶部(略微偏移)
  3. 将两个标签背景设置为Color.Transparent
  4. 你会立即看到透明度从下面的标签中取出一块。

    示例代码(一体化文件,编译并运行):

    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();
    
            }
        }
    }