Embedded Windows User Controls into Internet Explorer [ printer friendly ]
Stats
Rating: 4.68 out of 5 by 56 users
Submitted: 01/30/02
Andrew Ma (ajmaonline@hotmail.com)
Web programming is great and the changes since the old days of plain Html have allowed web programmers to do amazing things with a web browser. Although, even with all the fancy JavaScript and DHTML we have, some things are difficult or impossible to do with a web browser. To get around that, Microsoft introduced ActiveX Controls which allowed Internet Explorer users to download controls which have a richer interface.
Enter the .NET Framework. Now what does .NET provide? The .NET Framework comes with something similar to ActiveX. It allows us to embed windows user controls into Internet Explorer in a similar way as ActiveX. Any user controls written for a windows desktop application can be loaded into any IE browser.
Let\’s get started.
The User Control
First we\’ll start by creating a Windows User Control. Here is a sample control you can cut and paste. I won\’t be explaining the details of a user control since it\’s out of the scope of this tutorial. This is a simple user control which contains a rich text box and two buttons to add bold and italics to selected text.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace devhood
&leftsign;
/// <summary>
/// Summary description for rtfcontrol.
/// </summary>
public class rtfcontrol : System.Windows.Forms.UserControl
&leftsign;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btnBold;
private System.Windows.Forms.Button btnItalic;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public rtfcontrol()
&leftsign;
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
&rightsign;
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
&leftsign;
if( disposing )
&leftsign;
if( components != null )
components.Dispose();
&rightsign;
base.Dispose( disposing );
&rightsign;
#region Component Designer generated code
/// <summary>
/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
&leftsign;
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.btnItalic = new System.Windows.Forms.Button();
this.btnBold = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.BackColor = System.Drawing.Color.White;
this.richTextBox1.Location = new System.Drawing.Point(0, 40);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(488, 432);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.LightGray;
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] &leftsign;
this.btnItalic,
this.btnBold&rightsign;);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(488, 40);
this.panel1.TabIndex = 1;
//
// btnItalic
//
this.btnItalic.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnItalic.Location = new System.Drawing.Point(72, 8);
this.btnItalic.Name = "btnItalic";
this.btnItalic.Size = new System.Drawing.Size(56, 24);
this.btnItalic.TabIndex = 1;
this.btnItalic.Text = "Italic";
this.btnItalic.Click += new System.EventHandler(this.btnItalic_Click);
//
// btnBold
//
this.btnBold.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnBold.Location = new System.Drawing.Point(8, 8);
this.btnBold.Name = "btnBold";
this.btnBold.Size = new System.Drawing.Size(56, 24);
this.btnBold.TabIndex = 0;
this.btnBold.Text = "Bold";
this.btnBold.Click += new System.EventHandler(this.btnBold_Click);
//
// rtfcontrol
//
this.BackColor = System.Drawing.Color.LightGray;
this.Controls.AddRange(new System.Windows.Forms.Control[] &leftsign;
this.panel1,
this.richTextBox1&rightsign;);
this.Name = "rtfcontrol";
this.Size = new System.Drawing.Size(488, 472);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
&rightsign;
#endregion
private void btnBold_Click(object sender, System.EventArgs e)
&leftsign;
if (this.richTextBox1.SelectionFont.Bold)
this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, FontStyle.Regular);
else
this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, FontStyle.Bold);
&rightsign;
private void btnItalic_Click(object sender, System.EventArgs e)
&leftsign;
if (this.richTextBox1.SelectionFont.Italic)
this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, FontStyle.Regular);
else
this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, FontStyle.Italic);
&rightsign;
&rightsign;
&rightsign;
One major thing to note about this user control is that it does not use any system colours. The colour of the form, panel and the textbox are all defined in this control. This is an important thing to note because if anything is set to system colours, the control will not load in the browser.
Now that we have the User Control created, we have to compile it to a DLL. Use this syntax to create a DLL:
csc /t:library rtfcontrol.cs
After compilation is done, place the DLL in a location on your web server. Note, do not place the DLL in the /bin directory since this DLL will be downloaded by the IE client. Most /bin directories are set up with permissions that do not allow files to be downloaded from the /bin directory.
The HTML page
Next we have to create the HTML page that the users will visit. Simple enough, all we have to add is an <object> tag at the location where we want the user control to appear. Here is a sample HTML page:
<html>
<body>
<center>
<object id="rtfcontrol" height="472" width="488"
classid="http://127.0.0.1/RtfControl.dll#devhood.rtfcontrol"> //直接 RtfControl.dll#devhood.rtfcontrol
</object>
</center>
</body>
</html>
Notice the classid in the <object> tag. The classid has two parts to it. First we have the URL to the DLL file (again, make sure the DLL is not in the /bin directory). It is then followed by a # sign and ends with the fully qualified name of the control (namespace + control name).
There you have it. When you load this HTML page in a IE web browser, the control will load and you\’re ready to go.
历史博文
- 20090812 KIS2010 WIN2003 - 2009
- 20090813 Wordpress cms BBtheme 修改 - 2009
- 20090812 xml rpc comodo - 2009
- 20090807 Godaddy gegehost - 2009
- 20090807 双飞燕 G7630 电池 - 2009
- 20090807 c# WebBrowser NewWindow2 - 2009
- 20090807 c# Parent Process - 2009
- 20090803 長沙 益阳 印象 - 2009
- 20090803 益阳 丧俗 - 2009
- 20090803 母系 亲戚 外婆 - 2009