Windows Forms WebBrowser

29.1 How can I host a WebBrowser control in a Windows Form?

We have two suggestions with sample projects how you host a WebBrowser control inside a form and display HTML contents and listen to events such as NavigateComplete or BeforeNavigate. Of course there are many other ways to do this.

Download htmlviewer.zip for two sample projects for the suggestions discussed below.

1) The first suggestion is to generate an ActiveX wrapper for shdocvw using the aximp tool.
The command line for this tool should be as follows:

aximp c:\windows\system32\shdocvw.dll

This will generate the following assemblies.

Generated Assembly: D:\Syncfusion\faq\HtmlBrowser\HtmlViewer2\SHDocVw.dll

Generated Assembly: D:\Syncfusion\faq\HtmlBrowser\HtmlViewer2\AxSHDocVw.dll

Now you can reference these dlls in your project and use AxWebBrowser. In the attached HtmlViewer2 sample we have derived a HtmlControl class from AxWebBrowser and added some properties that let you specify a CSS Stylesheet and the Html content as a string.

2) Our second sample lets you bypass the generation of a ActiveX wrapper. You don't have to include and ship shdocvw.dll and axshdocvw.dll. In the attached HtmlViewer sample, we derived from AxHost and attached our own IWebBrowserEvents interface by overriding the CreateSink, AttachInterfaces and DetachSink methods.

You can use HtmlControl in your form and specify HTML content by assigning a HTML string to HtmlControl. A cascading style sheet can be specified by assigning a path name to the CascadingStyleSheet property. The sample demonstrates how to use a CSS style sheet that has been embedded as a resource in the assembly.

29.2 How can I enable editing for the WebBrowser?

You can set the content editable by getting a reference to a IHTMLElement3 element and initializing the contentEditable property with "true".

private void EnableEditing()

{

IHTMLDocument3 doc = (IHTMLDocument3) browser.GetDocument();

if (doc != null)

{

IHTMLElement3 el = (IHTMLElement3) doc.GetBody();

el.contentEditable = "true";

}

}

If you want to read out contents of a specific element use an id in the html source:

EditableText

From CSharp you can reference this text with

IHTMLElement3 el = (IHTMLElement3) doc.getElementById("EditText");

MessageBox.Show(el.GetInnerText());

MessageBox.Show(el.GetInnerHTML());

You can use IHTMLDocument3 and IHTMLElement3 interface definitions from the imported type library dll C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll

[

Guid(@"3050F485-98B5-11CF-BB82-00AA00BDCE0B"),

TypeLibType(TypeLibTypeFlags.FDispatchable|TypeLibTypeFlags.FDual)

]

public interface IHTMLDocument3 : IHTMLDocument2

{

[DispId(1072 /*0x0430*/)]

void releaseCapture();

[DispId(1073 /*0x0431*/)]

void recalc(bool fForce);

[DispId(1074 /*0x0432*/)]

[return: MarshalAs(UnmanagedType.Interface)]

object createTextNode(string text);

[DispId(1075 /*0x0433*/)]

IHTMLElement documentElement

{

[return: MarshalAs(UnmanagedType.Interface)] get; }

[DispId(1077 /*0x0435*/)]

string uniqueID

{

[return: MarshalAs(UnmanagedType.BStr)] get; }

[DispId(-2147417605 /*0x800101FB*/)]

bool attachEvent(string _event, object pdisp);

[DispId(-2147417604 /*0x800101FC*/)]

void detachEvent(string _event, object pdisp);

[DispId(-2147412050 /*0x800117AE*/)]

object onrowsdelete

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412049 /*0x800117AF*/)]

object onrowsinserted

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412048 /*0x800117B0*/)]

object oncellchange

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412072 /*0x80011798*/)]

object ondatasetchanged

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412071 /*0x80011799*/)]

object ondataavailable

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412070 /*0x8001179A*/)]

object ondatasetcomplete

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412065 /*0x8001179F*/)]

