2015-09-28 37 views
-1

我無法添加webbrowser控件到winapp中的面板,出現此錯誤「無法獲取'WebBrowser'控件的窗口句柄。不支持無窗口ActiveX控件。 「我也嘗試過使用STAthread要做得像無法在c#中插入webbrowser面板winapp

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

你多久想問這個問題? – fubo

+1

[在c#中插入webbrowser內部面板時出現錯誤]的可能重複(http://stackoverflow.com/questions/32820005/getting-error-on-inserting-webbrowser-inside-panel-in-c-sharp) – fubo

+0

yes我問過,但我沒有得到答案,所以試圖用不同的方式解釋它。 –

回答

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; 

你可以有一個瀏覽器預先初始化,後來在你的線程使用它時,它工作正常。