Programmatically change custom mouse cursor in windows?

南楼画角 提交于 2020-04-07 02:54:07

问题


I am trying to change the windows cursors (the default is Windows Custom Scheme) to my custom cursors (It named Cut the rope):

Is there any idea to change all of cursors (Arrow, Busy, Help Select, Link select,...) to my Cut the rope?


回答1:


If you want to change the default Mouse Cursor theme:

You can just change it in the registry:

There are three main registry keys that come into play.

  1. The registry key HKEY_CURRENT_USER\Control Panel\Cursors contains the active user cursors

    1a) The values underneath this are the different types of cursors
    1b) The Scheme Source specifies the type of cursor scheme that is currently being used.

    The different values are:

    "0" – Windows Default
    "1" – User Scheme
    "2" – System Scheme

  2. The registry key HKEY_CURRENT_USER\Control Panel\Cursors contains the user defined cursor schemes (i.e. Scheme Source = 1)

  3. The registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Schemes contains the system cursor schemes (i.e. Scheme Source = 2)

If you already changed the path to one of the cursor type in HKCU\Control Panel\Cursors and realized that it did not do anything. You are correct, just updating a key – HKCU\Control Panel\Cursors\Arrow, for instance – isn’t enough. You have to tell windows to load the new cursor.

This is where the SystemParametersInfo call comes in. To try this out let’s go ahead and change HKCU\Control Panel\Cursors\Arrow to C:\WINDOWS\Cursors\appstar3.ani (assuming you have this icon) and then make a call to SystemParametersInfo.

In AutoHotKey Script:

SPI_SETCURSORS := 0x57
result := DllCall("SystemParametersInfo", "UInt", SPI_SETCURSORS, "UInt", 0, "UInt", 0, "UInt", '0')
MsgBox Error Level: %ErrorLevel% `nLast error: %A_LastError%`nresult: %result%

Translated to C#:

[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);

const int SPI_SETCURSORS = 0x0057;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDCHANGE = 0x02;
SystemParametersInfo(SPI_SETCURSORS, 0, 0, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);

Changing to the Default Windows Cursor

Now the tricky part. If you look at HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Schemes you will notice that “Windows Default” is defined as “,,,,,,,,,,,,,” or in other words no pointers to actual cursors!

What to do now? Don’t worry. All you have to do is set the different cursor types to empty string and then make the SystemParametersInfo call as usual. In fact, you can set any of the cursor type to empty string in any scheme and Windows will default it to it’s equivalent in the “Windows Default” scheme.

REF:

https://thebitguru.com/articles/programmatically-changing-windows-mouse-cursors/3

https://social.msdn.microsoft.com/Forums/vstudio/en-US/977e2f40-3222-4e13-90ea-4e8d0cdf289c/faq-item-how-to-change-the-systems-cursor-using-visual-cnet?forum=csharpgeneral




回答2:


You can do like this. Get the Cursor.cur file to load custom cursor. On MouseLeave set the Default cursor for form.

public static Cursor ActuallyLoadCursor(String path)
    {
        return new Cursor(LoadCursorFromFile(path));
    }

    [DllImport("user32.dll")]
    private static extern IntPtr LoadCursorFromFile(string fileName);

Button btn = new Button();
btn.MouseLeave += Btn_MouseLeave;
btn.Cursor = ActuallyLoadCursor("Cursor.cur");

private static void Btn_MouseLeave(object sender, EventArgs e)
    {
        this.Cursor = Cursors.Default;
    }



回答3:


using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1 {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
      Bitmap bmp = Properties.Resources.Image1;
      bmp.MakeTransparent(Color.White);
      IntPtr hIcon = bmp.GetHicon();
      Icon ico = Icon.FromHandle(hIcon);
      Cursor cur = new Cursor(hIcon);
      using (FileStream fs = new FileStream(@"c:\temp\test.cur", FileMode.Create, FileAccess.Write))
        ico.Save(fs);
      cur.Dispose();
      ico.Dispose();
      DestroyIcon(hIcon);

      // Test it
      cur = new Cursor(@"c:\temp\test.cur");
      this.Cursor = cur;
    }
    [DllImport("user32.dll")]
    extern static bool DestroyIcon(IntPtr handle);
  }
}

REF: https://social.msdn.microsoft.com/Forums/windows/en-US/9ea0bf74-760f-4f40-b64c-0cf7b0a56939/save-custom-cursor?forum=winforms



来源:https://stackoverflow.com/questions/41713827/programmatically-change-custom-mouse-cursor-in-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!