object onpropertychange

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412995 /*0x800113FD*/)]

string dir

{

set; [return: MarshalAs(UnmanagedType.BStr)] get; }

[DispId(-2147412047 /*0x800117B1*/)]

object oncontextmenu

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412044 /*0x800117B4*/)]

object onstop

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(1076 /*0x0434*/)]

[return: MarshalAs(UnmanagedType.Interface)]

IHTMLDocument2 createDocumentFragment();

[DispId(1078 /*0x0436*/)]

IHTMLDocument2 parentDocument

{

[return: MarshalAs(UnmanagedType.Interface)] get; }

[DispId(1079 /*0x0437*/)]

bool enableDownload

{

set; get; }

[DispId(1080 /*0x0438*/)]

string baseUrl

{

set; [return: MarshalAs(UnmanagedType.BStr)] get; }

[DispId(-2147417063 /*0x80010419*/)]

object childNodes

{

[return: MarshalAs(UnmanagedType.IDispatch)] get; }

[DispId(1082 /*0x043A*/)]

bool inheritStyleSheets

{

set; get; }

[DispId(-2147412043 /*0x800117B5*/)]

object onbeforeeditfocus

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(1086 /*0x043E*/)]

[return: MarshalAs(UnmanagedType.Interface)]

object getElementsByName(string v);

[DispId(1088 /*0x0440*/)]

[return: MarshalAs(UnmanagedType.Interface)]

IHTMLElement getElementById(string v);

[DispId(1087 /*0x043F*/)]

[return: MarshalAs(UnmanagedType.Interface)]

object getElementsByTagName(string v);

}

[

InterfaceType(ComInterfaceType.InterfaceIsDual),

ComVisible(true),

Guid(@"332C4425-26CB-11D0-B483-00C04FD90119")

]

public interface IHTMLDocument2

