Start talking about smart Client from using Windows forms controls in Internet Explorer

Source: Internet
Author: User
Tags html page iis variable
The client|window| control first references the article in the Microsoft Windows Forms QuickStart Tutorial, "Using Windows Forms Controls in Internet Explorer": (if some of the referenced information doesn't appear correctly, see the link above directly above) )

This topic describes how to successfully execute a Windows forms control within Internet Explorer (IE). Windows forms controls within IE can be activated without user prompting, it does not require registration, and uses common language runtime (CLR) code access security.

Activating Windows Forms controls within Internet Explorer takes five steps, each of which is listed below, and is described in detail below.

Create a Windows forms control.
Creates an HTML document with an OBJECT tag.
Create a virtual directory and set permissions.
Run the control.
To create a Windows forms control
You can host almost any Windows forms control in Internet Explorer, but for this example, we will host the SimpleControl contained in the Create Control section of this QuickStart tutorial. The control must be installed in the global assembly cache or in the same virtual directory as the Web page that contains it.



To create an HTML document with an OBJECT tag
The next step is to create an HTML document with an OBJECT tag that references the Windows forms control. For this example, you will also add a few simple scripts and input tags to demonstrate programmatic access to the control.

<object id= "SimpleControl1"

Classid= "Http:simplecontrol.dll#microsoft.samples.winforms.cs.simplecontrol.simplecontrol"
height= "Width=" viewastext>
<param name= "Text" value= "Simple Control" >
</object>


ClassID has two interesting parts: the path to the control library and the fully qualified name of the control, separated by the # number. If you are familiar with the ActiveX object tag, you will notice that a GUID is missing. In Windows forms, a combination of paths and fully qualified class names is a unique identifier.

The Param tag can be used to set properties on the control. In this case, the name attribute is the names of the properties, and the value attribute is the property.


<script>

function Changetext () {
Simplecontrol1.text = Text1.value;
}

</script>
?-

<input type= "text" id= "Text1" >
<input type= "button" value= "Change Text" onclick= "Changetext ()" >


To get programmatic access to a control, you can script the control. Use the buttons and text boxes on the page with simple JScript function Changetext to set the control's Text property. The following is the complete HTML and scripting code for this example.


<script language= "JScript" >

function Changetext () {
Simplecontrol1.text = Text1.value;
}

</script>

<body>

<p>simple Control
<br>
<br>
</body>

<object id= "SimpleControl1"

Classid= "Http:simplecontrol.dll#microsoft.samples.winforms.cs.simplecontrol.simplecontrol"
height= "Width=" viewastext>
<param name= "Text" value= "Simple Control" >
</object>

<br>
<br>

<input type= "text" id= "Text1" >
<input type= "button" value= "Change Text" onclick= "Changetext ()" >


Create a virtual directory and set permissions
The HTML page must reside in the IIS virtual directory on the WEB server and must have the appropriate permissions. In this example, a Windows forms control resides in the same directory, but it can also be installed into the global assembly cache. The execution permissions on the virtual directory must be set to scripts, and the control will not be activated correctly if the Execute permission is set to Scripts & executables. For this example, these steps have been performed for you.


Run the control
To run the control, simply point Internet Explorer to the HTML page in the virtual directory. If the control is not properly activated, you may need to restart Internet Explorer.

///////////////////////////////////////////////////////////////////////////////

End of reference

Since the last time I saw the Webexc event on MSDN, the so-called smart Client who put the window program on the Web page in Webcast_asp.net_2 (that is, asp.net technique 2), I couldn't sleep. What about the model?
And always repeated look at Xiao Chen opened the two original code of the picture:

So, this code of Little Chen is in the article quoted above:
<object id= "SimpleControl1"
Classid= "Http:simplecontrol.dll#microsoft.samples.winforms.cs.simplecontrol.simplecontrol"
height= "Width=" viewastext>
<param name= "Text" value= "Simple Control" >
</object>
It can be said to be the same.

In the past, I didn't pay much attention to looking at window Forms QuickStart articles. I remember I used to read those articles. But I have always been the development of B/s mainly, so it is seen also because there is no application and forget. Words outside

So I did exactly the same thing with the Quick Start sample code.
It worked!
I feel special excitement, even in the middle of the night, I want to continue to go on.

I started doing it myself ...
The first one I put up is a form that writes out a normal situation.
It doesn't show, I'm dizzy. Why not show it. I also do it according to the above meaning!
I have read my code several times, yes, but it's hopeless.
I later found that the form can not be put up, but only the control
Please refer to the previous program picture of Xiao Chen's last demo:



In this picture (left for the web, left window), you will find that the window form in the demo is a little bit different from the one on the Web: a "Game" menu on the Winow.
So it's a form file (the MainMenu control cannot be placed on the control). Rather than a control. And the one under the menu. So it can be separated and placed on the web (should be a user control or custom control on the Web).
I originally thought, Xiao Chen that. exe file put up. Is the start of an application's main () function.
I'll say it again. The form file cannot be put up. or all the files inherited from form are not put up. (Cause I don't know yet.) I used to engage in B/s. Oh. )

After I failed to put the form file up, I tried to put the controls on again many times. I will not say much of the failure. I have to explore some of the things that may be useful to everyone to share.

