1. 程式人生 > 實用技巧 >How to host winform control in IE

How to host winform control in IE

Hosting a Windows Form Control in a web page

Although it is not the most common use of it, it is possible to host a Windows Form Control in a Web Page and run it from within Internet Explorer. This allows to build powerful client side functionality with all the advantages of using the .Net framework and executing the control in the client side. Of course there are some restrictions that cannot be left aside. At least the .Net framework must be installed on the client for the control to run. In addition, it is possible that some permission must be granted to the control, too, depending on the actions the control will take on the client machine.
Let’s build an example to see how this works: 1. Create the Windows Form Control Create a “Windows Control Library” project in Visual Studio. For this simple example we will add a label to the control and a public “SendMessage” method to change the label’s text. In order to be able to call the method from IE, we will set the control’s COMVisible attribute to true.
using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsControlLibrary1 { [ComVisible(true)] public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public void SendMessage(string msg){ _lblMessage.Text = msg; } } } 2. Createthe hosting HTML document
The control will be hosted using the HTML <object> element. In its classid attribute we will place the control's reference in the following manner: classid="http:[relativePath/]<winControlAssemblyName>.dll#<ControlNamespace>.<ControlClassName>". For this example we will have an HTML page (index.html) with the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" ><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

<head>

<title>Windows Form Control Test Page</title>

<script type="text/javascript">

function SendMessageToWinControl() {

var winCtrl = document.getElementById("MyWinControl");

winCtrl.SendMessage("Message sent from the HTML page!!");

}

</script>

</head>

<body>

<h1>Windows Form Control:</h1>

<object id="MyWinControl" classid="http:WindowsControlLibrary1.dll#WindowsControlLibrary1.UserControl1"

height="100" width="300" VIEWASTEXT/>

<br/><br/>

<input type="button" onclick="SendMessageToWinControl()" value="Send Message" />

</body>

</html>

3. Deploy the sample Copy both the index.html page and the control library WindowControlLibrary1.dll to a directory (we will name it WinControlTest). In the “Web Sharing” dir properties select Share this folder, leave the default alias and application permissions to Scripts and make sure not to select the Execute permission). 4. Try it in Internet Explorer Open the page [url]http://localhost/WinControlTest/index.html[/url]in Internet Explorer. We can see the control hosted in the html page: WinControlTest2.JPG By clicking the Send Message button,we will call the Control's SendMessage method from javscript. Resulting in thelabel's text changed: WinControlTest3.JPG If you modify the control after its first use, you will need to close IE, replace the control's dll in the virtual directory and clean the contents of the download cache. You do this cleaning using the following command: "gacutil /cdl".Tolist the contents of the download cache use "gacutil /ldl". This solution is not intended to replace a WebControl or any common web artifact. But is good to know it can be use inan uncommon scenario if needed. Other references: [url]http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsIeSourcing.aspx[/url] [url]http://www.eggheadcafe.com/articles/20021205.asp[/url] [url]http://dotnet.org.za/codingsanity/archive/2006/04/20/51710.aspx[/url]