How to embed Windows Forms controls into HTML pages ~

Source: Internet
Author: User


Http://msdn.microsoft.com/msdnmag/issues/02/06/Rich/default.aspx

Windows Forms controls embedded in HTML pages:

  • The embedded class must be derived from control or from another control-derived type.
  • The embedded class must be public and contain a public default constructor (with no parameters ).
  • The object's size property must be set explicitly. if it is not set, it will not display in the HTML page. this can be done in one of two ways. set the size in markup using the width and height attributes of the <Object> tag, or you can chose to assign a size from within your control-derived type's constructor.
  • Code in your class will almost certainly execute with restricted access to the user's system. through configuration, it is possible to give a network-deployed Assembly greater permissions on a system. your code, though, shocould not assume the extra configuration step has been taken by a system administrator.
  • JScript in the HTML page can, and often must, be used to interact with the control by calling methods on the object. JScript helper code is often desirable for a number of reasons.
  • You must use a web server to test your browser control. unlike an ActiveX control, you cannot simply point Microsoft Internet Explorer to the appropriate HTML document in your file system and view the page with the control embedded.
  • the CLR Assembly resolver caches assemblies that contain controls for embedding in HTML statements. it will not redownload an assembly if it matches the strong name of the document in the cache. this means that at development time you must increment the version of your Assembly each time you want to test a code addition. to avoid this, You Should manually delete the Assembly from the Assembly Cache using a helper BAT file. the Assembly resolver download cache can be found at C: \ Documents ents and Settings \ USERNAME \ Local Settings \ Application Data \ Assembly \ DL. (pay attention to this during debugging. p.s: You can also change the Assembly by changing the Program set name.)
  • Embedded managed controls on a single page share the same managed appdomain. this means that they are running in the same "program. "your browser-deployed controls can access one another's public methods and properties as well as register for public event notification, and so on. this is a very cool and quite useful feature of browser controls.

Classid = "[location of assembly] # [class name]"

<Object ID = "Puzzle" border = "2" classid = "http: puzzlepix.exe # puzzlecontrol">

P.s: Remove classid = "http: puzzlepix.exe # http: In puzzlecontrol. Be sure to run this HTML page in IIS. OtherwiseCodeCannot be parsed.


Troubleshooting browser control code

  • Surprisingly, if the IIS virtual root is set to allow execute permissions of "scripts and executables, "Your managed browser control will not be hosted by the browser because IIS will treat DLL or EXE like an ISAPI and try to run it. setting the execute permissions for the virtual root to either "scripts" or "NONE" will allow your control to be hosted.
  • If your control-derived type is not marked as public, and if it does not have a public default constructor, then it will not be hosted in the browser.
  • Although form is derived from control, your browser control cannot be derived from form. If it is, the host will refuse to embed your control in the page.
  • Some security restrictions do not show themselves as a securityexception. for example, if your type overrides wndproc (a restricted feature), your type simply won't load in the browser. no exception is thrown because the code never even gets the opportunity to run.
  • If the control is not showing up in the browser, it is likely that it is because the object's constructor threw an exception that was not caught. you can use a blend of exception handling blocks and MessageBox. show callto find out if, and to what point, your code is being executed. if the constructor for your object is not being called at all, then it is most likely having trouble finding the assembly or finding the control type. in addition, the virtual root settings might also be wrong.
Controls. CS

Using system;
Using system. Windows. forms;

// Derive a class from ListBox
// ListBox is derived from control
Public class draglistbox: ListBox {
Public draglistbox (){

// Add support for drag and drop
Allowdrop = true;
}

// Public method for adding items to the control
Public void additem (string item ){
Items. Add (item );
}

// Implement drag and drop addition to ListBox
Protected override void ondragenter (drageventargs drgevent ){
If (drgevent. Data. getdatapresent (typeof (dragitem ))){
Drgevent. effect = dragdropeffects. move;
}
}

Protected override void ondragdrop (drageventargs drgevent ){
Dragitem item =
(Dragitem) drgevent. Data. getdata (typeof (dragitem ));
Items. Add (item. item );
}

Protected override void onmousemove (mouseeventargs e ){
Int32 Index = selectedindex;
If (index! =-1 & (E. Button & mousebuttons. Left )! = 0 )){
Dragitem item = new dragitem ();
Item. Item = items [Index];
Item. Sender = this;
Dragdropeffects result =
Dodragdrop (item, dragdropeffects. Move );
If (result = dragdropeffects. Move ){
Items. removeat (INDEX );
}
}
Base. onmousemove (E );
}

// Private class for dragged data
Class dragitem {
Public object item;
Public draglistbox sender;
}
}

 

Controls.html

<HTML>
<Head>
<Title> interacting controls example </title>
</Head>
<Body onload = "navigateto ()" bgcolor = "gainsboro">
<H1>
Interacting controls
</H1>
<HR>
<Object ID = "list1" classid = "http: controls. dll # draglistbox">
</Object>
</Hr>
<Br>
You can drag items from one list box to the other.
<HR>
<Object ID = "list2" classid = "http: controls. dll # draglistbox">
</Object>
</Hr>
<Script language = "jscript">
// Code to add items to one of the lists
Function navigateto (){
List1.additem ("Inky ");
List1.additem ("Pinky ");
List1.additem ("blinky ");
List1.additem ("Clyde ");
}
</SCRIPT>
</Body>
</Html>

p.s: Put controls.dlland controls.html in the same (IIS virtual) directory.

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.