1. Here I suggest that you use the "user control" to play first: it is more convenient. The operation of a custom control is a bit of a visualization. However, as long as the controls are OK.

2. Part of the code in the control code to delete!
The default code for creating a new user Close is:
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;

Namespace WindowsApplication3
{
<summary>
Summary description of the UserControl1.
</summary>
public class UserControl1:System.Windows.Forms.UserControl
{
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;//need to be erased

Public UserControl1 ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the InitializeComponent call

}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}//This method needs to be erased.

Code generated #region Component Designer
<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
components = new System.ComponentModel.Container ()//need to be deleted, but if you just pull a control in the designer, it will be gone.
}
#endregion
}
}
If you do not remove it, you will not be able to work properly on the web. After the code is removed:
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;

Namespace WindowsApplication3
{
<summary>
Summary description of the UserControl1.
</summary>
public class UserControl1:System.Windows.Forms.UserControl
{
<summary>
The required designer variable.
</summary>

Public UserControl1 ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the InitializeComponent call

}
Code generated #region Component Designer
<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{

}
#endregion
}
}

I'm not quite sure why. But when the resource is cleaned up, I think the control is loaded and placed in the Web cache. One but turn it off. The data in the cache is gone//I am not familiar with Windows, so I may make a mistake:

3. window controls on the Web cannot be run in XP style (maybe, but I haven't done it for a long time, after all, I'm a window layman.) Who made it, must be posted to share.

4. Projects can be either compiled into. exe or. dll files.

OK, now let's start with a try.
Create a new C # window project, name casually, I took a: SmartClient1
The Form1.cs file in the project, you do not like to delete it can also. I was removing it.
Then create a new user control: UserControl1.cs file.
Just pull a control over it and go up. I was pulling a button control.
Then I changed the background color and the back color of the control.
All the code is as follows:
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;

Namespace SmartClient1
{
<summary>
Summary description of the UserControl1.
</summary>
public class UserControl1:System.Windows.Forms.UserControl
{
Private System.Windows.Forms.Button button1;
<summary>
The required designer variable.
</summary>


Public UserControl1 ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the InitializeComponent call

}



Code generated #region Component Designer
<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
This.button1 = new System.Windows.Forms.Button ();
This. SuspendLayout ();
//
Button1
//
This.button1.BackColor = System.Drawing.Color.Teal;
This.button1.Location = new System.Drawing.Point (352, 112);
This.button1.Name = "Button1";
This.button1.TabIndex = 0;
This.button1.Text = "Button1";
//
UserControl1
//
This. BackColor = System.Drawing.Color.PaleTurquoise;
This. Controls.Add (This.button1);
This. Name = "UserControl1";
This. Size = new System.Drawing.Size (456, 150);
This. ResumeLayout (FALSE);

}
#endregion
}
}

All right, compile it. (I'm programming it as a DLL file)
Next we'll build an HTML page that calls it.
Before I built it, I created a new folder in D and set it in IIS to be a virtual directory: "SmartClient".
Then create a new a.htm file and a "bin" folder in the new folder I just created (for the DLL file I just compiled).
Then, the DLL file that you just compiled is added to this "bin" folder.
The code for the a.htm file is as follows:
<title>window control on the Web Test </title>
<body >
<object id= "obj1" classid= "Http:bin/smartclient1.dll#smartclient1.usercontrol1" viewastext>

</object>
</body>
Http:bin/smartclient1.dll#smartclient1.usercontrol1 meaning, I quote in the document has a description, you can refer to. And I should have seen it just now.
All right. Now you can open "http://localhost/SmartClient/a.htm" to try:
The effect chart is as follows:



So now we're going to go back a little bit more:
Create a new form file and point the form file through the button control just now. (I just deleted Form1.cs, I had to rebuild one.) )
The code is basically unchanged.
Just added a click event to the Button1:
This.button1.Click + = new System.EventHandler (This.button1_click);

private void Button1_Click (object sender, System.EventArgs e)
{
New Form1 (). Show ();
}
New form file I don't have any code plus no minus, as it is. So there's no code here.

Compile it again. Back the DLL file to the past. Remember to delete the original from the first. or cover it up.
Then turn off the original page. Because a DLL changes a Web page, you must clean out the original cache. So we have to open a new ie.
In the opening of new ie. Do not select the historical address that the Smart Display in the address bar gives you. That way you're just going to load the original control, because it's still in the history cache.

OK, now try to see! It worked. I click on that button, and the window form appears (what do you think this means?) )。
The effect chart is as follows:




I'm not going to talk about that smart client at the developer Conference. But there's a little problem in it (as I said in an article I read last time). Let me talk about it. When you click the. exe file on the Web, you will be downloaded to the download list. And you can only erase the download tool:

Maybe you could try it by using a control on the web that I used to transport to a window. is to start the system by doing a shipment control on the Web.
You can start a form, or you can start an EXE thread (which I have not tried on the web, and I forgot how to start so I have no way to try until then).
As for the. NET Framework security Configuration. Because there are already some very good articles on the Internet. I'm not going to talk about it, and I've talked about it in the papers cited above. Then I'll post their URIs in my Bolg.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.