zoukankan      html  css  js  c++  java
  • 【CITE】利用鼠标绘图C#

    实例018 利用鼠标绘图

    光盘位置:光盘MR118

    在常用的画图软件中,用户一般都可以通过鼠标在其中绘图,那么该功能是如何实现的呢?本实例将讲解如何使用C#实现通过拖动鼠标在窗体上绘图的功能,实例运行效果如图1.18所示。

     
    图1.18  利用鼠标绘图

    本实例实现时主要用到了Graphics类的DrawLine方法和MouseEventArgs类的X属性、Y属性,下面分别对它们进行详细介绍。

    Graphics类中的DrawLine方法主要用来绘制直线,该方法为可重载方法,本实例中用到的重载形式如下:

    1. public void DrawLine (Pen pen,Point pt1,Point pt2) 

    参数说明

    pen:Pen对象,它确定线条的颜色、宽度和样式。

    pt1:Point结构,它表示要连接的第一个点。

    pt2:Point结构,它表示要连接的第二个点。

    说明:关于MouseEventArgs类的X属性和Y属性的详细讲解,请参见实例005中的关键技术。

    (1)打开Visual Studio 2008开发环境,新建一个Windows窗体应用程序,并将其命名为MouseToDraw。

    (2)更改默认窗体Form1的Name属性为Frm_Main。

    (3)程序主要代码如下:

    private void Form1_MouseMove(object sender, MouseEventArgs e)  
    {  
     
      if (lastPoint.Equals(Point.Empty))             
    //判断绘图开始点是否为空  
     
      {  
     
          lastPoint = new Point(e.X, e.Y);           
    //记录鼠标当前位置  
     
      }  
     
      if (G_OnMouseDown)                            
    //开始绘图  
     
      {  
     
          Point cruuPoint = new Point(e.X, e.Y);      
    //获取鼠标当前位置  
     
          graphics.DrawLine(pen, cruuPoint, lastPoint); 
    //绘图  
     
      }  
     
      lastPoint = new Point(e.X, e.Y);             
    //记录鼠标当前位置  
    }  
    private void Form1_MouseUp(object sender, MouseEventArgs e)  
    {  
     
      G_OnMouseDown = false;                        
    //开始绘图标识设置为false  
    }  
    private void Form1_MouseDown(object sender, MouseEventArgs e)  
    {  
     
      G_OnMouseDown = true;                             //开始绘图标识设置为true  
    } 
  • 相关阅读:
    OpenGL的几何变换2之内观察立方体
    OpenGL的几何变换[转]
    OpenGL的glPushMatrix和glPopMatrix矩阵栈顶操作函数详解
    OpenGL的glScalef缩放变换函数详解
    [centos][ntp][administrator] chrony ntp
    [dpdk][kni] dpdk kernel network interface
    [administrator][netctl] 给未插线未UP端口设置IP
    [administrator] rpmbuild
    OWA (Office Web Access)
    [network] netfilter
  • 原文地址:https://www.cnblogs.com/hardsoftware/p/5715691.html
Copyright © 2011-2022 走看看