Recently I have been hearing from more and more people who are embedding Windows Forms controls into webpages and I thought that I would put together a series of blog posts on this technique.
Since the original release of .NET Windows Forms has supported hosting controls inside of a web browser (Internet Explorer). The control would be loaded inside the internet explorer process and would be visually represented inline with the other contents of the html page. Here's how it works.
First we'll create a really simple control:
File: SimpleControl.cs
using System;using System.Drawing;using System.Windows.Forms;
public class GreenControl:Control { public RedControl() { this.BackColor = Color.Green; }}
And a really simple web page to host it:
File: Host.html
<html> <head><title>Simple control host</title></head><body> <H1>Simple control host page</H1> <object width=20 height=20 classid="SimpleControl.dll#GreenControl"></object></body></html>
Then compile the control with the command line: csc /target:library SimpleControl.cs
I now have 3 files (SimpleControl.cs Host.html and SimpleControl.dll) in a directory. I copy the last two files (.html and .dll) to a web server somewhere and navigate to the html file to see a completely uninteresting web page with a small green square of managed code.