C#.NET의 WebBrowser 컨트롤 이벤트를 잡는(Catch) 방법에 대한 설명이다.
[요구사항]
• Microsoft Visual Studio .NET
• Internet Explorer 5.5 Service Pack 2 (SP2) or later version
• Internet Explorer 5.5 Service Pack 2 (SP2) or later version
[설명]
The WebBrowser control is easy to work with in Microsoft Visual Studio or Visual Studio .NET. But handling the events of the WebBrowser control in Visual Studio .NET may be confusing at first. The events themselves are exposed by the Mshtml.HTMLDocumentEvents2_Event event interface. This interface exposes most of the document events that must be handled in your application.
To handle the event, you must create your own function that you can call when the event occurs. You must match the signature of the event that fires. For example, the following function handles the MouseOver event of the document:
To handle the event, you must create your own function that you can call when the event occurs. You must match the signature of the event that fires. For example, the following function handles the MouseOver event of the document:
WebBrowser의 대부분의 이벤트들은 "Mshtml.HTMLDocumentEvents2_Event" 이벤트 인터페이스를 통해 노출되어져있다.
이벤트를 핸들하기 위해서는 이벤트가 발생했을 때 동작할 메서드를 생성되어 있어야한다. 아래의 예를 보자.
사실... 이보다 먼저 알아야 할 것이 있다.
WebBrowser에서 발생하는 이벤트를 어떻게 Hooking 할 것이냐이다.
After the event handler is in position, you must hook the event. You can hook an event at any time after the DocumentComplete event on the WebBrowser control is triggered. To connect the sender of the event to your event handler, you must set up a delegate. Delegates are type-safe, security-enhanced, managed objects that point to a method. The following code example demonstrates how to connect the MouseOverEventHandler method to the MouseOver event of the document:
WebBrowser의 DocumentComplete 라는 이벤트를 통해서 가능하며, DocumentComplete 이벤트가 발생했을때 수행되는 메서드 안에서 이벤트 등록이 이루어진다. 아래의 예제는 MouseOver Event에 MouseOverEventHandler를 설정하는 예제이다.
mshtml.HTMLDocumentEvents2_Event iEvent;
iEvent.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(MouseOverEventHandler);
iEvent.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(MouseOverEventHandler);
[프로젝트 생성]
여기서 생성해볼 프로젝트는 WebBrowser컨트롤에 "Http://www.microsoft.com"페이지를 로드한 후, OnMouserOver와 OnClick 이벤트를 추가해 이벤트가 발행한 결과를 리스트 박스 출력하여 내용을 확인하는 것이다.
1. 새 프로젝트 생성.
2. 참조에 Microsoft.mshtml(내 컴퓨터의 경우 "Microsoft HTML Object Library") 을 추가한다. "COM" Tab에 있다.
3. 도구상자의 일반탭에서 "항목 추가/삭제" 메뉴를 선택하여 Microsoft Web Brower를 추가한다. "COM 구성요소" Tab에 있다.
4. 폼에 Microsoft Web Brower와 ListBox를 추가한다.
5. 폼을 더블 클릭하여 Form_Load 이벤트를 추가하고, 다음과 같이 코드를 추가한다.
Add the following code for the Form1_Load method: private void Form1_Load(object sender, System.EventArgs e)
{
object oURL = "http://www.microsoft.com";
object oEmpty = "";
axWebBrowser1.Navigate2(ref oURL, ref oEmpty, ref oEmpty, ref oEmpty, ref oEmpty);
}
{
object oURL = "http://www.microsoft.com";
object oEmpty = "";
axWebBrowser1.Navigate2(ref oURL, ref oEmpty, ref oEmpty, ref oEmpty, ref oEmpty);
}
6. WebBrowser의 DocumentComplete 이벤트를 등록하고 이벤트 핸들러에 다음과 같이 OnMouserOver와 OnClick이벤트 핸들러를 등록한다.
private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
mshtml.HTMLDocument doc;
doc = (mshtml.HTMLDocument)axWebBrowser1.Document;
mshtml.HTMLDocumentEvents2_Event iEvent;
iEvent = (mshtml.HTMLDocumentEvents2_Event) doc;
iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
iEvent.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(MouseOverEventHandler);
listBox1.Items.Clear();
}
{
mshtml.HTMLDocument doc;
doc = (mshtml.HTMLDocument)axWebBrowser1.Document;
mshtml.HTMLDocumentEvents2_Event iEvent;
iEvent = (mshtml.HTMLDocumentEvents2_Event) doc;
iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
iEvent.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(MouseOverEventHandler);
listBox1.Items.Clear();
}
7. 각 이벤트 핸들어에 리스트에 결과를 추가하는 코드를 추가한다.
private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
listBox1.Items.Insert(0, e.type + ":" + e.srcElement.tagName);
return true;
}
private void MouseOverEventHandler(mshtml.IHTMLEventObj e)
{
listBox1.Items.Insert(0, e.type + ":" + e.srcElement.tagName);
}
-. 에혀... 모라 했는지... 모르것다. 열심히 적었는데.+_+
못 알아들어도 할 수 없다 내 말이 무슨 말인지 모르면 아래 링크 걸어놓은 원문을 보고 해보라. ^^
How to handle document events in a Visual C# .NET application
View products that this article applies to.
Posted by salt202@nate.com

