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

The author:(作者)qq
published in(发表于) 2014/7/11 9:22:42
C#教程:MouseClick和MouseDoubleClick事件使用实例

C#教程:MouseClick和MouseDoubleClick事件使用实例

MouseClick和MouseDoubleClick事件

1.MouseClick事件

该事件发生于单击控件时。

语法:

public event EventHandler MouseClick

参数说明如下。

EventHandler:表示将处理不包含事件数据的事件的方法。

示例

MouseClick事件的使用

在窗体中单击任何地方,触发MouseClick事件,在事件中起动timer控件,将窗体标题变成一个时钟。示例运行结果如图1所示。



图1 MouseClick事件的使用

private void frmMouseClick_MouseClick(object sender, MouseEventArgs e)

{

this.timer1.Enabled = True;

}

完整程序代码如下:

★ ★★★★frmMouseClick.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_04

{

public partial class frmMouseClick : Form

{

public frmMouseClick()

{

InitializeComponent();

}

private void frmMouseClick_MouseClick(object sender, MouseEventArgs e)

{

this.timer1.Enabled = true;

}

private void frmMouseClick_MouseDoubleClick(object sender, MouseEventArgs e)

{

}

private void timer1_Tick(object sender, EventArgs e)

{

this.Text = "现在时间" + DateTime.Now.ToString();

}

private void frmMouseClick_Load(object sender, EventArgs e)

{

}

}

}

★ ★★★★frmMouseClick.Designer.cs窗体设计文件完整程序代码★★★★★

namespace _2_04

{

partial class frmMouseClick

{

///



/// 必需的设计器变量。

///


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.timer1 = new System.Windows.Forms.Timer(this.components);

this.SuspendLayout();

//

// timer1

//

this.timer1.Interval = 1000;

this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

//

// frmMouseClick

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(451, 145);

this.Name = "frmMouseClick";

this.Text = "frmMouseClick";

this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.frmMouseClick_MouseDoubleClick);

this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.frmMouseClick_MouseClick);

this.Load += new System.EventHandler(this.frmMouseClick_Load);

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Timer timer1;

}

}

★ ★★★★Program.cs主程序文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace _2_04

{

static class Program

{

///

/// 应用程序的主入口点。

///


[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new frmMouseClick());

}

}

}

2.MouseDoubleClick事件

该事件发生于双击控件时。

语法:

public event EventHandler MouseDoubleClick

示例

MouseDoubleClick事件

双击窗体中的任何地方,触发MouseDoubleClick事件,在该事件中停止Timer控件。

private void frmMouseClick_MouseDoubleClick(object sender, MouseEventArgs e)

{

this.timer1.Enabled = False;

}

完整程序代码如下:

★ ★★★★frmMouseClick.cs窗体代码文件完整程序代码★★★★★


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





QQ:154298438
QQ:417480759