Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Change Mouse Cursor (Specific)

Description
This script will change the mouse cursor set to the one specified in the "cursor" string variable at the top of the script.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
May 13, 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)
	{
		//set the name of the mouse cursor set here, if you want to view a list, download the "Change Mouse Cursor (List)" script and run it
		//on most systems, the options are: "Magnified", "Windows Aero", "Windows Aero XL)", "Windows Black", "Windows Black (extra large)", "Windows Black (large)", "Windows Inverted", "Windows Inverted (extra large)", "Windows Inverted (large)", "Windows Standard (extra large)", "Windows Standard (large)"
		string cursor = "Windows Standard (extra large)";
		
		//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);
	}
}