C# WebBrowser(win form)不透明度

C# WebBrowser(win form)不透明度,c#,webbrowser-control,opacity,C#,Webbrowser Control,Opacity,我在另一个控件上有一个系统windows窗体web浏览器。web浏览器具有图像集 在web浏览器控件的顶部是一个继承的面板,我可以在其中控制不透明度 如果我使其透明,它不会显示web浏览器图像,而是显示web浏览器下控件的背景色 即 第1层控制。背景颜色是蓝色 图像为HTML的第二层web浏览器 第3层透明面板 第3层透明时显示第1层而不是第2层 如何使web浏览器不透明,以便第3层显示到第2层(webbrowser) 我尝试设置SetStyles来控制web浏览器上的不透明度 谢谢简短回答:

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

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

如果我使其透明,它不会显示web浏览器图像,而是显示web浏览器下控件的背景色

即 第1层控制。背景颜色是蓝色 图像为HTML的第二层web浏览器 第3层透明面板

第3层透明时显示第1层而不是第2层

如何使web浏览器不透明,以便第3层显示到第2层(webbrowser)

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


谢谢

简短回答:

你不能

现实世界解决方案:

你调查过WPF吗

长答案:

不透明度是WinForms的一个技巧

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

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

  • 创建一个新的winforms项目
  • 创建2个标签,将它们放置在彼此的顶部(稍微偏移)
  • 将两个标签背景设置为
    Color.Transparent
  • 您将立即看到透明度从下面的标签中去掉一大块

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

    使用系统;
    使用System.Windows.Forms;
    使用系统图;
    命名空间Windows窗体应用程序5
    {
    静态类程序
    {
    /// 
    ///应用程序的主要入口点。
    /// 
    [状态线程]
    静态void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    运行(新的MainForm());
    }
    }
    公共类主窗体:窗体
    {
    私有随机=新随机();
    私人按钮btnRandomBackgroundColor;
    自有品牌lblBackgroundLabel;
    自有品牌lblttransparent;
    公共表格(
    {
    初始化组件();
    }
    私有无效按钮\u单击(对象发送者,事件参数e)
    {
    BackColor=Color.FromArgb(random.Next(0255),
    随机。下一个(0255),
    下一个(0255));
    }
    私有void InitializeComponent()
    {
    this.btnRandomBackgroundColor=new System.Windows.Forms.Button();
    this.lblBackgroundLabel=new System.Windows.Forms.Label();
    this.lblttransparent=new System.Windows.Forms.Label();
    这个.SuspendLayout();
    // 
    //BTN随机背景色
    // 
    this.btnRandomBackgroundColor.Location=新系统.图纸.点(12,12);
    this.btnRandomBackgroundColor.Name=“btnRandomBackgroundColor”;
    this.btnRandomBackgroundColor.Size=新系统.图纸.尺寸(144,23);
    this.btnRandomBackgroundColor.TabIndex=0;
    this.btnRandomBackgroundColor.Text=“随机化背景色”;
    this.btnRandomBackgroundColor.UseVisualStyleBackColor=true;
    this.btnRandomBackgroundColor.Click+=按钮\u单击;
    // 
    //lblBackgroundLabel
    // 
    this.lblBackgroundLabel.AutoSize=true;
    this.lblBackgroundLabel.BackColor=System.Drawing.Color.Transparent;
    this.lblBackgroundLabel.Font=new System.Drawing.Font(“Microsoft无衬线”,21.75F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(0));
    this.lblBackgroundLabel.Location=新系统.图纸.点(41,49);
    this.lblBackgroundLabel.Name=“lblBackgroundLabel”;
    this.lblBackgroundLabel.Size=新系统图纸尺寸(184,33);
    this.lblBackgroundLabel.TabIndex=1;
    this.lblBackgroundLabel.Text=“简单标签”;
    // 
    //透明的
    // 
    this.lblttransparent.AutoSize=true;
    this.lblttransparent.BackColor=System.Drawing.Color.Transparent;
    this.lblttransparent.Font=new System.Drawing.Font(“Microsoft Sans Serif”,21.75F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(0));
    this.lblttransparent.Location=新系统.图纸.点(61,63);
    this.lblttransparent.Name=“lblttransparent”;
    this.lblttransparent.Size=新系统图尺寸(251,33);
    this.lblttransparent.TabIndex=2;
    this.lblttransparent.Text=“透明标签”;
    // 
    //表格1
    // 
    此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
    this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
    this.BackColor=System.Drawing.Color.White;
    this.ClientSize=新系统图尺寸(341114);
    控件。添加(this.lblttransparent);
    this.Controls.Add(this.lblBackgroundLabel);
    this.Controls.Add(this.btnRandomBackgroundColor);
    this.Name=“Form1”;
    this.Text=“MainForm”;
    此选项为.resume布局(false);
    这个。执行布局();
    }
    }
    }
    
    是否要将不透明设置为不同的%?如果仅为100%或0%,则可以将webBrowser.Visible设置为可见。
    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();
    
            }
        }
    }