How to host winform control in IE
2007-10-12 14:20
387 查看
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. Create the 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" >
<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[/b], 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 http://localhost/WinControlTest/index.html in Internet Explorer. We can see the control hosted in the html page:
By clicking the Send Message button, we will call the Control's SendMessage method from javscript. Resulting in the label's text changed:
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".To list 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 in an uncommon scenario if needed.
Other references:
http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsIeSourcing.aspx
]http://www.eggheadcafe.com/articles/20021205.asp
http://dotnet.org.za/codingsanity/archive/2006/04/20/51710.aspx
相关文章推荐
- How to host winform control in IE
- How to show separators in WinForm's StatusStrip control?
- how to reference the parent form from the WPF control(Control in ElementHost)
- Passing Domain Credentials in C# to a WebBrowser Control in .Net WinForms Applications(winform中传递域认)
- How to keep Z-order of a control in MFC dialog?
- How to debug Control Panel applet (cpl) in VS .NET 2005
- How to setup Relay host in mail server
- Why is applicationhost.config still being added to source control even thought it's in gitignore
- How to run a user control assembly that is hosted on Internet Information Services (IIS) in Internet Explorer
- how to use ocx control in console app.
- FND:How to create a link in IE favorite to directly login into apps
- JS: How to control the IE(keyDown...)?
- (在做报文编辑器时右键菜单老不出来)This article explains how to display a context menu for a tree control in MFC
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0
- How To Determine When a Page Is Done Loading in WebBrowser Control
- How To Call Run() Method of the Microsoft Script Control in C++
- How to use For loop in CruiseControl.net
- 6 methods to control what and how your content appears in search engines
- How to add ComboBox control to the ToolBar control in VC - 如何在VC中向ToolBar控件加入ComboBox控件
- How to get control of your time and your life (Alan Lakein.)