我使用此代码在新主题中创建Web浏览器控件:
void Exec(string url)
{
var th = new Thread(() =>
{
using (WebBrowser wb = new WebBrowser())
{
wb.DocumentCompleted += (sndr, e) =>
{
// Do something when completed
wb.Dispose();
Application.ExitThread();
};
wb.Navigate(url);
Application.Run();
}
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join();
}
现在每次调用此方法时,它都会创建Web浏览器控件的新实例。 我的问题是,当创建新实例时,是否可以在表单上绘制/重新绘制控件?
答案 0 :(得分:0)
试试这个。可能会进行优化。
void Exec(string url)
{
var th = new Thread(() =>
{
using (WebBrowser wb = new WebBrowser())
{
wb.Name = "webBrowserGenerated" + Guid.NewGuid();
List<Control> list = new List<Control>();
GetAllControl(this, list);
foreach (Control control in list)
{
if (control.GetType() == typeof(WebBrowser))
{
if (control.Name.StartsWith("webBrowserGenerated"))
{
control.Refresh();
}
}
}
wb.DocumentCompleted += (sndr, e) =>
{
// Do something when completed
wb.Dispose();
Application.ExitThread();
};
wb.Navigate(url);
Application.Run();
}
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join();
}
private void GetAllControl(Control c, List<Control> list)
{
foreach (Control control in c.Controls)
{
list.Add(control);
if (control.GetType() == typeof(Panel))
GetAllControl(control, list);
}
}