鼠标重要性不必多说,我们这些"小毛孩"可能不敢想象没有鼠标时的计算机是如何操作的,我承认鼠标不是不可替代,但也已不可或缺.
大多鼠标编程都与API函数有着某种关系,毕竟鼠标的操作已进入了非纯软件领域.因此,要对鼠标下刀,就得熟悉相关的API函数,这点无可厚非.
今天我们就对鼠标编程进行一个大杂汇.工作中能否用到,不太清楚.....
下面就分四个部分讲述我们与鼠标的不解之缘.
一.应用篇:
1.利用鼠标绘图
很多很多书籍,很多很多网页都在讲述这个东东,为了不找骂,这里直接附代码!
实现:利用窗体的MouseDown,MouseMove,MouseUp事件及Pen,Graphics等类实现.
代码:
鼠标绘图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ziyiMouse1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pen = new Pen(Color.FromName("black"));//始末画笔
graphics = CreateGraphics();//初始画板
}
public bool G_OnMouseDown = false;//控制画图
public Point lastPoint = Point.Empty;
public Pen pen;
public Graphics graphics;
//将上一个点的LastPoint的值设为目前点的currPoint值.
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);
}
//当鼠标离开时把控制画图设为false;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
G_OnMouseDown = false;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
G_OnMouseDown = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
效果: