在Visual Studio .net中,类似于Applet或ActiveX控件,WinForm控件可以嵌入IE中使用。嵌入IE的Windows窗体控件不要求注册,不需用户提示即可激活。我们可以很方便地实现一些WebForm中实现起来相对麻烦的交互操作,结合.net Remoting等技术访问后台数据库,则可生成功能强大而且美观的WebForm页面。 使用该技术,需要客户端安装.net FrameWork及IE 6.0,在Windows 2003中已经自带了.net FrameWork。 嵌入WebForm的WinFrom控件利用公共语言运行库代码访问安全性,一些特殊操作还需要设置访问权限。
下面就让我们做个简单的例子,在WinForm用户控件中使用GDI+实现画线功能,并把它嵌入IE浏览器。 开发环境:Windows 2000专业版、Visualt Studio .net 2002
1.创建WinForm用户控件 我们可以建立一个“Windows控件库”项目,最后嵌入浏览器时只需要生成的dll文件。但为了方便调试,我们可以先把控件嵌入WinForm中。 新建“Windows应用程序”,名称为WinFormInWebForm,生成的解决方案也名称为WinFormInWebForm。在解决方案中再添加一个“Windows控件库”项目WinFormControl,系统在该项目中自动添加一个了UserControl1的用户控件,删除该控件,然后在“Windows控件库”项目中添加一个用户控件WinFormGDICtrl。 现在我们先把该控件加如“Windows应用程序”的Form1中。 首先需要生成解决方案以生成控件的dll文件。然后打开工具箱,点右键选择“添加选项卡”,在工具箱中添加一个“WinForm控件”选项卡。在该选项卡上点右键,选择“自定义工具箱”,弹出自定义工具箱页面。切换到.net框架组件页面,单击浏览,到“\WinFormControl\bin\Debug”目录选择WinFormControl.dll文件,打开后在“WinForm控件”选项卡里就会出现WinFormGDICtrl控件,这时就可以把该控件拖动到Form1上了。

打开WinFormGDICtrl.cs文件,我们可以看到WinFormGDICtrl类继承自System.Windows.Forms.UserControl。 由于我们要使用GDI+绘图,为防止由控件重绘引起的闪烁,我们可以启用双缓冲,指定控件的ControlStyles.DoubleBuffer为true。要完全启用双缓冲,必须也要将 UserPaint 和 AllPaintingInWmPaint位数设置为 true。在构造函数中加入 public WinFormGDICtrl() { InitializeComponent();
this.SetStyle(ControlStyles.UserPaint,true); this.SetStyle(ControlStyles.AllPaintingInWmPaint,true); this.SetStyle(ControlStyles.DoubleBuffer,true); }
添加一个类LineObj,用于保存线对象,并给该类添加一个Draw方法用于画线 using System; using System.Drawing; namespace WinFormControl { public class LineObj { public Point m_startPoint; //起始点 public Point m_endPoint; //截止点
public LineObj(int x,int y) { m_startPoint=new Point(x,y); m_endPoint=new Point(x,y); }
public void Draw(Graphics g) { g.DrawLine(new Pen(Color.Blue,2),m_startPoint,m_endPoint); } } }
在WinFormGDICtrl类中添加两个类变量 private ArrayList m_arrayLines; private bool m_bDrawing; m_arrayLines为线对象集合,m_bDrawing指示是否画线。 并在类构造函数中初始化变量 m_arrayLines=new ArrayList(); m_bDrawing=false; 给控件添加MouseDown,MouseMove,MouseUp及Paint事件响应函数 private void WinFormGDICtrl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { LineObj m_lineObj=new LineObj(e.X,e.Y); m_arrayLines.Add(m_lineObj); m_bDrawing=true; }
private void WinFormGDICtrl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if(m_bDrawing) { LineObj m_lineObj=(LineObj)m_arrayLines[m_arrayLines.Count-1]; m_lineObj.m_endPoint=new Point(e.X,e.Y); this.Invalidate(); } }
private void WinFormGDICtrl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { m_bDrawing=false; }
private void WinFormGDICtrl_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g=e.Graphics; g.FillRectangle(Brushes.Yellow,this.ClientRectangle);
foreach(Object obj in m_arrayLines) { LineObj m_lineObj=(LineObj)obj; m_lineObj.Draw(g); } } 生成解决方案,运行Form1,你就可以看到控件的效果了 打开\WinFormControl\bin\Debug目录,其中的WinFormControl.dll就是我们所需要的
2.把控件嵌入IE浏览器 新建一个虚拟目录WinFormCtrl,把WinFormControl.dll文件复制进该目录中,再在该目录中创建一个带有object标记的html文件test.htm <html> <head> </head> <body> <object id="drawcontrol" classid="http:WinFormControl.dll#WinFormControl.WinFormGDICtrl" height=300px width=400px VIEWASTEXT></object> </body> </html> 其中我们关心的是objcect标记的classid,classid分为两部分:控件名(可包括路径)和控件的完全限定名,中间用“#”相隔。完全限定名由“命名空间.类名”组成 从示例来看 WinFormControl.dll为控件名,WinFormControl为控件命名空间,WinFormGDICtrl为控件类名。 打开IE,在地址栏输入http:\\localhost\WinFormCtrl\test.htm,在你的控件上画画线吧


|