YellowWee's Scripts

妳的世界能夠從此不同...而我的世界... 亦因妳而改變...

导航

如何把Windows Forms controls嵌入到HTML页面中~

Posted on 2004-06-01 14:26  YellowWee(端木柒)  阅读(2022)  评论(3编辑  收藏  举报


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, should 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 documents. 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 and Settings\UserName\Local Settings\Application Data\assembly\dl. (调试时需要注意这点. P.S:也可以通过改变程序集名称来更改assembly)
  • 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:测试后发现去掉classid="http:PuzzlePix.exe#PuzzleControl中的http:也能执行.注意一定要在IIS中运行此HTML Page,否则此段代码不能被解析.



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 calls to 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:Controls.dll和Controls.html放在同一(IIS虚拟)目录下.