繁体   English   中英

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

[英]WebBrowser (win form) opacity

我在另一个控件的顶部有一个系统Windows窗体Web浏览器。 Web浏览器有一个图像集。

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

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

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

当透明显示Layer1而不是Layer2时的Layer3

如何使Web浏览器不透明,以便layer3可以显示到layer2(webbrowser)?

我尝试设置SetStyles来控制Web浏览器上的不透明度。

谢谢

简短答案:

你不能

实际解决方案:

您是否研究过WPF?

长答案:

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

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

在一个简单的示例中,这确实变得显而易见:

  1. 创建一个新的winforms项目
  2. 创建2个标签,将它们彼此叠置(略有偏移)
  3. 将两个标签的背景都设置为Color.Transparent

您会立即看到透明度从下面的标签中取出了一大块。

示例代码 (全部包含在一个文件中,编译并运行):

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