{

[return: MarshalAs(UnmanagedType.Interface)]

object GetScript();

[return: MarshalAs(UnmanagedType.Interface)]

object GetAll();

[return: MarshalAs(UnmanagedType.Interface)]

IHTMLElement GetBody();

[return: MarshalAs(UnmanagedType.Interface)]

object GetActiveElement();

[return: MarshalAs(UnmanagedType.Interface)]

object GetImages();

[return: MarshalAs(UnmanagedType.Interface)]

object GetApplets();

[return: MarshalAs(UnmanagedType.Interface)]

object GetLinks();

[return: MarshalAs(UnmanagedType.Interface)]

object GetForms();

[return: MarshalAs(UnmanagedType.Interface)]

object GetAnchors();

void SetTitle(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetTitle();

[return: MarshalAs(UnmanagedType.Interface)]

object GetScripts();

void SetDesignMode(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetDesignMode();

[return: MarshalAs(UnmanagedType.Interface)]

object GetSelection();

[return: MarshalAs(UnmanagedType.BStr)]

string GetReadyState();

[return: MarshalAs(UnmanagedType.Interface)]

object GetFrames();

[return: MarshalAs(UnmanagedType.Interface)]

object GetEmbeds();

[return: MarshalAs(UnmanagedType.Interface)]

object GetPlugins();

void SetAlinkColor(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetAlinkColor();

void SetBgColor(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetBgColor();

void SetFgColor(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetFgColor();

void SetLinkColor(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetLinkColor();

void SetVlinkColor(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetVlinkColor();

[return: MarshalAs(UnmanagedType.BStr)]

string GetReferrer();

[return: MarshalAs(UnmanagedType.Interface)]

object GetLocation();

[return: MarshalAs(UnmanagedType.BStr)]

string GetLastModified();

void SetURL(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetURL();

void SetDomain(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetDomain();

void SetCookie(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetCookie();

void SetExpando(bool p);

[return: MarshalAs(UnmanagedType.Bool)]

bool GetExpando();

void SetCharset(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetCharset();

void SetDefaultCharset(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetDefaultCharset();

[return: MarshalAs(UnmanagedType.BStr)]

string GetMimeType();

[return: MarshalAs(UnmanagedType.BStr)]

string GetFileSize();

[return: MarshalAs(UnmanagedType.BStr)]

string GetFileCreatedDate();

[return: MarshalAs(UnmanagedType.BStr)]

string GetFileModifiedDate();

[return: MarshalAs(UnmanagedType.BStr)]

string GetFileUpdatedDate();

[return: MarshalAs(UnmanagedType.BStr)]

string GetSecurity();

[return: MarshalAs(UnmanagedType.BStr)]

string GetProtocol();

[return: MarshalAs(UnmanagedType.BStr)]

string GetNameProp();

void DummyWrite(int psarray);

void DummyWriteln(int psarray);

[return: MarshalAs(UnmanagedType.Interface)]

object Open(string URL, object name, object features, object replace);

void Close();

void Clear();

[return: MarshalAs(UnmanagedType.Bool)]

bool QueryCommandSupported(string cmdID);

[return: MarshalAs(UnmanagedType.Bool)]

bool QueryCommandEnabled(string cmdID);

[return: MarshalAs(UnmanagedType.Bool)]

bool QueryCommandState(string cmdID);

[return: MarshalAs(UnmanagedType.Bool)]

bool QueryCommandIndeterm(string cmdID);

[return: MarshalAs(UnmanagedType.BStr)]

string QueryCommandText(string cmdID);

[return: MarshalAs(UnmanagedType.Struct)]

object QueryCommandValue(string cmdID);

[return: MarshalAs(UnmanagedType.Bool)]

bool ExecCommand(string cmdID, bool showUI, object value);

[return: MarshalAs(UnmanagedType.Bool)]

bool ExecCommandShowHelp(string cmdID);

[return: MarshalAs(UnmanagedType.Interface)]

object CreateElement(string eTag);

void SetOnhelp(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnhelp();

void SetOnclick(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnclick();

void SetOndblclick(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOndblclick();

void SetOnkeyup(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnkeyup();

void SetOnkeydown(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnkeydown();

void SetOnkeypress(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnkeypress();

void SetOnmouseup(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmouseup();

void SetOnmousedown(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmousedown();

void SetOnmousemove(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmousemove();

void SetOnmouseout(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmouseout();

void SetOnmouseover(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmouseover();

void SetOnreadystatechange(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnreadystatechange();

void SetOnafterupdate(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnafterupdate();

void SetOnrowexit(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnrowexit();

void SetOnrowenter(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnrowenter();

void SetOndragstart(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOndragstart();

void SetOnselectstart(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnselectstart();

[return: MarshalAs(UnmanagedType.Interface)]

object ElementFromPoint(int x, int y);

[return: MarshalAs(UnmanagedType.Interface)]

object GetParentWindow();

[return: MarshalAs(UnmanagedType.Interface)]

object GetStyleSheets();

void SetOnbeforeupdate(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnbeforeupdate();

void SetOnerrorupdate(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnerrorupdate();

[return: MarshalAs(UnmanagedType.BStr)]

string toString();

[return: MarshalAs(UnmanagedType.Interface)]

object CreateStyleSheet(string bstrHref, int lIndex);

}

[

Guid(@"3050F434-98B5-11CF-BB82-00AA00BDCE0B"),

TypeLibType(TypeLibTypeFlags.FDispatchable|TypeLibTypeFlags.FDual)

]

public interface IHTMLElement2 : IHTMLElement

{

// Methods

[DispId(-2147417073 /*0x8001040F*/)]

string scopeName

{

[return: MarshalAs(UnmanagedType.BStr)] get; }

[DispId(-2147417072 /*0x80010410*/)]

void setCapture(bool containerCapture);

[DispId(-2147417071 /*0x80010411*/)]

void releaseCapture();

[DispId(-2147412066 /*0x8001179E*/)]

object onlosecapture

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417070 /*0x80010412*/)]

[return: MarshalAs(UnmanagedType.BStr)]

string componentFromPoint(int x, int y);

[DispId(-2147417069 /*0x80010413*/)]

void doScroll(object component);

[DispId(-2147412081 /*0x8001178F*/)]

object onscroll

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412063 /*0x800117A1*/)]

object ondrag

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412062 /*0x800117A2*/)]

object ondragend

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412061 /*0x800117A3*/)]

object ondragenter

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412060 /*0x800117A4*/)]

object ondragover

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412059 /*0x800117A5*/)]

object ondragleave

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412058 /*0x800117A6*/)]

object ondrop

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412054 /*0x800117AA*/)]

object onbeforecut

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412057 /*0x800117A7*/)]

object oncut

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412053 /*0x800117AB*/)]

object onbeforecopy

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412056 /*0x800117A8*/)]

object oncopy

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412052 /*0x800117AC*/)]

object onbeforepaste

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412055 /*0x800117A9*/)]

object onpaste

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417105 /*0x800103EF*/)]

object currentStyle

{

[return: MarshalAs(UnmanagedType.Interface)] get; }

[DispId(-2147412065 /*0x8001179F*/)]

object onpropertychange

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417068 /*0x80010414*/)]

[return: MarshalAs(UnmanagedType.Interface)]

object getClientRects();

[DispId(-2147417067 /*0x80010415*/)]

[return: MarshalAs(UnmanagedType.Interface)]

object getBoundingClientRect();

[DispId(-2147417608 /*0x800101F8*/)]

void setExpression(string propname, string expression, string language);

[DispId(-2147417607 /*0x800101F9*/)]

[return: MarshalAs(UnmanagedType.Struct)]

object getExpression(string propname);

[DispId(-2147417606 /*0x800101FA*/)]

bool removeExpression(string propname);

[DispId(-2147418097 /*0x8001000F*/)]

short tabIndex

{

set; get; }

[DispId(-2147416112 /*0x800107D0*/)]

void focus();

[DispId(-2147416107 /*0x800107D5*/)]

string accessKey

{

set; [return: MarshalAs(UnmanagedType.BStr)] get; }

[DispId(-2147412097 /*0x8001177F*/)]

object onblur

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412098 /*0x8001177E*/)]

