分享
 
 
 

Extended web browser control for .NET 1.0/1.1

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Project source, example, HTML help documentation - 348 Kb

Download control source only (includes help .chm file) - 159 Kb

Quickstart

Pretty simple: download the project, run it. See 'Adding the referenced ActiveX controls' if it doesn't compile from reference issues.

Introduction

The next version of the .NET framework features a comprehensive wrapper class for the Microsoft Web Browser control - a COM control that is used by IE and is embedded into Explorer. This new control features everything people are at pains to produce using COM without fishing through newsgroups and tech articles.

Well, I thought it was about time there was an equivalent control for .NET 1.0 / .NET 1.1, so I've put my open-source apron on once more, to produce this control.

This control contains the following features that you do not get (without doing it yourself) with the standard AxWebBrowser control:

Context menu

Optionally opening all links in a new window

Show the find dialog

Show the print dialog

Show the save-as dialog

Show the print preview dialog

Disabling accelerator keys such as CTRL+N, CTRL+F and Backspace

Restrict images, Java, ActiveX

Stop images, sounds, videos from displaying

Disable JavaScript

Disable downloading/running of ActiveX and Java controls

Ability to turn a 3D border on or off

Turn the scrollbars on or off

Make all form controls XP themed

Simplified and more feature-rich navigation methods (Navigate with more options, Refresh with different refresh options.)

Documentation for the events.

In some ways, it's actually more feature-rich than the .NET 2.0 control, although the .NET 2.0 control features a security model implementation and a few other properties that will hopefully be put into WebBrowserEx during its lifetime.

How is it implemented?

I'm not going to bother going into detail about how it's been implemented - there is a wealth of information out there on the net that explains this already. See the References section for links to the places I got the information from. The control is a user control with the AxWebBrowser docked to full on it. I tried to derive from AxWebBrowser, but it didn't like me adding properties in, so I gave up on that method. This meant that in the control, lots of properties and events have had to be re-implemented. This was pretty boring to do, but has actually made for a more concise control than having a derived class with all its ActiveX properties drooping out.

IDocHostUIHandler and context menus

The control implements IDocHostUIHandler (see the References for what this is) which handles the context menu and key restrictions. Unlike the .NET 2.0 control, I've left the control with just a ContextMenu property. You can set this to a blank ContextMenu, e.g.:

myBrowser.ContextMenu = new ContextMenu();

if you don't want the default IE context menu. In the version 2 control, there is an additional option to not show the the IE context menu. If there's enough demand for this, I'll add it in.

The legend of the IDispatch_Invoke_Handler

The control also implements IOleClientSite. As before, you can read what this is in the References. The most obscure and important part of the control is the IDispatch_Invoke_Handler(). I spent quite a while trying to work out how to implement IDispatch::Invoke in order to restrict what IE was showing (such as images, ActiveX controls, Java). I found out that if you add a IDispatch_Invoke_Handler() method in your code with the COM dispatch identifier of -5512, this does the job for you. A very obscure answer, but it works well. The options enumeration then works with this to set the options you want available in your browser.

I don't believe the .NET 2.0 control has these options available; hopefully someone at MS who has ties to .NET will read this and request that it's added in, as they're quite core features for the control.

WebExecWB

The control features an easier to use ExecWB method which queries the interface before executing it, fixing the "Trying to revoke a drop target that has not been registered." and similar errors. The print, save-as etc. methods, use this wrapped call. You'll find, some of the CMDID constants do nothing. Welcome to the world of COM interop - I found out whilst making this control that some of the constants Microsoft has on the MSDN site haven't worked for years, and they just haven't bothered updating their documentation. Some of these constants may be useable by casting the CurrentDocument to an IOleCommandTarget, and calling its Exec method (see the Find method). This is basically all that the ExecWB method does anyway, as far as I'm aware.

Flags designer

The control implements Thierry Bouquain's designer flag editor for setting the options flags in the designer. It currently has all of its COM imports, interfaces and the flag designer editor in the single file. This makes the source a bit bulkier, but nothing that regions don't overcome. It also makes the control more self-contained in my view.

XP Service Pack 2

In XP Service pack 2, Microsoft has updated the MSHTML part of the browser so that you get more options in your NewWindow event (it's called NewWindowEvent3). I added this originally, but then realized that it would break on most people's machines due to dependency failures. You can add it quite easily to the control, if you want to know how your new window was created.

Adding the referenced ActiveX controls[/url]

When you download the project, you might find the following references missing:

AxInterop.SHDocVw.dll

Interop.SHDocVw.dll

Microsoft.mshtml.dll

I haven't included these as they are operating system dependent. I'm running XP SP2, whilst you might be running Windows 2000, XP SP1 etc., so my DLLs will break with your operating system, if I included them.

To add Microsoft.mshtml.dll is straight forward; click on the Add Reference in the project, and add the following:

Adding SHDocVw is slightly strange. As far as I know, you can't add it as reference, so you have to add it into your toolbox and add it to the control's form, then remove it (something to do with the AxImp tool and VS.NET, I'm not sure what). To do this, open up the WebBrowserEx control so it's in designer mode, and then go to your toolbox, click on Add/Remove items in the context menu, and select the following from the COM tab:

Once that's added to your toolbox, add a temporary form to the WebBrowserEx project, drop it onto the form, and then remove it. This should add it to your references. Now, do a voodoo dance and compile, and hopefully all should be well.

I haven't tried compiling without VS.NET, adding the references should be straight forward enough though, using the AxImp tool with SHDocVw.dll.

DOM event handlingI've added support into the control for the MSHTML document events. The events allow you to capture keydown, mouseclick etc. events in the document - events which are not available to the browser control itself as it is just a host for the document parser. The implementation I've added is fairly experimental, and can be switched on using EnableHtmlDocumentHandling property. Setting this to true will mean all HtmlDocument* events are fired, however I should stress that these events tend to swallow up your key and mouse events and seem to make the form key presses and link clicking go screwy. I didn't have time to work out why this was, I'll probably have a look at it on a rainy Sunday.

And finally

The method and property names in the control don't match those of the .NET 2.0 control. Some are the same, some aren't. I can't really see a scenario where you'd upgrade from this to the new .NET 2.0 control, but if this is a big problem with people, I can change it so that all the property names and methods are the same.

One small touch I've added to the control is to include the documentation from MSDN for all of the events that come from AxWebBrowser.

That's all. I hope the control comes in useful.

References

[url=http://www.itwriting.com/htmleditor/]HTML editor using IE, where I got a lot of the COM declarations.

Sample chapter about customizing the browser.

Flags designer UI editor.

Work around for ExecWB inadequacies.

An OLE interface.

Another OLE interface.

Browser overview.

IDispatch_Invoke_Handler magic.

Bugs with OnBeforeNavigate2 (plus others) in .NET 1.0.

WebBrowser Customization (for a C++ audience).

Skinnable tabbed browser with advert blocking, using the IE engine.

About smallguy78

C# (winforms + ASP.NET), SQL Server programmer. I also maintain (very ocassionally) my anorak website, www.sloppycode.net, when i'm not djing,making house/techno, hl2 mapping or drinking in Bristol.

Click here to view smallguy78's online profile.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有