Using Windows Forms Controls in Internet Explorer

翻译 2007年09月18日 17:14:00

                        <td width="23%" class="contentInput">&nbsp;&nbsp;一卡通卡号 :</td>
                        <td><OBJECT ID="CommonCardNo" height=24 width=140 CLASSID="CLSID:0CA54D3F-CEAE-48AF-9A2B-31909CB9515D" codebase=',2,0,2'></OBJECT>(12位或16位数字)</td>
           <td class="contentInput">&nbsp;&nbsp;支付密码 :</td>
           <td><OBJECT ID="CommonCardPwd" height=24 width=140 language=vbs onload='CommonCardPwd.PasswdCtrl=true' CLASSID="CLSID:0CA54D3F-CEAE-48AF-9A2B-31909CB9515D" codebase=',2,0,2'></OBJECT></td>

其中OBJECT ID="CommonCardPwd这样的就用到了下面翻译的知识。当然也可用到其他安全性要求较高的领域,但这篇译作只是个简单的开始,在这特感谢在美国的佛吉利亚理工大学 wj zhang的审校。

Using Windows Forms Controls in Internet Explorer

(在浏览器中使用Windows Forms Controls

This topic describes how to successfully execute Windows Forms controls within Internet Explorer (IE). Windows Forms controls within IE are activated without a user prompt, require no registration, and utilize the common language runtime (CLR) code access security.

There are five steps in getting a Windows Forms control activated within Internet Explorer, and each is listed here and detailed below.

这篇文章的主题就是怎样在IE中成功的执行Windows Forms Controls。在IE, Windows Forms Controls的激活是不需要向用户提示的,也不需要注册, 使用CLR安全入口代码。

  • Create the Windows Forms control.

  • Create an HTML document with an object tag.

  • Create the virtual directory and set permissions.

  • Run the control.

Create the Windows Forms Control

Almost any Windows Forms control can be hosted in Internet Explorer, but for this example we will host the SimpleControl that is included in the Creating Controls section of this QuickStart tutorial. The control must be installed to the global assembly cache or be present in the same virtual directory as the web page that contains it

几乎所有的Windows Forms control能被IE所识别,例如,我们识别SimpleControl, 它被包括在快速启动指南的Creating Controls部分。这个控件必须安装到global assembly cache或出现在作为网页包含相同的虚拟目录下。


VB SimpleControl

[Run Sample] | [View Source]


Create an HTML document with an object Tag

The next step is to create an HTML document with an object tag that refers to the Windows Forms control. For this sample, some simple script and input tags will also be added to demonstrate programmatic access to the control.

接下来的一步就是使用一个与Windows Forms control 相关的object标签来创建一个HTML文档。例如,一些简单脚本和输入标签也将被加入来证明对控件的可编程接入。



The classid has two interesting parts: the path to the control library, and the fully qualified name of the control, separated by the pound sign. If you are familiar with an ActiveX object tag, you will notice the lack of a guid. In the case of Windows Forms, the combination of the path and fully qualified class name serve as the unique identifier.

Classid有两个有趣的部分:一部分是到控件库的路径,还有一部分就是完全修饰的控件名,这两部分被“#”隔开。如果你熟悉ActiveX object标签的话,你将注意它缺少一个guid。在Windows Forms实例中,联合的路径和完全修饰的类名作为唯一的鉴别者。

Param tags may be used to set properties on controls. In this case, the name attribute is the name of the property and the value attribute is the value of the property.







To gain programmatic access to your control, you can write script against it. A button and text box on the page is used in conjunction with the simple JScript function ChangeText to set the text property of the control. The following is the complete HTML and script code for this example.

想获得你定义控件的可编程接口,你要为他写一段脚本,一个按钮和一个文本框被联合用来和一个JScript ChangeText的函数来来设置控件的文本特性。下面这个例子是完整的HTMLJS脚本代码











Create the Virtual Directory and Set Permissions

The HTML page must reside in an IIS virtual directory on your web server, and have appropriate permissions. In this example, the Windows Forms control resides in the same directory, but it can also be installed in the global assembly cache. Execution permissions on the virtual directory must be set to scripts -- the control will not be properly activated if the execution permissions are set to scripts & executables. For this sample, these steps have been performed for you.

HTML 页必须位于你的web server IIS的虚拟目录下,并且你有适当的权限访问。例如,Windows Forms control位于相同的目录下,但也能被装载在global assembly cache中。虚拟目录下执行权限必须被设为Scripts,如果执行权限被为scripts & executables控件将不能被正确激活

Run the Control

To run the control, just point Internet Explorer to the HTML page in your virtual directory. If the control is not activating correctly, it may be necessary to restart Internet Explorer. To view and run this sample, click the icon below.



VB IE Sourcing

[Run Sample] | [View Source]






'/ <copyright from='1997' to='2001' company='Microsoft Corporation'>

'/    Copyright (c) Microsoft Corporation. All Rights Reserved.


'/    This source code is intended only as a supplement to Microsoft

'/    Development Tools and/or on-line documentation.  See these other

'/    materials for detailed information regarding Microsoft code samples.


'/ </copyright>


Imports System

Imports System.ComponentModel

Imports System.Windows.Forms

Imports System.Drawing


Namespace Microsoft.Samples.WinForms.VB.SimpleControl


    <DefaultProperty("DrawingMode"), DefaultEvent("DrawingModeChanged")> _

        Public Class SimpleControl


        Inherits System.Windows.Forms.Control


        Private Sub InitializeComponent()


        End Sub


        Private myDrawingMode As DrawingModeStyle

        Private myOnDrawingModeChanged As EventHandler


        '*** Constructors


        Public Sub New()



            'Initialise drawingMode

            myDrawingMode = DrawingModeStyle.Happy


            'Initialise BackColor and ForeColor based on DrawingMode



            'Make sure the control repaints as it is resized

            SetStyle(ControlStyles.ResizeRedraw, True)


        End Sub



        '*** Properties


        'Remove the BackColor property from the properties window

        <Browsable(False)> Public Overrides Property BackColor() As Color


                Return MyBase.BackColor

            End Get


            Set(ByVal Value As Color)

                'No Action

            End Set


        End Property



        'DrawingMode - controls how the control paints

        <Category("Appearance"), _

         Description("Controls how the control paints"), _

         DefaultValue(DrawingModeStyle.Happy), _

         Bindable(True)> _

        Public Property _

        DrawingMode() As DrawingModeStyle



                Return myDrawingMode

            End Get


            Set(ByVal Value As DrawingModeStyle)

                myDrawingMode = Value


                'Set BackColor and ForeColor based on DrawingMode



                'Raise property changed event for DrawingMode



            End Set


        End Property



        'Remove the ForeColor property from the properties window

        <Browsable(False)> Public Overrides Property ForeColor() As Color


                Return MyBase.ForeColor

            End Get


            Set(ByVal Value As Color)

                'No Action

            End Set


        End Property




        '*** Events


        'Handle the paint event

        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)


            e.Graphics.FillRectangle(New SolidBrush(BackColor), ClientRectangle)


            Dim textSize As SizeF = e.Graphics.MeasureString(Me.Text, Font)


            Dim xPos As Single = CSng((ClientRectangle.Width / 2) - (textSize.Width / 2))

            Dim yPos As Single = CSng((ClientRectangle.Height / 2) - (textSize.Height / 2))


            e.Graphics.DrawString(Me.Text, Font, New SolidBrush(ForeColor), xPos, yPos)


        End Sub


        Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)



        End Sub



        'Catch property changed event for DrawingMode to fire DrawingMoe changed

        'and repaint the control

        Protected Overridable Sub OnDrawingModeChanged(ByVal E As EventArgs)

            'Set BackColor and ForeColor based on DrawingMode



            If Not (myOnDrawingModeChanged Is Nothing) Then myOnDrawingModeChanged.Invoke(Me, E)

        End Sub


        'DrawingModeChanged Event

            <Description("Raised when the DrawingMode changes")> _

            Public Event _

            DrawingModeChanged(ByVal sender As Object, ByVal ev As EventArgs) 'As EventHandler


        'Set the ForeColor and BackColor based on the value of DrawingMode

        Private Sub SetColors()


            Select Case myDrawingMode


                Case DrawingModeStyle.Happy

                    MyBase.BackColor = Color.Yellow

                    MyBase.ForeColor = Color.Green


                Case DrawingModeStyle.Sad

                    MyBase.BackColor = Color.LightSlateGray

                    MyBase.ForeColor = Color.White


                Case DrawingModeStyle.Angry

                    MyBase.BackColor = Color.Red

                    MyBase.ForeColor = Color.Teal


                Case Else

                    MyBase.BackColor = Color.Black

                    MyBase.ForeColor = Color.White


            End Select


        End Sub


    End Class


