Checking mouse cursor shape or state (WPF)

Last edited on

Overview

In WPF applications, the cursor is set for each FrameworkElement via the System.Windows.FrameworkElement.Cursor property, which either yields null if no custom cursor was set or an instance of System.Windows.Input.Cursor .

Unfortunately, System.Windows.Input.Cursor has no "getter" to get the shape of the cursor - which makes sense, given that you can have cursors with arbitrary bitmaps. However, a workaround is to use the ToString() method (note the capital T): In case the cursor is one of the standard-ones defined in the Cursors class (see System.Windows.Input.Cursors ) the ToString() representation will yield one of the values in the "Name" column of the table in the link above, e.g. "Cross" or "Pen".

So, to test the cursor for a standard shape something like this can be used:

function main()
{
    // ...
    var obj = waitForObject("...");
    var cursor = obj.nativeObject.Cursor;
    if (isNull(cursor)) {
        test.fail("Cursor on WPF object is not a Cross cursor");
    } else {
        test.compare(cursor.ToString(), "Cross");
    }
}
test.js