Using Windows Forms Controls in Internet Explorer

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

 引:在许多银行的网页中,有许多这样的HTML
<tr>
                        <td width="23%" class="contentInput">&nbsp;&nbsp;一卡通卡号 :</td>
                        <td><OBJECT ID="CommonCardNo" height=24 width=140 CLASSID="CLSID:0CA54D3F-CEAE-48AF-9A2B-31909CB9515D" codebase='https://site.cmbchina.com/download/CMBEdit.cab#version=1,2,0,2'></OBJECT>(12位或16位数字)</td>
                       </tr>
                       <tr>
           <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='https://site.cmbchina.com/download/CMBEdit.cab#version=1,2,0,2'></OBJECT></td>
          </tr>

其中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文档。例如,一些简单脚本和输入标签也将被加入来证明对控件的可编程接入。

<object id="simpleControl1"
 
classid="http:SimpleControl.dll#Microsoft.Samples.WinForms.Cs.SimpleControl.SimpleControl"
height="300" width="300" VIEWASTEXT>
        <param name="Text" value="Simple Control">
</object>
 

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.

Param标签常用来设置控件的属性。在实例中,Name属性是Param的特征名;value属性是Param的特征值。

<script>
 
function ChangeText() {
        simpleControl1.Text = text1.value;
}
 
</script>
 
 
<input type="text" id="text1">
<input type="button" value="Change Text" onclick="ChangeText()">
 

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脚本代码

<html>
 
<script language="JScript">
 
function ChangeText() {
        simpleControl1.Text = text1.value;
}
 
</script>
 
<body>
 
<p>Simple Control
<br>
<br>
</body>
 
<object id="simpleControl1"
 
classid="http:SimpleControl.dll#Microsoft.Samples.WinForms.Cs.SimpleControl.SimpleControl"
height="300" width="300" VIEWASTEXT>
        <param name="Text" value="Simple Control">
</object>
 
<br>
<br>
 
<input type="text" id="text1">
<input type="button" value="Change Text" onclick="ChangeText()">
 
</html>
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.

运行这个控件,恰好在你虚拟目录的HTML页指向你的IE,如果不能正确激活,你必需要重启IE。观看和运行这个例子,请点击下面的图标。

 


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()

            MyBase.New()

 

            'Initialise drawingMode

            myDrawingMode = DrawingModeStyle.Happy

 

            'Initialise BackColor and ForeColor based on DrawingMode

            SetColors()

 

            '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

            Get

                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

 

            Get

                Return myDrawingMode

            End Get

 

            Set(ByVal Value As DrawingModeStyle)

                myDrawingMode = Value

 

                'Set BackColor and ForeColor based on DrawingMode

                SetColors()

 

                'Raise property changed event for DrawingMode

                OnDrawingModeChanged(EventArgs.Empty)

 

            End Set

 

        End Property

 

 

        'Remove the ForeColor property from the properties window

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

            Get

                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)

            MyBase.OnTextChanged(e)

            Invalidate()

        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

            SetColors()

            Invalidate()

            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

原文:http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsIeSourcing.aspx

相关文章推荐

How to: Host Controls in Windows Forms DataGridView Cells(介绍如何在datagridview中自定义列)

The DataGridView control provides several column types, enabling your users to enter and edit valu...

C# Internet Explorer DOM using ObjectFromLresult

Howdo, I've been working on this for a couple of days, I did a VB.NET example of this on a forum a f...

Display fullscreen website using javascript (Internet Explorer/Safari/Chrome/Firefox)//

Until now, this is a great information, there are many browser that have implemented fullscreen sup...

Measuring Element Dimension and Location with CSSOM in Internet Explorer 9

Measuring Element Dimension and Location with CSSOM in Internet Explorer 9This topic is designed to ...
  • xjinza
  • xjinza
  • 2011年07月22日 15:01
  • 309
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Using Windows Forms Controls in Internet Explorer
举报原因:
原因补充:

(最多只允许输入30个字)