Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Change Mouse Cursor (List)

Description
This script displays the mouse cursors available on your machine, and allows you to choose one via a drop-down list.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
May 12, 2015
Date Last Modified
May 13, 2015

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using Microsoft.Win32;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Window Location target when run by a Window Location rule
//   - TitleBar Button owner when run by a TitleBar Button
//   - Jump List owner when run from a Taskbar Jump List
//   - Currently focused window if none of these match
public static class DisplayFusionFunction
{
	//this function lets us tell windows that we changed the cursor
	[DllImport("user32.dll", SetLastError = true)]
	[return: MarshalAs(UnmanagedType.Bool)]
	static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
	const uint SPI_SETCURSORS = 0x0057;
	
	public static void Run(IntPtr windowHandle)
	{
		//this will hold the cursors that the user has installed
		string[] cursors;
		
		//open the registry to get the cursors the user has installed
		using (RegistryKey regkey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Cursors\Schemes", RegistryKeyPermissionCheck.ReadSubTree))
		{
			//if we couldn't open the registry, tell the user and exit
			if (regkey == null)
			{
				BFS.Dialog.ShowMessageError("Unable to get cursors");
				return;
			}

			//convert the value into a list
			cursors = regkey.GetValueNames();
		}
		
		//sort the cursors to the user can read them better
		Array.Sort(cursors);
		
		//let the user pick which cursor they want
		string cursor = BFS.Dialog.GetUserInputList("Select Mouse Cursor", cursors);
		
		//if they pressed cancel, exit
		if(string.IsNullOrEmpty(cursor))
			return;
					
		//this will hold the paths to the cursor files (.cur)
		string[] cursorPaths;
		
		//open the selected registry entry and get the paths
		using(RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Cursors\Schemes", RegistryKeyPermissionCheck.ReadSubTree))
		{
			//if we couldn't open the registry, tell the user and exit
			if (regKey == null)
			{
				BFS.Dialog.ShowMessageError("Unable to get cursor paths");
				return;
			}
			
			//read the registry path
			object value = regKey.GetValue(cursor, "");
			
			//the paths are seperated by commas, split up the string
			cursorPaths = value.ToString().Split(new char[]{','});
		}
		
		//this is the order than the paths appear in the comma seperated path
		string[] pathKeys = new string[]{"Arrow", "Help", "AppStarting", "Wait", "Crosshair", "IBeam", "NWPen", "No", "SizeNS", "SizeWE", "SizeNWSE", "SizeNESW", "SizeAll", "UpArrow", "Hand", ""};
		
		//if we don't have enough paths, tell the user and exit
		if(cursorPaths.Length < pathKeys.Length)
		{
			BFS.Dialog.ShowMessageError("Not enough paths");
			return;
		}
		
		//open the registry and set the used cursor values to the new ones we found
		using(RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"Control Panel\Cursors", RegistryKeyPermissionCheck.ReadWriteSubTree))
		{
			//if we couldn't open the registry, tell the user and exit
			if (regKey == null)
			{
				BFS.Dialog.ShowMessageError("Unable set cursors");
				return;
			}
			
			//set each path
			for(int i = 0; i < pathKeys.Length; i++)
				regKey.SetValue(pathKeys[i], cursorPaths[i], RegistryValueKind.String);
		}
		
		//tell windows we changed the cursor so it gets updated
		SystemParametersInfo(SPI_SETCURSORS, 0, IntPtr.Zero, 0);
	}
}