Using Windows Forms Controls in Internet Explorer

 

This document describes how to successfully execute Windows Forms controls within Internet Explorer (IE). Windows Forms controls within IE are activated without user prompt, require no registration and utilize the Common Language Runtime (CLR) code access security.

There are four steps in getting the Windows Forms control activated within Internet Explorer, each listed here and detailed below.

  • Configure Code Access Permissions
  • Create the Windows Forms control
  • Create an HTML document with an object tag
  • Create the virtual directory, copy content and set permissions
  • Run the control
1. Windows Forms Control : SimpleControl.dll

Almost any Windows Forms control could be, but for this example the SimpleControl that is included in the .NET Framework SDK QuickStart Tutorial Creating Controls will be used.

2. HTML Document : SimpleControl.html

The next step is to create an HTML document with and object tag to insert and activate the Windows Forms control. Some simple script and input tags will also be added to demonstrate programmatic access to the control.

Object tag
<object id="simpleControl1"
    classid="http:SimpleControl.dll#Microsoft.Samples.WinForms.Cs.SimpleControl.SimpleControl"
    height="300"
    width="300">
    <param name="Text" value="Simple Control">
</object>

The classid has two interesting parts; the path to the control library and the fully qualified name of the control separated by the pound sign. If you are familiar with an ActiveX object tag, youll notice the lack of a GUID. In the case of Windows Forms, the combination of path and fully qualified classname serve as the unique identifier.

Param tags are used to set properties on controls. The name attribute is the name of the property and the value attribute is the value of the property.

Script
<script>

function ChangeText() {
      simpleControl1.Text = text1.value;
}

</script>


          
          
<input type="text" id="text1"> <input type="button" value="Change Text" οnclick="ChangeText()">

To gain programmatic access to your control, you can write script against it. A button and text box on the page is used in conjunction with the simple function ChangeText to set the text property of the control.

Complete

The following is the complete HTML code for this example.

<html>

<script>

function ChangeText() {
	simpleControl1.Text = text1.value;
}

</script>

<body>

    <p>Simple Control</p>
    <br>
    <br>
    
    <object id="simpleControl1"
        classid="http:SimpleControl.dll#Microsoft.Samples.WinForms.Cs.SimpleControl.SimpleControl"
        height="300"
        width="300">
        <param name="Text" value="Simple Control">
    </object>
    
    <br>
    <br>
    
    <input type="text" id="text1">
    <input type="button" value="Change Text" οnclick="ChangeText()">

</body>

</html>

3. Virtual Directory

Create a new virtual directory and populate it with both the control (SimpleControl.dll) and the HTML document (SimpleControl.html).

Important: Set execution permissions on the virtual directory to scripts. The control will not be properly activated if the execution permissions are set to scripts & executables.

4. Configure Code Access Permissions
This sample will execute correctly if you are running the control from an intranet site and you have not altered the .Net Framework security policy. If you have altered policy, or if you are running the control from an internet site then you may need to configure Internet Explorer or alter security policy to allow it to run. One way to enable a specific site for control download and execution is to identify your hosting page as belonging to the Trusted zone. This change is done in Internet Explorer with the following steps.

1. Choose Tools|Options
2. Select the "Security" tab
3. Select the "Trusted Sites" icon
4. Click the Sites... button
5. Add your site using the dialog presented
6. Click OK to commit your changes

Now, when you browse to that page, it should be in the Trusted Sites zone which has the Internet permission set by default.

You can also configure the .net framework security policy to enable code to run from a particular web site or from the Internet. The SDK documentation contains details of how to configure security policy.
5. Run the Control

To run the control, just point Internet Explorer to SimpleControl.html in your virtual directory. If the control is not activating correctly it may be necessary to restart Internet Explorer or clear the assembly download cache.

