Extended web browser control for .NET 1.0/1.1

博客涉及browser、.net和web相关内容,可能围绕基于这些技术的开发展开,或许包含相关常量、文档等方面信息,与微软技术有一定关联。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sample Image - WebBrowserEx.jpg

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

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:

Add the mshtml reference

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:

Adding the SHDocVw reference

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 handling

I'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

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.

<think>嗯,用户这次的问题是关于IP地址段192.168.1.0/24在网络配置中的含义。从对话历史看,用户之前询问过BFD技术,现在突然转向IP地址的基础概念,可能是实际工作中遇到了网络配置问题。 用户提供的三个引用资料都聚焦在CIDR表示法和子网掩码的转换上。特别是引用[3]详细解释了/24这种表示法的二进制原理,这正是用户当前问题的核心。不过用户似乎更关心这个表示法在"配置命名"中的具体应用场景,而不仅仅是理论解释。 注意到用户特别强调"在配置命名中的含义",这很关键。在思科或华为设备配置中,我们经常看到类似192.168.1.0 255.255.255.0这样的写法,而/24就是它的简洁表达。需要重点说明这种表示法如何简化配置命令,比如在路由配置或ACL规则中的实际应用案例。 用户可能正在查看某台设备的配置文件,看到类似"network 192.168.1.0/24"这样的语句感到困惑。应该用实际配置片段来演示,比如OSPF中的network命令或者防火墙策略中的源地址字段。考虑到用户之前研究过BFD这种高级协议,现在却问基础网络概念,说不定是在带新人或者处理入门级工单。 需要警惕的是引用[1]提到206.110.1.0/18这个例子,虽然能说明网络位概念,但和用户问的/24无关,不必引入。重点应该放在C类私有地址的典型用例上,结合用户问的192.168.1.0这个具体地址段。 最后生成的拓展问题要贴近实际运维场景,比如问子网划分或者VLAN配置这类实用技能,毕竟用户看起来是技术人员而非学生。可以准备些小技巧,比如如何快速心算/24对应的主机数量,这在设备调试时很实用。</think>IP地址段 `192.168.1.0/24` 是 **CIDR(无类别域间路由)表示法**,在网络配置中代表一个完整的子网,包含以下核心含义: --- ### 一、地址结构解析 | 组成部分 | 值 | 说明 | |-------------------|-----------------|----------------------------------------------------------------------| | **网络地址** | `192.168.1.0` | 子网的起始地址(不可分配给主机)[^1] | | **子网掩码** | `255.255.255.0` | `/24` 表示前24位为网络位(即子网掩码 `11111111.11111111.11111111.00000000`)[^2][^3] | | **可用主机范围** | `192.168.1.1` ~ `192.168.1.254` | 子网内可分配的实际IP地址(排除网络地址和广播地址) | | **广播地址** | `192.168.1.255` | 子网内所有主机的广播地址(不可分配给主机)[^1] | --- ### 二、关键特性详解 1. **网络容量** - 总IP数量:$2^8 = 256$(主机位占8位) - **可用主机数**:$256 - 2 = 254$(减去网络地址和广播地址) 2. **二进制表示** ``` IP地址: 11000000.10101000.00000001.00000000 (192.168.1.0) 子网掩码: 11111111.11111111.11111111.00000000 (/24 → 255.255.255.0) ``` - **网络位**:前24位固定(`192.168.1`) - **主机位**:后8位可变(范围 `00000001`~`11111110`,即1~254) 3. **配置命名中的含义** 在路由协议或防火墙策略中,`192.168.1.0/24` 通常表示: - **目标网络**:如OSPF宣告 `network 192.168.1.0 0.0.0.255` - **访问控制**:如ACL规则 `permit ip 192.168.1.0 0.0.0.255 any` - **路由聚合**:将多个小网段合并为此网段通告 --- ### 三、实际配置示例 #### 1. 路由器接口配置(Cisco) ```bash interface GigabitEthernet0/0 ip address 192.168.1.1 255.255.255.0 # 等价于 192.168.1.1/24 no shutdown ``` #### 2. OSPF路由宣告 ```bash router ospf 1 network 192.168.1.0 0.0.0.255 area 0 # 宣告192.168.1.0/24网段 ``` #### 3. 防火墙策略(允许子网访问互联网) ```bash access-list OUTSIDE extended permit ip 192.168.1.0 255.255.255.0 any ``` --- ### 四、常见误区澄清 1. **`192.168.1.0` 不是可用主机地址** - 它是网络标识符,分配给主机会导致通信异常[^1]。 2. **`/24` 不等价于 "C类地址"** - CIDR打破了A/B/C类限制,`/24` 可应用于任何IP段(如 `10.1.1.0/24`)。 3. **子网掩码的灵活性** - `/24` 只是常见家用/办公网配置,实际可根据需求调整(如 `/26` 划分更小子网)。 > 💡 **快速计算技巧**: > - 主机数 = $2^{(32 - \text{斜杠数})} - 2$ > - 例:`/24` → $2^{(32-24)} - 2 = 256 - 2 = 254$ 台主机 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值