End Namespace


如何关闭Internet Explorer 增强的安全配置

什么是Internet Explorer 增强的安全配置?简单的说就是在你访问一个未标记为信任的网站的时候给你弹出一个提示,如下图:如果你认为这个网站是可信任的,就点击“添加”按钮,把它添加到信任区,...
  • testcs_dn
  • testcs_dn
  • 2015年10月26日 17:30
  • 18575

Internet Explorer 已限制此网页运行脚本或者ActiveX控件

To help protect your security, Internet explorer has restricted this web page from running scripts o...
  • kangkanglou
  • kangkanglou
  • 2016年12月13日 19:30
  • 1799

Windows 8 下Internet Explorer 10(IE10含Metro) 无法打开(闪退)或需要管理员权限打开

  • ituff
  • ituff
  • 2013年02月28日 20:09
  • 12853

Internet Explorer 已不再尝试还原此网站。该网站看上去仍有问题。

近日浏览163新闻网时,发现打开一个链接后很快就会提示“Internet Explorer 已不再尝试还原此网站。该网站看上去仍有问题。”,如下图: 这个有点奇怪,打开其它网站比如csdn就没有问题...
  • jszj
  • jszj
  • 2017年07月01日 09:10
  • 1070

为了有利于保护安全性,Internet Explorer 已限制此网页运行可以访问计算机的脚本或 ActiveX控件。请单击这里获取选项