Note:You can view the contents of your assembly download cache using gacutil /ldl on the command line. You can clear the contents of the cache using gacutil /cdl

 
<think>好的,我现在需要帮助用户找到关于Modern.Forms的示例代码和使用教程。首先,我应该回忆一下用户提供的引用内容,特别是引用[1]和[2],因为它们提到了Modern.Forms的示例应用和文档。 用户提到Modern.Forms有一个名为ModernDecompiler的示例应用,还有ControlGallery、Explorer和Outlaw等示例。这些应该是用户需要查找的示例代码来源。因此,我应该建议用户查看这些示例项目,并指导如何获取它们。 接下来,用户可能需要知道如何安装和运行这些示例。引用[1]提到需要安装并运行,所以可能需要通过NuGet安装Modern.Forms包,然后克隆仓库或下载示例代码。另外,引用[4]提到了使用make命令编译源代码,但不确定是否适用于Modern.Forms,可能需要进一步确认,但可能用户需要运行示例的话,可能需要构建项目。 用户也可能需要基本的使用教程,比如如何创建窗口、添加控件等。引用[1]指出语法接近WinForms,所以可以建议有WinForms经验的用户迁移的方法,并给出简单的代码示例。 还要考虑到用户可能遇到的问题,比如如何获取帮助,这时候引用[2]提到查阅正式文档或在线社区,所以应该推荐这些资源。 需要确保回答结构清晰,分步骤说明,并引用正确的来源。同时,按照系统指令,使用正确的中文和LaTeX格式,不过这个问题可能涉及代码较多,所以注意代码块的格式。 最后,生成相关问题,帮助用户深入探索,比如优化方法、迁移技巧等。</think>### Modern.Forms 示例代码与使用教程 #### 一、示例代码获取与运行 1. **官方示例项目** Modern.Forms提供了多个高质量示例,包含基础控件演示和复杂应用实现: - `ControlGallery`:展示各类控件(按钮、文本框、列表等)的使用方式[^1] - `Explorer`:模拟文件浏览器界面,演示树形视图和列表视图联动 - `Outlaw`:文本编辑器应用,展示富文本处理能力 - `ModernDecompiler`:.NET程序集反编译工具,体现实际项目结构 **运行步骤**: ```bash git clone https://github.com/modern-forms/Modern.Forms.Samples cd Modern.Forms.Samples dotnet build dotnet run --project ControlGallery ``` 2. **基础代码片段** 创建简单窗口示例: ```csharp using Modern.Forms; class Program { static void Main() { var app = new Application(); var window = app.CreateWindow("Demo", 800, 600); var button = new Button { Text = "点击我", Location = new Point(50, 50) }; button.Click += (s, e) => MessageBox.Show(window, "Hello Modern.Forms!"); window.Controls.Add(button); app.Run(); } } ``` #### 二、核心使用技巧 1. **布局系统** 使用`StackLayout`或`TableLayout`实现响应式布局: ```csharp var panel = new StackLayout { Orientation = Orientation.Vertical, Items = { new Label("用户名"), new TextBox(), new Button("提交") } }; ``` 2. **数据绑定** 通过`DataContext`实现MVVM模式: ```csharp public class UserViewModel { public string Name { get; set; } = "默认用户"; } var viewModel = new UserViewModel(); var label = new Label().BindText(nameof(UserViewModel.Name)); label.DataContext = viewModel; ``` #### 三、进阶开发指引 1. **自定义控件** 继承现有控件创建扩展功能: ```csharp public class GradientButton : Button { protected override void OnPaint(PaintEventArgs e) { using var brush = new LinearGradientBrush(ClientRectangle, Color.Red, Color.Blue, 45); e.Canvas.FillRectangle(brush, ClientRectangle); base.OnPaint(e); } } ``` 2. **性能优化** 针对复杂界面建议: - 使用`SuspendLayout()`/`ResumeLayout()`控制渲染批次 - 对大数据列表启用虚拟化模式 ```csharp var listView = new ListView { VirtualMode = true, ItemCount = 100000 }; ``` #### 四、资源推荐 1. **文档参考** 项目官方文档包含完整的API参考和设计指南,建议优先查阅[^2] 2. **社区支持** 开发者可通过GitHub Issues提交问题,活跃社区响应速度通常在24小时内
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值