object onfocus

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412076 /*0x80011794*/)]

object onresize

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147416110 /*0x800107D2*/)]

void blur();

[DispId(-2147416095 /*0x800107E1*/)]

void addFilter(object pUnk);

[DispId(-2147416094 /*0x800107E2*/)]

void removeFilter(object pUnk);

[DispId(-2147416093 /*0x800107E3*/)]

int clientHeight

{

get; }

[DispId(-2147416092 /*0x800107E4*/)]

int clientWidth

{

get; }

[DispId(-2147416091 /*0x800107E5*/)]

int clientTop

{

get; }

[DispId(-2147416090 /*0x800107E6*/)]

int clientLeft

{

get; }

[DispId(-2147417605 /*0x800101FB*/)]

bool attachEvent(string _event, object pdisp);

[DispId(-2147417604 /*0x800101FC*/)]

void detachEvent(string _event, object pdisp);

[DispId(-2147412996 /*0x800113FC*/)]

object readyState

{

[return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412087 /*0x80011789*/)]

object onreadystatechange

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412050 /*0x800117AE*/)]

object onrowsdelete

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412049 /*0x800117AF*/)]

object onrowsinserted

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412048 /*0x800117B0*/)]

object oncellchange

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412995 /*0x800113FD*/)]

string dir

{

set; [return: MarshalAs(UnmanagedType.BStr)] get; }

[DispId(-2147417056 /*0x80010420*/)]

[return: MarshalAs(UnmanagedType.IDispatch)]

object createControlRange();

[DispId(-2147417055 /*0x80010421*/)]

int scrollHeight

{

get; }

[DispId(-2147417054 /*0x80010422*/)]

int scrollWidth

