下面一段文字来自C# Expert,
I am wondering how to build a C# applet. I built a class library with a custom control. But the browser (IE6) fails to render it as an object. Do you have some examples to show how to achieve this? By the way, can a C# applet be run under platforms without the .NET Framework pre-installed?
There's no such concept as a C# "applet". At most, there are custom Windows forms controls that are embedded on a page. Of course, just like in Java, you can't run it if you don't have the corresponding runtime on the client machine. You can use the <object> tag to add it to an HTML page: (zzj0820注:其实并没有所谓的C# Applet的概念,通常所说的C# Applet指的是嵌入到浏览器里的窗体控件。当然,跟java类似,必须在客户端机器上安装相应的运行时库才能使用嵌入在html页面里的object标签控件)
<object id="MyControl" classid="http://mywebsite/MyClassLibrary.dll#FullNamespace.MyControlClass" height="480 "width="640"></object>
The class ID contains the URL of the DLL for downloading and the fully qualified name of the class that implements the control. The library must not be placed on the /bin folder of a Web application in order to be served by IIS/ASP.NET
下面有两个简单的例子,希望有所帮助J
Eg1: 原文链接
/*
* A simple C# program that displays some text in a webpage.
* Compile with: "csc /t:library hello-world-applet.cs"
* Run byte deploying in a webpage with:
* <object classid="http:hello-world-applet.dll#HelloWorldApplet" width="200" height="100"></object>
* Ben Bederson, January 16, 2002
*/
using System;
using System.Drawing;
using System.Windows.Forms;
public class HelloWorldApplet : System.Windows.Forms.Control {
public HelloWorldApplet() {
// Create a "label" control
Label label = new Label();
label.Text = "Hello world!";
label.ForeColor = Color.Blue;
label.Font = new Font("Arial", 24, FontStyle.Bold);
label.AutoSize = true;
// Insert the label
Controls.Add(label);
}
}
Eg2: 原文链接
C# Applet
By Lloyd Dupont
I want to tell you how to write C# Applet, display it in a web page and the requirement.
The requirement first, your C# Applet won't work anywhere nor from anywhere. You should put (and test) it on IIS, for unknown reason (at last unknown to me) this won't work locally or with apache. BTW http://www.brinkster.com make free ".NET" hosting.
Not any client could display a C# Applet, you surely need IE6 and probably .NET SDK (at last with these configuration this work everywhere i know).
After you wrote your custom System.Windows.Forms.Control, create it with a parameterless constructor (which will be called by IE) and wrap it in an assembly.
After you could display it very easily in a web page like this:
<object
id=anID
classid="http:myAssemblyDll.dll#controlClassToInstantiate"
height=300 width=300>
</object>
and with id you could call it public method vis javascript like this
<script> <!--
myID.AProperty = aValue;
myID.Refresh()
//--></script>
Here is an example you could see at http://www24.brinkster.com/superlloyd/un.htm
-- un.html --
<html>
<head>
<title>C# Web control I</title>
</head>
<body>
and now...<br>
<object id=t
classid="http:Un.DLL#WT.T"
height=300 width=300 VIEWASTEXT>
</object>
<br>
<a href=un.cs>source</a>
</body>
</html>
-- un.cs --
using System;
using System.Drawing;
using System.Windows.Forms;
// csc un.cs
// csc /t:library /out:Un.DLL un.cs
namespace WT
{
public class T : Control
{
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Azure), ClientRectangle);
e.Graphics.DrawLine(Pens.DarkSalmon,
new Point(0, 0),
new Point(ClientRectangle.Width, ClientRectangle.Height));
}
public static void Main(string[] m)
{
Form f = new Form();
T t = new T();
t.Dock = DockStyle.Fill;
f.Controls.Add(t);
Application.Run(f);
}
}