C#实现自定义动画鼠标的示例详解
作者:芝麻粒儿
这篇文章主要为大家详细介绍了如何利用C#实现自定义动画鼠标效果,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
实践过程
效果
代码
public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("user32.dll")] public static extern IntPtr LoadCursorFromFile(string fileName); [DllImport("user32", EntryPoint = "LoadCursorFromFile")] public static extern int IntLoadCursorFromFile(string lpFileName); [DllImport("user32", EntryPoint = "SetSystemCursor")] public static extern void SetSystemCursor(int hcur, int i); const int OCR_NORAAC = 32512;//标准 const int OCR_HAND = 32649;//手 const int OCR_NO = 32648;//斜的圆 const int OCR_SIZEALL = 32646;//移动 private void ToolS_From_Click(object sender, EventArgs e) { Cursor myCursor = new Cursor(Cursor.Current.Handle); IntPtr colorCursorHandle = LoadCursorFromFile("0081.ani");//鼠标图标路径 myCursor.GetType().InvokeMember("handle", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField, null, myCursor, new object[] { colorCursorHandle }); this.Cursor = myCursor; } private void ToolS_FromRevert_Click(object sender, EventArgs e) { this.Cursor = Cursors.Default; } private void ToolS_System_Click(object sender, EventArgs e) { //将要修改的标鼠图片存入到系统的WINDOWS\Cursors目录下 //设置正常选择鼠标 int cur = IntLoadCursorFromFile(@"C:\WINDOWS\Cursors\01.cur"); SetSystemCursor(cur, OCR_NORAAC); //设置移动 cur = IntLoadCursorFromFile(@"C:\WINDOWS\Cursors\03.cur"); SetSystemCursor(cur, OCR_SIZEALL); //设置不可用 cur = IntLoadCursorFromFile(@"C:\WINDOWS\Cursors\04.cur"); SetSystemCursor(cur, OCR_NO); //设置超链接 cur = IntLoadCursorFromFile(@"C:\WINDOWS\Cursors\06.cur"); SetSystemCursor(cur, OCR_HAND); } private void ToolS_SystemRevert_Click(object sender, EventArgs e) { //恢复正常选择鼠标 int cur = IntLoadCursorFromFile(@"C:\WINDOWS\Cursors\arrow_m.cur"); SetSystemCursor(cur, OCR_NORAAC); //恢复移动 cur = IntLoadCursorFromFile(@"C:\WINDOWS\Cursors\move_r.cur"); SetSystemCursor(cur, OCR_SIZEALL); //恢复不可用 cur = IntLoadCursorFromFile(@"C:\WINDOWS\Cursors\no_r.cur"); SetSystemCursor(cur, OCR_NO); //恢复超链接 cur = IntLoadCursorFromFile(@"C:\WINDOWS\Cursors\hand.cur"); SetSystemCursor(cur, OCR_HAND); } }
partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.鼠标样式设置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.ToolS_From = new System.Windows.Forms.ToolStripMenuItem(); this.ToolS_FromRevert = new System.Windows.Forms.ToolStripMenuItem(); this.ToolS_System = new System.Windows.Forms.ToolStripMenuItem(); this.ToolS_SystemRevert = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.鼠标样式设置ToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(292, 24); this.menuStrip1.TabIndex = 2; this.menuStrip1.Text = "menuStrip1"; // // 鼠标样式设置ToolStripMenuItem // this.鼠标样式设置ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.ToolS_From, this.ToolS_FromRevert, this.ToolS_System, this.ToolS_SystemRevert}); this.鼠标样式设置ToolStripMenuItem.Name = "鼠标样式设置ToolStripMenuItem"; this.鼠标样式设置ToolStripMenuItem.Size = new System.Drawing.Size(89, 20); this.鼠标样式设置ToolStripMenuItem.Text = "鼠标样式设置"; // // ToolS_From // this.ToolS_From.Name = "ToolS_From"; this.ToolS_From.Size = new System.Drawing.Size(190, 22); this.ToolS_From.Text = "在窗体中改变鼠标样式"; this.ToolS_From.Click += new System.EventHandler(this.ToolS_From_Click); // // ToolS_FromRevert // this.ToolS_FromRevert.Name = "ToolS_FromRevert"; this.ToolS_FromRevert.Size = new System.Drawing.Size(190, 22); this.ToolS_FromRevert.Text = "在窗体中还原鼠标样式"; this.ToolS_FromRevert.Click += new System.EventHandler(this.ToolS_FromRevert_Click); // // ToolS_System // this.ToolS_System.Name = "ToolS_System"; this.ToolS_System.Size = new System.Drawing.Size(190, 22); this.ToolS_System.Text = "改变系统的鼠标样式"; this.ToolS_System.Click += new System.EventHandler(this.ToolS_System_Click); // // ToolS_SystemRevert // this.ToolS_SystemRevert.Name = "ToolS_SystemRevert"; this.ToolS_SystemRevert.Size = new System.Drawing.Size(190, 22); this.ToolS_SystemRevert.Text = "还原系统的鼠标样式"; this.ToolS_SystemRevert.Click += new System.EventHandler(this.ToolS_SystemRevert_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 172); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "Form1"; this.Text = "自定义动画鼠标"; this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem 鼠标样式设置ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem ToolS_From; private System.Windows.Forms.ToolStripMenuItem ToolS_FromRevert; private System.Windows.Forms.ToolStripMenuItem ToolS_System; private System.Windows.Forms.ToolStripMenuItem ToolS_SystemRevert; }
到此这篇关于C#实现自定义动画鼠标的示例详解的文章就介绍到这了,更多相关C#自定义动画鼠标内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!