又一个winform控件在web中使用的说明

在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.DoubleBuffe

r,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,在你的控件上画画线吧

 

posted on 2008-10-28 13:46 tiger534 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/zgff008/archive/2008/10/28/1321266.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完整的服务端及客户端调用程序,在win7+ vs2015环境运行通过. 一、说明 1、创建winfrom应用程序;(或者是控制台项目) 2、在项目添加一个WCF服务,并实现服务; 3、在需要启动WebService服务的地方启动该服务即可; 二、代码如下: 1、新建一个WCF服务——定义服务接口    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]     public interface ICalculator     {         [OperationContract]         double Add(double n1, double n2);     } 2、新建一个WCF服务——实现服务 public class CalculatorService : ICalculator     {         public double Add(double n1, double n2)         {             return n1 + n2;         }     } 3、添加完WcF服务后会在应用程序配置文件有入下节点                             <!--TestServer.ICalculator服务定义的接口,根据自己定义进行修改-->                                                                   <baseAddresses> <!--这个是要发布的服务地址,可以进行修改-->                                   </baseAddresses>                   4、在要启动服务的地方启动服务监听   public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { try { //打开服务创建监听,并开始监听消息 ServiceHost serviceHost = new ServiceHost(typeof(Service1));//需要using System.ServiceModel; serviceHost.Open(); label1.Text = "服务启动正常"; } catch (Exception ex) { label1.Text = ex.Message; } } 5、下面可以在客户端通过上面的服务地址”http://xxx.xxx.xxx.xx:8733/test/Service1/“对服务进行调用 到这步就实现在控制台实现webService的发布。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值