无法在c#winapp

时间:2015-09-28 12:47:30

标签: c# webbrowser-control panel

我无法在winapp中向web面板添加webbrowser控件,收到此错误“无法获取'WebBrowser'控件的窗口句柄。不支持无窗口ActiveX控件。”我也尝试使用STAthread来执行此操作/ p>

Thread uy = new Thread(me_p);
uy.SetApartmentState(ApartmentState.STA);
uy.Start();

private void me_p(object obj)
{
//throw new NotImplementedException();
this.p_bottom.Controls.Add(this.webBrowser2);         
}

1 个答案:

答案 0 :(得分:0)

以这种方式工作正常

private void InitializeComponent()
{
    this.panel1 = new System.Windows.Forms.Panel();
    this.webBrowser1 = new System.Windows.Forms.WebBrowser();
    this.panel1.SuspendLayout();
    this.SuspendLayout();
    // 
    // panel1
    // 
    this.panel1.Controls.Add(this.webBrowser1);
    this.panel1.Location = new System.Drawing.Point(47, 149);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(200, 100);
    this.panel1.TabIndex = 0;
    // 
    // webBrowser1
    // 
    this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.webBrowser1.Location = new System.Drawing.Point(0, 0);
    this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
    this.webBrowser1.Name = "webBrowser1";
    this.webBrowser1.Size = new System.Drawing.Size(200, 100);
    this.webBrowser1.TabIndex = 0;
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 261);
    this.Controls.Add(this.panel1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.panel1.ResumeLayout(false);
    this.ResumeLayout(false);
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.WebBrowser webBrowser1;

您可以预先初始化浏览器,然后在线程中使用它。