在dreamweaver的“代码”顶端你会发现并找到" lang="en"> 这个后,在dtd">...
  • ken2002
  • ken2002
  • 2015年01月24日 20:25
  • 1884

Internet Explorer无法打开Internet 站点的原因

一、网络设置的问题  这种原因比较多出现在需要手动指定IP、网关、DNS服务器联网方式下,及使用代理服务器上网的。仔细检查计算机的网络设置。 二、DNS服务器的问题  当IE无法浏览网页...
  • xwnxwn
  • xwnxwn
  • 2013年04月20日 15:48
  • 530

Internet Explorer 升级到IE11遇到问题案例分析

使用升级到的Internet Explorer 11访问应用界面,在F12开发人员工具中跟踪到如下错误:“SCRIPT1629:提供的数据类型不对”。...
  • xiaoyw
  • xiaoyw
  • 2016年02月27日 21:27
  • 2896


最近部门的Web服务器突然无法访问! 加班解决! 问题症状: 1、“Internet Explorer 无法显示该网页" 2、网站各项配置正常,昨天还能好好访问的 问题究竟在哪里呢? 通过...
  • foxeatapple
  • foxeatapple
  • 2014年03月24日 20:14
  • 4906

定制Internet Explorer下载管理器

定制Internet Explorer下载管理器
  • hellochenlian
  • hellochenlian
  • 2014年12月25日 16:56
  • 1398


转载自: QC问题汇总(转)   (2012-07-11 10:19:...
  • ysysbaobei
  • ysysbaobei
  • 2013年11月26日 19:47
  • 5552
您举报文章:Using Windows Forms Controls in Internet Explorer