Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:22:47
C#教程:MouseDown和MouseUp事件

C#教程:MouseDown和MouseUp事件

MouseDown和MouseUp事件

1.MouseDown事件

当鼠标指针位于控件上并按下鼠标键时发生。

语法:

public event MouseEventHandler MouseDown

参数说明如下。

MouseEventHandler:表示将处理窗体、控件或其他组件的MouseDown、MouseUp或 MouseMove事件的方法。

MouseEventHandler委托事件语法:

public delegate void MouseEventHandler (Object sender,MouseEventArgs e)

参数说明如下。

sender:事件源。

e:包含事件数据的MouseEventArgs。

MouseEventArgs用来构造函数对数,其公共属性及说明如表1所示。



表1 MouseEventArgs参数属性及说明

示例

MouseDown事件的使用

本示例单击鼠标右键,弹出快捷菜单。示例运行结果如图1所示。



图1 MouseDown事件的使用

private void Form1_MouseDown(object sender, MouseEventArgs e)

{

OnMouseDown = True;

if (e.Button == MouseButtons.Right)

{

this.contextMenuStrip1.Show(this, 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();

}

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的值设为目前点currPoint

private void Form1_MouseMove(object sender, MouseEventArgs e)

{

}

//当鼠标离开时把布尔变量设为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));

}

}

private 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 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

///


private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);

this.blackToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.whiteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.redToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.greenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.blueToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.contextMenuStrip1.SuspendLayout();

this.SuspendLayout();

//

// contextMenuStrip1

//

this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.blackToolStripMenuItem,


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759