Getting the cursor size on Windows

By Jeison Cardoso in Windows

October 10, 2023

Getting the cursor size in Windows is a trivial task, but it can be a bit confusing.

Windows API does not provide a direct function to get the current size of the mouse cursor as it is configured in the system settings and it is not easily accessible via code.

The cursor size is defined in the Windows registry, under the key HKEY_CURRENT_USER\Control Panel\Cursors.

#include <Windows.h>

int GetCursorSize() {
    // Open the registry key for cursor settings
    HKEY hKey;
    if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Control Panel\\Cursors", 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
        // Error handling, if necessary
        return -1;
    }

    DWORD cursorSizeSetting;
    DWORD size = sizeof(cursorSizeSetting);

    // Get the DWORD value "CursorBaseSize" from the registry
    if (RegQueryValueEx(hKey, L"CursorBaseSize", nullptr, nullptr, reinterpret_cast<BYTE*>(&cursorSizeSetting), &size) != ERROR_SUCCESS) {
        // Error handling, if necessary
        RegCloseKey(hKey);
        return -1;
    }

    // Close the registry key
    RegCloseKey(hKey);

    // Check if the value was found
    if (size != sizeof(cursorSizeSetting)) {
        // Value not found or incorrect, handle error, if necessary
        return -1;
    }

    // Use the registry value to calculate the cursor size
    const int baseSize = GetSystemMetrics(SM_CXCURSOR) / 2;

    if (cursorSizeSetting < 32 || cursorSizeSetting > 256) {
        // The registry value is out of the allowed range, handle the error, if necessary
        return -1;
    }

    int cursorSizeValue = (cursorSizeSetting - 2 * baseSize) / baseSize;
    const int cursorSize = baseSize + cursorSizeValue * (baseSize / 2);

    return cursorSize;
}
Error handling was omitted to simplify the code.

In addition, the cursor size is defined in pixels, but the cursor size is defined in points. To get the cursor size in pixels, it is necessary to get the cursor size in points and multiply it by the monitor’s DPI.

Finally, to get the monitor’s DPI, use the GetDpiForSystem function:

int GetDpiForSystem() {
    // Get the monitor's DPI
    HDC hdc = GetDC(nullptr);
    const int dpi = GetDeviceCaps(hdc, LOGPIXELSX);
    ReleaseDC(nullptr, hdc);

    return dpi;
}

The resulting factor as a floating-point value is the cursor size in points. To get the cursor size in pixels, multiply the cursor size in points by the monitor’s DPI.

int GetCursorSizeInPixels() {
    // Get the cursor size in points
    const int cursorSize = GetCursorSize();

    // Get the monitor's DPI see below
    const int dpi = GetDpiForSystem();

    // Calculate the cursor size in pixels
    const int cursorSizeInPixels = cursorSize * dpi / 72;

    return cursorSizeInPixels;
}

The third line of the code calculates the size of the cursor in pixels by multiplying the cursor size in points by the DPI and dividing the result by 72. The value 72 is used because there are 72 points in an inch. This calculation converts the cursor size from points to pixels.

If it doesn’t work, contact me.

Note: This post was originally published in Portuguese.

Posted on:
October 10, 2023
Length:
3 minute read, 455 words
Categories:
Windows
Tags:
cpp
See Also:
Obtendo o tamanho do cursor no Windows (pt-BR)
Boost on Visual Studio 2019
Build libvolk on windows