如何在不使用Winform的情况下使用WebBrowser [英] How Use WebBrowser without winform
问题描述
大家好,
我想在简单的类中使用(不是winform).我认为我必须使用线程来运行WebBowser.
我在webbrowser和线程之间有一个例外
Hi guys,
I want to use in simple class ( not winform). I think that i must use thread to run WebBowser.
I have an exception between webbrowser and thread
Public Class CScenTest
Private Dim m_WebBrowser As WebBrowser
Private Dim m_test as Boolean
private sub init
AddHandler m_WebBrowser.DocumentCompleted, AddressOf m_WebBrowser_DocumentCompleted
end sub
Private Sub Execute()
Dim l_Thread As Thread
l_Thread = New Thread(AddressOf navigeUrl)
l_Thread.Start()
End Sub
private Sub m_WebBrowser_DocumentCompleted(sender As object, e As WebBrowserDocumentCompletedEventArgs)
''I want to go here
m_test = true
End Sub
Private sub navigeUrl()
m_WebBrowser.Navigate("www.yahoo.com")
End Sub
End Class
推荐答案
在调试过程中摸索了应用程序给我的各种响应之后,然后在互联网上进行了搜索,最后我想到了这个解决方案,可以在没有表单的情况下托管您的Web浏览器控件.由于我不习惯在VB.NET中编写程序,因此我将用C#进行工作.如果您在将其转换为VB.net时遇到任何困难,也许您会在CP上找到一些帮助.无论如何,在C#中进行尝试都不会造成问题.这么多字,这是代码:
After scratching my head some over the various responses the application gave me during debugging and then googling some on the internets I finally came up with this solution that works without a form to host your webbrowser control. Since I''m not used to writing programs in VB.NET I''ll give you my work in C#. If you have any difficulties translating it into VB.net maybe you''ll find some assistance here on CP. Anyhow trying it out in C# should not pose a problem. So many words, here is the code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WebBrowserWithoutAForm
{
class Program
{
private static bool completed = false;
private static WebBrowser wb;
[STAThread]
static void Main(string[] args)
{
wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate("http://www.google.com");
while (!completed)
{
Application.DoEvents();
Thread.Sleep(100);
}
Console.Write("\n\nDone with it!\n\n");
Console.ReadLine();
}
static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Console.WriteLine(wb.Document.Body.InnerHtml);
completed = true;
}
}
}
我知道使用Application.DoEvents()可能会收到一些抨击,但它仍然可以正常工作,并且可以满足您不希望将表单用作容器的请求. WebBrowser控件所在的位置仍然必须包含System.Windows.Forms.
提示:初始化WebBroswer控件时,过程main的STAThread属性是必不可少的,因为在多线程单元模式下不允许这样做.
除了使用Application.DoEvents()之外,还有其他方法可以实现和设置自己的消息泵.如果时间允许,我将对此进行调查并报告我的发现 [/Edit]
编码愉快,玩得开心!
-MRB
I know I may receive some bashing over using Application.DoEvents() but it still works though and fullfills your request of not wanting to use a form as a container. System.Windows.Forms still has to be included though as that is where the WebBrowser control lives.
Hint: The STAThread attribute of procedure main is essential when initializing the WebBroswer control as this is not allowed in Multi-Threaded Appartment Mode.
There may be some other way than using Application.DoEvents() which would involve implementing and setting up one''s own message pump. As time allows I will look into this and report on my findings[/Edit]
Happy coding and have fun!
-MRB
然后将您的应用程序与
一起使用客户端浏览器
then force your application to use the clients browser with
System.Diagnostics.Process.Start("http://www.website.com")
WebBrowser
控件就是这样-一个控件.必须将其放在容器(窗体)中才能工作.如果您不希望在窗体上使用边框或标题栏,则可以这样做,但是必须将控件放入窗体中才能使用.
The WebBrowser
control is just that - a control. It has to be placed in a container (a form) to work. If you don''t wanta border or titlebar on the form, you can do that, but you must put the control into a form in order to use it.
这篇关于如何在不使用Winform的情况下使用WebBrowser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!