Programmatically change user mouse cursor in Windows?

I am trying to change the Windows cursors (the default is Windows Custom Scheme) to my own cursors (called Cut the Rope):

enter image description here

Is there an idea to replace all cursors (Arrow, Busy, Help selection, Link selection, ...) with My Cut the rope?

+6
source share
3 answers

If you want to change the default mouse cursor "Mouse Cursor" theme:

enter image description here

You can simply change it in the registry:

The game includes three main registry keys.

  • HKEY_CURRENT_USER registry key \ Control Panel \ Cursors contain active user cursors

    1a) Values โ€‹โ€‹below this are different types of cursors
    1b) Source Scheme Source indicates the type of cursor scheme currently in use.

    Different meanings:

    "0" - Windows Default
    "1" - User Scheme
    "2" - System diagram

  • HKEY_CURRENT_USER registry key \ Control Panel \ Cursors contains user-defined cursor schemes (i.e. source of the scheme = 1)

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

enter image description here

If you have already changed the path to one of the cursor types in HKCU \ Control Panel \ Cursors and realized that you did not do anything. You are right, just updating the key - HKCU \ Control Panel \ Cursors \ Arrow, for example - is not enough. You must point the windows to load the new cursor.

This is where SystemParametersInfo is called. To try this, you can continue and change HKCU \ Control Panel \ Cursors \ Arrow to C: \ WINDOWS \ Cursors \ appstar3.ani (assuming you have this icon), and then call 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); 

Going 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 real cursors!

So what's now? Do not worry. All you have to do is set up different types of cursors for empty lines, and then make the call to SystemParametersInfo normal. In fact, you can set any type of cursor to an empty string in any scheme, and Windows will use its equivalent in the default Windows scheme by default.

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

+3
source

You can do it. Get the Cursor.cur file to load the custom cursor. On MouseLeave set the default cursor for the 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
source
 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

+1
source

Source: https://habr.com/ru/post/1262977/


All Articles