{

get; }

[DispId(-2147417053 /*0x80010423*/)]

int scrollTop

{

set; get; }

[DispId(-2147417052 /*0x80010424*/)]

int scrollLeft

{

set; get; }

[DispId(-2147417050 /*0x80010426*/)]

void clearAttributes();

[DispId(-2147417049 /*0x80010427*/)]

void mergeAttributes(IHTMLElement mergeThis);

[DispId(-2147412047 /*0x800117B1*/)]

object oncontextmenu

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417043 /*0x8001042D*/)]

[return: MarshalAs(UnmanagedType.Interface)]

IHTMLElement insertAdjacentElement(string where, IHTMLElement insertedElement);

[DispId(-2147417047 /*0x80010429*/)]

[return: MarshalAs(UnmanagedType.Interface)]

IHTMLElement applyElement(IHTMLElement apply, string where);

[DispId(-2147417042 /*0x8001042E*/)]

[return: MarshalAs(UnmanagedType.BStr)]

string getAdjacentText(string where);

[DispId(-2147417041 /*0x8001042F*/)]

[return: MarshalAs(UnmanagedType.BStr)]

string replaceAdjacentText(string where, string newText);

[DispId(-2147417040 /*0x80010430*/)]

bool canHaveChildren

{

get; }

[DispId(-2147417032 /*0x80010438*/)]

int addBehavior(string bstrUrl, ref object pvarFactory);

[DispId(-2147417031 /*0x80010439*/)]

bool removeBehavior(int cookie);

[DispId(-2147417048 /*0x80010428*/)]

object runtimeStyle

{

[return: MarshalAs(UnmanagedType.Interface)] get; }

[DispId(-2147417030 /*0x8001043A*/)]

object behaviorUrns

{

[return: MarshalAs(UnmanagedType.IDispatch)] get; }

[DispId(-2147417029 /*0x8001043B*/)]

string tagUrn

{

set; [return: MarshalAs(UnmanagedType.BStr)] get; }

[DispId(-2147412043 /*0x800117B5*/)]

object onbeforeeditfocus

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417028 /*0x8001043C*/)]

int readyStateValue

{

get; }

[DispId(-2147417027 /*0x8001043D*/)]

[return: MarshalAs(UnmanagedType.Interface)]

object getElementsByTagName(string v);

} // end of class mshtml.IHTMLElement2

[

TypeLibType(TypeLibTypeFlags.FDispatchable|TypeLibTypeFlags.FDual),

Guid(@"3050F673-98B5-11CF-BB82-00AA00BDCE0B")

]

public interface IHTMLElement3 : IHTMLElement2

{

// Methods

[DispId(-2147417016 /*0x80010448*/)]

void mergeAttributes(IHTMLElement mergeThis, ref object pvarFlags);

[DispId(-2147417015 /*0x80010449*/)]

bool isMultiLine

{

get; }

[DispId(-2147417014 /*0x8001044A*/)]

bool canHaveHTML

{

get; }

[DispId(-2147412039 /*0x800117B9*/)]

object onlayoutcomplete

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412038 /*0x800117BA*/)]

object onpage

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417012 /*0x8001044C*/)]

bool inflateBlock

{

set; get; }

[DispId(-2147412035 /*0x800117BD*/)]

object onbeforedeactivate

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417011 /*0x8001044D*/)]

void setActive();

[DispId(-2147412950 /*0x8001142A*/)]

string contentEditable

{

set; [return: MarshalAs(UnmanagedType.BStr)] get; }

[DispId(-2147417010 /*0x8001044E*/)]

bool isContentEditable

{

get; }

[DispId(-2147412949 /*0x8001142B*/)]

bool hideFocus

{

set; get; }

[DispId(-2147418036 /*0x8001004C*/)]

bool disabled

{

set; get; }

[DispId(-2147417007 /*0x80010451*/)]

bool isDisabled

{

get; }

[DispId(-2147412034 /*0x800117BE*/)]

