Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:22:50 C#教程:MouseMove事件使用实例
C#教程:MouseMove事件使用实例|方法
MouseMove事件当鼠标指针移到控件上时发生MouseMove事件。语法:public event MouseEventHandler MouseMove示例MouseMove事件的使用本示例实现的是在窗体的鼠标MouseMove事件中,实现画板功能。示例运行结果如图1所示。图1 MouseMove事件的使用private void Form1_MouseMove(object sender, MouseEventArgs e){if (lastPoint.Equals(Point.Empty)){ lastPoint = new Point(e.X, e.Y); }if (OnMouseDown){Point cruuPoint = new Point(e.X, e.Y);graphics.DrawLine(pen, cruuPoint, lastPoint);}lastPoint = new Point(e.X, e.Y);}完整程序代码如下:★ ★★★★frmMouse.cs窗体代码文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _2_05{public partial class frmMouse : Form{public frmMouse(){InitializeComponent();pen = new Pen(Color.FromName(G_str_color));//始末画笔graphics = CreateGraphics();//初始画板}public bool G_OnMouseDown = false;public Point lastPoint = Point.Empty;public string G_str_color = "black";public Pen pen;public Graphics graphics;//当你拖拽鼠标处理的和序,当没有上一点时,你目当所按下的点就是上一个点//目当所按下的点就是CurrPoint,使用您所选反背景色画笔pen从lastPointi画一条直线,然后将//上一个点LastPoint的值设为目前点currPointprivate 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;}//将布尔变量OnMouseDown设为true 按下鼠标右键时显示菜单private void Form1_MouseDown(object sender, MouseEventArgs e){G_OnMouseDown = true;if (e.Button == MouseButtons.Right){this.contextMenuStrip1.Show(this, new Point(e.X, e.Y));}}#region 获取背景色名称后转成Color对象充当Pen构造函数的自变量,用于创建画笔对象Penprivate void whiteToolStripMenuItem_Click(object sender, EventArgs e){G_str_color = ((ToolStripMenuItem)sender).Text;pen = new Pen(Color.FromName(G_str_color));}private void redToolStripMenuItem_Click(object sender, EventArgs e){G_str_color = ((ToolStripMenuItem)sender).Text;pen = new Pen(Color.FromName(G_str_color));}private void greenToolStripMenuItem_Click(object sender, EventArgs e){G_str_color = ((ToolStripMenuItem)sender).Text;pen = new Pen(Color.FromName(G_str_color));}private void blackToolStripMenuItem_Click(object sender, EventArgs e){G_str_color = ((ToolStripMenuItem)sender).Text;pen = new Pen(Color.FromName(G_str_color));}private void blueToolStripMenuItem_Click(object sender, EventArgs e){G_str_color = ((ToolStripMenuItem)sender).Text;pen = new Pen(Color.FromName(G_str_color));}#endregionprivate void frmMouse_Load(object sender, EventArgs e){}}}★ ★★★★frmMouse.Designer.cs窗体设计文件完整程序代码★★★★★namespace _2_05{partial class frmMouse{/// /// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。/// /// 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。///
赞