Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:19:30 C#中定义鼠标指针为指定的图片实例
定义鼠标指针为指定的图片除了定义鼠标指针为指定的形状之外,还可以将鼠标指针定义为指定的图片。将鼠标指针定义为指定图片的方法非常简单,即将Cursor属性设置为指定鼠标指针样式的图片即可。示例定义鼠标为指定图片本示例通过设置窗体的Cursor属性,在窗体上显示指定图片样式的鼠标指针形状。示例运行结果如图1所示。程序主要代码如下:private void button1_Click(object sender, EventArgs e){this.Cursor = new Cursor("a.cur");}注意:a.cur是鼠标文件图片。完整程序代码如下:★ ★★★★frmMouseImage.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_02{public partial class frmMouseImage : Form{public frmMouseImage(){InitializeComponent();}private void frmMouseImage_Load(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("")).LastIndexOf(""));reportPath += @"a.cur";this.Cursor = new Cursor(reportPath);}private void button2_Click(object sender, EventArgs e){//本教程来自:http://www.isstudy.com}}}★ ★★★★frmMouseImage.Designer.cs窗体设计文件完整程序代码网站源代码★★★★★namespace _2_02{partial class frmMouseImage{/// /// 必需的设计器变量。/// 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.button1 = new System.Windows.Forms.Button();this.SuspendLayout();//// button1//this.button1.Location = new System.Drawing.Point(60, 12);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(132, 23);this.button1.TabIndex = 0;this.button1.Text = "自定义图片";this.button1.UseVisualStyleBackColor = true;this.button1.Click += new System.EventHandler(this.button1_Click);//// frmMouseImage//this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(292, 128);this.Controls.Add(this.button1);this.Name = "frmMouseImage";this.Text = "frmMouseImage";this.Load += new System.EventHandler(this.frmMouseImage_Load);this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.Button button1;}}★ ★★★★Program.cs主程序文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.Windows.Forms;namespace _2_02{static class Program{//本教程来自:http://www.isstudy.com/// /// 应用程序的主入口点。/// [STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmMouseImage());}}}图1 定义鼠标指针形状为指定图片
赞