object onmove

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412033 /*0x800117BF*/)]

object oncontrolselect

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417006 /*0x80010452*/)]

bool FireEvent(string bstrEventName, ref object pvarEventObject);

[DispId(-2147412029 /*0x800117C3*/)]

object onresizestart

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412028 /*0x800117C4*/)]

object onresizeend

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412031 /*0x800117C1*/)]

object onmovestart

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412030 /*0x800117C2*/)]

object onmoveend

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412027 /*0x800117C5*/)]

object onmouseenter

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412026 /*0x800117C6*/)]

object onmouseleave

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412025 /*0x800117C7*/)]

object onactivate

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147412024 /*0x800117C8*/)]

object ondeactivate

{

set; [return: MarshalAs(UnmanagedType.Struct)] get; }

[DispId(-2147417005 /*0x80010453*/)]

bool dragDrop();

[DispId(-2147417004 /*0x80010454*/)]

int glyphMode

{

get; }

} // end of class mshtml.IHTMLElement3

[

ComVisible(true),

Guid(@"3050F1FF-98B5-11CF-BB82-00AA00BDCE0B"),

InterfaceType(ComInterfaceType.InterfaceIsDual)

]

public interface IHTMLElement

{

void SetAttribute(string strAttributeName, object AttributeValue, int lFlags);

void GetAttribute(string strAttributeName, int lFlags, object[] pvars);

[return: MarshalAs(UnmanagedType.Bool)]

bool RemoveAttribute(string strAttributeName, int lFlags);

void SetClassName(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetClassName();

void SetId(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetId();

[return: MarshalAs(UnmanagedType.BStr)]

string GetTagName();

[return: MarshalAs(UnmanagedType.Interface)]

IHTMLElement GetParentElement();

[return: MarshalAs(UnmanagedType.Interface)]

object GetStyle();

void SetOnhelp(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnhelp();

void SetOnclick(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnclick();

void SetOndblclick(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOndblclick();

void SetOnkeydown(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnkeydown();

void SetOnkeyup(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnkeyup();

void SetOnkeypress(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnkeypress();

void SetOnmouseout(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmouseout();

void SetOnmouseover(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmouseover();

void SetOnmousemove(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmousemove();

void SetOnmousedown(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmousedown();

void SetOnmouseup(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnmouseup();

[return: MarshalAs(UnmanagedType.Interface)]

object GetDocument();

void SetTitle(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetTitle();

void SetLanguage(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetLanguage();

void SetOnselectstart(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnselectstart();

void ScrollIntoView(object varargStart);

[return: MarshalAs(UnmanagedType.Bool)]

bool Contains(IHTMLElement pChild);

[return: MarshalAs(UnmanagedType.I4)]

int GetSourceIndex();

[return: MarshalAs(UnmanagedType.Struct)]

object GetRecordNumber();

void SetLang(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetLang();

[return: MarshalAs(UnmanagedType.I4)]

int GetOffsetLeft();

[return: MarshalAs(UnmanagedType.I4)]

int GetOffsetTop();

[return: MarshalAs(UnmanagedType.I4)]

int GetOffsetWidth();

[return: MarshalAs(UnmanagedType.I4)]

int GetOffsetHeight();

[return: MarshalAs(UnmanagedType.Interface)]

IHTMLElement GetOffsetParent();

void SetInnerHTML(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetInnerHTML();

void SetInnerText(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetInnerText();

void SetOuterHTML(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetOuterHTML();

void SetOuterText(string p);

[return: MarshalAs(UnmanagedType.BStr)]

string GetOuterText();

void InsertAdjacentHTML(string where, string html);

void InsertAdjacentText(string where, string text);

[return: MarshalAs(UnmanagedType.Interface)]

IHTMLElement GetParentTextEdit();

[return: MarshalAs(UnmanagedType.Bool)]

bool GetIsTextEdit();

void Click();

[return: MarshalAs(UnmanagedType.Interface)]

object GetFilters();

void SetOndragstart(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOndragstart();

[return: MarshalAs(UnmanagedType.BStr)]

string toString();

void SetOnbeforeupdate(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnbeforeupdate();

void SetOnafterupdate(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnafterupdate();

void SetOnerrorupdate(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnerrorupdate();

void SetOnrowexit(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnrowexit();

void SetOnrowenter(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnrowenter();

void SetOndatasetchanged(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOndatasetchanged();

void SetOndataavailable(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOndataavailable();

void SetOndatasetcomplete(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOndatasetcomplete();

void SetOnfilterchange(object p);

[return: MarshalAs(UnmanagedType.Struct)]

object GetOnfilterchange();

[return: MarshalAs(UnmanagedType.Interface)]

object GetChildren();

[return: MarshalAs(UnmanagedType.Interface)]

object GetAll();

}

29.3 How can I catch the BeforeNavigate2 event?

This is a known bug. See http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q311298&ID=KB;EN-US;

If you are using the second solution from our FAQ "How can I host a WebBrowser control in a Windows Form" you will not have this problem but if you use the automatically generated wrapper classes see the following solution:

John Cullen posted the following answer in the microsoft.public.dotnet.framework.interop newsgroup.

The problem of the BeforeNavigate2 event not firing in C# applications has been floating around various groups for several months. Microsoft have not yet fixed the problem, although it is documented in the knowledge base. Until they do provide a fix, I suggest the following workaround to the problem which uses the fact the the old Webbrowser_V1 BeforeNavigate event *can* be caught. Note, use of this interface is deprecated, however since there doesn't appear to be any chance of a fix any time soon...

...

// define the webbrowser object

private AxSHDocVw.AxWebBrowser axDocument;

// define an IE3 compatible webbrowser object.

private SHDocVw.WebBrowser_V1 axDocumentV1;

public Form1()

{

//...

object o = null;

axDocument.Navigate("about:blank", ref o, ref o, ref o, ref o);

object oOcx = axDocument.GetOcx();

try

{

axDocumentV1 = oOcx as WebBrowser_V1;

axDocumentV1.BeforeNavigate += new SHDocVw.DWebBrowserEvents_BeforeNavigateEventHandler(this.axDocumentV1_BeforeNavigate);

}

catch (Exception ex)

{

// ignore errors. If it doesn't work, there's not a lot to do!

Console.WriteLine("Add BeforeNavigate event handler failed with{0}.", ex.Message);

}

//...

}

private void axDocumentV1_BeforeNavigate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)

{

Console.WriteLine("BeforeNavigateURL= {0}", URL);

//false= allow navigate to continue.

//true= cancel navigation.

Processed=false;

}

29.4 WebBrowser control does not seem to be cleaning up temp files. How can this be done?

Look at the following articles to clear up Cache:

Visual Basic .NET version:

Q311289 - HOW TO: Clear the Cache When Your Application Hosts WebBrowser http://support.microsoft.com/support/kb/articles/q311/2/89.asp.

C# Version:

Q326201 - HOW TO: Clear the Cache When Your Application Hosts WebBrowser http://support.microsoft.com/support/kb/articles/q326/2/01.asp.

Posted by Bharat Patel (MS) on microsoft.public.dotnet.framework.windowsforms.

29.5 How can I use MSHTML to edit HTML in a Windows Forms control?

Take a look at Tim Anderson's HTMLEditor control which is a wrapper for MSHTML.




© 2001-06 Copyright George Shepherd.

30. Windows Forms ProgressBars FAQ Home

30.1 How can I implement a smooth ProgressBar?



30.1 How can I implement a smooth ProgressBar?

Refer to the following KnowledgeBase articles in MSDN:
HOW TO: Create a Smooth Progress Bar in Visual C# .NET
HOW TO: Create a Smooth ProgressBar in Visual Basic .NET




© 2001-06 Copyright George Shepherd.

31. Windows Forms PictureBox FAQ Home

31.1 How can I place a border around a PictureBox?

31.2 How can I copy a bitmap from the clipboard to a PictureBox?

31.3 How can I copy and paste images/graphs etc from MS Office to a PictureBox?

31.4 How can I drag and drop an image from one PictureBox to another?



31.1 How can I place a border around a PictureBox?

One solution is to use a panel that has a picturebox placed on it with DockStyle.Fill. This will make the picturebox assume the size of the panel. In addition, set the DockPadding.All property to the width of the desired border. Then in the Panel's OnPaint method, call the baseclass and then paint the desired borders.

Here are both VB and C# projects that illustrate how you might go about this. The derived PicturePanel class has properties that allow you to set the bordersize and color as well as the image that is to be displayed. This sample retrieves the image from an embedded resource. It also uses double bufferring to minimize flashing as you resize the control.

31.2 How can I copy a bitmap from the clipboard to a PictureBox?

This code snippet shows how you can set your PictureBox's image to be the image from the clipboard:

[C#]

this.pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);

[VB.Net]

Me.pictureBox1.Image = CType(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)

31.3 How can I copy and paste images/graphs etc from MS Office to a PictureBox?

Since .NET uses it's own format that is not compatible with the EnhancedMetafile format you will have to use reflection to achieve this. (From a posting in the microsoft.public.dotnet.framework.drawing newsgroup)

[C#]

using System.Runtime.InteropServices;

using System.Reflection;

public const uint CF_METAFILEPICT = 3;

public const uint CF_ENHMETAFILE = 14;

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]

public static extern bool OpenClipboard(IntPtr hWndNewOwner);

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]

public static extern bool CloseClipboard();

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]

public static extern IntPtr GetClipboardData(uint format);

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]

public static extern bool IsClipboardFormatAvailable(uint format);

//Pasting into PictureBox

if (OpenClipboard(this.Handle))

{

if (IsClipboardFormatAvailable(CF_ENHMETAFILE))

{

IntPtr ptr = GetClipboardData(CF_ENHMETAFILE);

if (!ptr.Equals(new IntPtr(0)))

{

Metafile metafile = new Metafile(ptr,true);

//Set the Image Property of PictureBox

this.pictureBox1.Image = metafile;

}

}

CloseClipboard();

}

31.4 How can I drag and drop an image from one PictureBox to another?

The following code snippet demonstrates how you can drag and copy an image from one picturebox (Source) another (Target:

[C#]

//In the Form Load

//Set AllowDrop of the Target PictureBox to true as this property cannot be set in the Designer

this.pictureBox2.AllowDrop = true;

//Source PictureBox

private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

{

if (e.Button == MouseButtons.Left)

pictureBox1.DoDragDrop( pictureBox1.Image, DragDropEffects.All );

}

//Target PictureBox

//Drag Drop Effects

private void pictureBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)

{

if ( e.Data.GetDataPresent( DataFormats.Bitmap ) )

{

e.Effect = DragDropEffects.Copy;

}

else

e.Effect = DragDropEffects.None;

}

//Set the image to be the dragged image.

private void pictureBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)

{

if ( (e.Data.GetDataPresent(DataFormats.Bitmap)))

{

this.pictureBox1.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));

}

}

[VB.NET]

'In the Form Load

'Set AllowDrop of the Target PictureBox to true as this property cannot be set in the Designer

Me.pictureBox2.AllowDrop = True

'Source PictureBox

Private Sub pictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

If e.Button = MouseButtons.Left Then

pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All)

End If

End Sub

'Target PictureBox

'Drag Drop Effects

Private Sub pictureBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)

If e.Data.GetDataPresent(DataFormats.Bitmap) Then

e.Effect = DragDropEffects.Copy

Else

e.Effect = DragDropEffects.None

End If

End Sub

'Set the image to be the dragged image.

Private Sub pictureBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)

If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then

Me.pictureBox1.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)

End If

End Sub

No comments: