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

The author:(作者)qq
published in(发表于) 2014/7/11 9:19:28
C#中定义鼠标指针为指定的动画图标实例

C#中定义鼠标指针为指定的动画图标实例|方法

定义鼠标指针为指定的动画图标

在Windows窗体中,通过设置控件的属性无法将鼠标指针设置为动画图标的形式,如果要实现该功能,可以通过API函数LoadCursorFromFile和SetClassLong实现。这两个函数的声明代码如下:

[DllImport("user32", EntryPoint = "LoadCursorFromFile")]

public static extern int LoadCursorFromFile(string lpFileName);

[DllImport("user32", EntryPoint = "SetSystemCursor")]

public static extern void SetSystemCursor(int hcur, int i);

注意:调用API函数时,需要导入using System. Runtime. InteropServices命名空间。

示例

定义鼠标指针为指定的动画图标。

本示例中,程序运行时,当鼠标指针移动到窗体上时,鼠标指针显示动画效果。示例运行结果如图1所示。



图1 鼠标为指定的动画图标

private void frmPicut_Load(object sender, EventArgs e)

{

string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,

Application.StartupPath.LastIndexOf("")).LastIndexOf(""));

reportPath += @"sl3210mouse.ani";

int cur = LoadCursorFromFile(reportPath);

SetSystemCursor(cur, 32512);

}

private void frmPicut_FormClosing(object sender, FormClosingEventArgs e)

{

int cur = LoadCursorFromFile(@"C:WINDOWSCursorsarrow_m.cur");

SetSystemCursor(cur, 32512);

}

完整程序代码如下:

★ ★★★★Form1.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_03

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

}

}

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

namespace _2_03

{

partial class Form1

{

///



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

///


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.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.Text = "Form1";

}

#endregion

}

}

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

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace _2_03

{

public partial class frmPicut : Form

{

public frmPicut()

{

InitializeComponent();

}

[DllImport("user32", EntryPoint = "LoadCursorFromFile")]

public static extern int LoadCursorFromFile(string lpFileName);

[DllImport("user32", EntryPoint = "SetSystemCursor")]

public static extern void SetSystemCursor(int hcur, int i);

private void frmPicut_Load(object sender, EventArgs e)

{

string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,

Application.StartupPath.LastIndexOf("")).LastIndexOf(""));

reportPath += @"mouse.ani";

int cur = LoadCursorFromFile(reportPath);

SetSystemCursor(cur, 32512);

}

private void frmPicut_FormClosing(object sender, FormClosingEventArgs e)

{

int cur = LoadCursorFromFile(Environment.SystemDirectory.Substring(0,Environment.SystemDirectory.LastIndexOf(""))+@"Cursorsarrow_m.cur");

SetSystemCursor(cur, 32512);

}

}

}

★ ★★★★frmPicut.Desigment.cs窗体设计文件完整程序代码★★★★★


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





QQ:154298438
QQ:417480759