关于“C# Applet”

下面一段文字来自 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

  1<object> tag to add it to an HTML page: (zzj0820  注:其实并没有所谓的  C# Applet  的概念,通常所说的  C# Applet  指的是嵌入到浏览器里的窗体控件。当然,跟  java  类似,必须在客户端机器上安装相应的运行时库才能使用嵌入在  html  页面里的  object  标签控件  ) 
  2
  3<object classid="http://mywebsite/MyClassLibrary.dll#FullNamespace.MyControlClass" height="480 " id="MyControl" width="640"></object>
  4
  5The 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 
  6
  7下面有两个简单的例子,希望有所帮助  J 
  8
  9Eg1:  原文链接 
 10
 11/* 
 12
 13* A simple C# program that displays some text in a webpage. 
 14
 15* Compile with: "csc /t:library hello-world-applet.cs" 
 16
 17* Run byte deploying in a webpage with: 
 18
 19* <object classid="http:hello-world-applet.dll#HelloWorldApplet" height="100" width="200"></object>
 20
 21* Ben Bederson, January 16, 2002 
 22
 23*/ 
 24
 25using System; 
 26
 27using System.Drawing; 
 28
 29using System.Windows.Forms; 
 30
 31public class HelloWorldApplet : System.Windows.Forms.Control { 
 32
 33public HelloWorldApplet() { 
 34
 35// Create a "label" control 
 36
 37Label label = new Label(); 
 38
 39label.Text = "Hello world!"; 
 40
 41label.ForeColor = Color.Blue; 
 42
 43label.Font = new Font("Arial", 24, FontStyle.Bold); 
 44
 45label.AutoSize = true; 
 46
 47// Insert the label 
 48
 49Controls.Add(label); 
 50
 51} 
 52
 53} 
 54
 55Eg2:  原文链接 
 56
 57C# Applet    
 58By  Lloyd Dupont 
 59
 60I want to tell you how to write C# Applet, display it in a web page and the requirement. 
 61
 62The 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. 
 63
 64Not 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). 
 65
 66After 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. 
 67
 68After you could display it very easily in a web page like this: 
 69
 70<object classid="http:myAssemblyDll.dll#controlClassToInstantiate" height="300" id="anID" width="300">
 71</object>
 72
 73and with id you could call it public method vis javascript like this 
 74
 75<script> <!-- 
 76
 77myID.AProperty = aValue; 
 78
 79myID.Refresh() 
 80
 81//--></script>
 82
 83Here is an example you could see at  http://www24.brinkster.com/superlloyd/un.htm 
 84
 85\-- un.html -- 
 86
 87<html>
 88<head>
 89<title>C# Web control I</title>
 90</head>
 91<body>
 92
 93and now...<br/>
 94<object classid="http:Un.DLL#WT.T" height="300" id="t" viewastext="" width="300">
 95</object>
 96<br/>
 97<a href="un.cs">source</a>
 98</body>
 99</html>
100
101\-- un.cs -- 
102
103using System; 
104
105using System.Drawing; 
106
107using System.Windows.Forms; 
108
109// csc un.cs 
110
111// csc /t:library /out:Un.DLL un.cs 
112
113namespace WT 
114
115{ 
116
117public class T : Control 
118
119{ 
120
121protected override void OnPaint(PaintEventArgs e) 
122
123{ 
124
125e.Graphics.FillRectangle(new SolidBrush(Color.Azure), ClientRectangle); 
126
127e.Graphics.DrawLine(Pens.DarkSalmon, 
128
129new Point(0, 0), 
130
131new Point(ClientRectangle.Width, ClientRectangle.Height)); 
132
133} 
134
135public static void  Main  (string[] m) 
136
137{ 
138
139Form f = new Form(); 
140
141T t = new T(); 
142
143t.Dock = DockStyle.Fill; 
144
145f.Controls.Add(t); 
146
147Application.Run(f); 
148
149} 
150
151}</object>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus