All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

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

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:19:28 Browse times: 347 Comment times: 0

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


Head photo

Go homepage
Upload pictures
Write articles

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窗体设计文件完整程序代码★★★★★




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.