| Version: 3.3.1

Public Member Functions | List of all members
wxCursor Class Reference
Class List by Category » Graphics Device Interface (GDI)

`#include <wx/cursor.h>`

Inheritance diagram for wxCursor:
[legend]

## Detailed Description

A cursor is a small bitmap used for denoting where the mouse pointer is, with a picture that indicates the point of a mouse click.

A cursor may be associated either with the given window (and all its children, unless any of them defines its own cursor) using wxWindow::SetCursor(), or set globally using wxSetCursor(). It is also common to temporarily change the cursor to a "busy cursor" indicating that some lengthy operation is in progress and wxBusyCursor can be used for this.

Because a custom cursor of a fixed size would look either inappropriately big in standard resolution or too small in high resolution, wxCursorBundle class allows to define a set of cursors of different sizes, letting wxWidgets to automatically select the most appropriate one for the current resolution and user's preferred cursor size. Using this class with wxWindow::SetCursorBundle() is the recommended way to use custom cursors.

# Creating a Custom Cursor

The following is an example of creating a cursor from 32x32 bitmap data (down_bits) and a mask (down_mask) where 1 is black and 0 is white for the bits, and 1 is opaque and 0 is transparent for the mask. It works on Windows and GTK+.

static char down_bits[] = { 255, 255, 255, 255, 31,
255, 255, 255, 31, 255, 255, 255, 31, 255, 255, 255,
31, 255, 255, 255, 31, 255, 255, 255, 31, 255, 255,
255, 31, 255, 255, 255, 31, 255, 255, 255, 25, 243,
255, 255, 19, 249, 255, 255, 7, 252, 255, 255, 15, 254,
255, 255, 31, 255, 255, 255, 191, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255 };
static char down_mask[] = { 240, 1, 0, 0, 240, 1,
0, 0, 240, 1, 0, 0, 240, 1, 0, 0, 240, 1, 0, 0, 240, 1,
0, 0, 240, 1, 0, 0, 240, 1, 0, 0, 255, 31, 0, 0, 255,
31, 0, 0, 254, 15, 0, 0, 252, 7, 0, 0, 248, 3, 0, 0,
240, 1, 0, 0, 224, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0 };
#ifdef __WXMSW__
wxBitmap down_bitmap(down_bits, 32, 32);
wxBitmap down_mask_bitmap(down_mask, 32, 32);
down_bitmap.SetMask(new wxMask(down_mask_bitmap));
wxImage down_image = down_bitmap.ConvertToImage();
down_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 6);
down_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 14);
wxCursor down_cursor = wxCursor(down_image);
#elif defined(__WXGTK__)
wxCursor down_cursor = wxCursor(down_bits, 32, 32, 6, 14,
down_mask, wxWHITE, wxBLACK);
#endif
wxBitmap
This class encapsulates the concept of a platform-dependent bitmap, either monochrome or colour or co...
Definition: bitmap.h:212
wxCursor
A cursor is a small bitmap used for denoting where the mouse pointer is, with a picture that indicate...
Definition: cursor.h:87
wxCursor::wxCursor
wxCursor()
Default constructor.
wxImage
This class encapsulates a platform-independent image.
Definition: image.h:526
wxImage::SetOption
void SetOption(const wxString &name, const wxString &value)
Sets a user-defined option.
wxMask
This class encapsulates a monochrome mask bitmap, where the masked area is black and the unmasked are...
Definition: bitmap.h:1071
wxBLACK
wxColour * wxBLACK
Definition: colour.h:385
wxWHITE
wxColour * wxWHITE
Definition: colour.h:392
wxIMAGE_OPTION_CUR_HOTSPOT_X
#define wxIMAGE_OPTION_CUR_HOTSPOT_X
Definition: image.h:136
wxIMAGE_OPTION_CUR_HOTSPOT_Y
#define wxIMAGE_OPTION_CUR_HOTSPOT_Y
Definition: image.h:137

##

Library: wxCore
Category: Graphics Device Interface (GDI)

Predefined objects/pointers:

- wxNullCursor
- wxSTANDARD_CURSOR
- wxHOURGLASS_CURSOR
- wxCROSS_CURSOR

See also
  wxBitmap, wxIcon, wxWindow::SetCursor(), wxSetCursor(), wxStockCursor

## Public Member Functions
| wxCursor ()
| Default constructor. More...
| wxCursor (const wxBitmap &bitmap, int hotSpotX=0, int hotSpotY=0)
| Constructs a cursor from the provided bitmap and hotspot position. More...
| wxCursor (const wxBitmap &bitmap, const wxPoint &hotSpot)
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
| wxCursor (const wxString &cursorName, wxBitmapType type=wxCURSOR_DEFAULT_TYPE, int hotSpotX=0, int hotSpotY=0)
| Constructs a cursor by passing a string resource name or filename. More...
| wxCursor (const wxString &name, wxBitmapType type, const wxPoint &hotSpot)
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
| wxCursor (wxStockCursor cursorId)
| Constructs a cursor using a cursor identifier. More...
| wxCursor (const wxImage &image)
| Constructs a cursor from a wxImage. More...
| wxCursor (const char *const *xpmData)
| Constructs a cursor from XPM data. More...
| wxCursor (const char bits[], int width, int height, int hotSpotX=-1, int hotSpotY=-1, const char maskBits[]=nullptr, const wxColour *fg=nullptr, const wxColour *bg=nullptr)
| wxGTK-specific constructor from data in XBM format. More...
| wxCursor (const wxCursor &cursor)
| Copy constructor, uses reference counting. More...
virtual bool | IsOk () const
| Returns true if cursor data is present. More...
wxPoint | GetHotSpot () const
| Returns the coordinates of the cursor hot spot. More...
wxCursor & | operator= (const wxCursor &cursor)
| Assignment operator, using reference counting. More...
Public Member Functions inherited from wxGDIObject
| wxGDIObject ()
| Default constructor. More...
Public Member Functions inherited from wxObject
| wxObject ()
| Default ctor; initializes to nullptr the internal reference data. More...
| wxObject (const wxObject &other)
| Copy ctor. More...
virtual | ~wxObject ()
| Destructor. More...
virtual wxClassInfo * | GetClassInfo () const
| This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar). More...
wxObjectRefData * | GetRefData () const
| Returns the wxObject::m_refData pointer, i.e. the data referenced by this object. More...
bool | IsKindOf (const wxClassInfo *info) const
| Determines whether this class is a subclass of (or the same class as) the given class. More...
bool | IsSameAs (const wxObject &obj) const
| Returns true if this object has the same data pointer as obj. More...
void | Ref (const wxObject &clone)
| Makes this object refer to the data in clone. More...
void | SetRefData (wxObjectRefData *data)
| Sets the wxObject::m_refData pointer. More...
void | UnRef ()
| Decrements the reference count in the associated data, and if it is zero, deletes the data. More...
void | UnShare ()
| This is the same of AllocExclusive() but this method is public. More...
void | operator delete (void *buf)
| The delete operator is defined for debugging versions of the library only, when the identifier `__WXDEBUG__` is defined. More...
void * | operator new (size_t size, const wxString &filename=nullptr, int lineNum=0)
| The new operator is defined for debugging versions of the library only, when the identifier `__WXDEBUG__` is defined. More...

## Additional Inherited Members
Protected Member Functions inherited from wxObject
void | AllocExclusive ()
| Ensure that this object's data is not shared with any other object. More...
virtual wxObjectRefData * | CreateRefData () const
| Creates a new instance of the wxObjectRefData-derived class specific to this object and returns it. More...
virtual wxObjectRefData * | CloneRefData (const wxObjectRefData *data) const
| Creates a new instance of the wxObjectRefData-derived class specific to this object and initializes it copying data. More...
Protected Attributes inherited from wxObject
wxObjectRefData * | m_refData
| Pointer to an object which is the object's reference-counted data. More...

## Constructor & Destructor Documentation

## ◆ wxCursor() [1/10]

wxCursor::wxCursor | ( |  | ) |

Default constructor.

## ◆ wxCursor() [2/10]

wxCursor::wxCursor | ( | const wxBitmap & | bitmap,
|  | int | hotSpotX = `0`,
|  | int | hotSpotY = `0`
| ) |  |

Constructs a cursor from the provided bitmap and hotspot position.

Parameters

  bitmap | The bitmap to use for the cursor, should be valid.
  hotSpotX | Hotspot x coordinate (relative to the top left of the image).
  hotSpotY | Hotspot y coordinate (relative to the top left of the image).

Since
  3.3.0

## ◆ wxCursor() [3/10]

wxCursor::wxCursor | ( | const wxBitmap & | bitmap,
|  | const wxPoint & | hotSpot
| ) |  |

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
  3.3.0

## ◆ wxCursor() [4/10]

wxCursor::wxCursor | ( | const wxString & | cursorName,
|  | wxBitmapType | type = `wxCURSOR_DEFAULT_TYPE`,
|  | int | hotSpotX = `0`,
|  | int | hotSpotY = `0`
| ) |  |

Constructs a cursor by passing a string resource name or filename.

The arguments hotSpotX and hotSpotY are only used when there's no hotspot info in the resource/image-file to load (e.g. when using `wxBITMAP_TYPE_ICO` under wxMSW or `wxBITMAP_TYPE_XPM` under wxGTK).

Parameters

  cursorName | The name of the resource or the image file to load.
  type | Icon type to load. It defaults to `wxCURSOR_DEFAULT_TYPE`, which is a #define associated to different values on different platforms: under Windows, it defaults to `wxBITMAP_TYPE_CUR_RESOURCE`. Other permitted types under Windows are `wxBITMAP_TYPE_CUR` (to load a cursor from a .cur cursor file), `wxBITMAP_TYPE_ICO` (to load a cursor from a .ico icon file) and `wxBITMAP_TYPE_ANI` (to load a cursor from a .ani icon file). under MacOS, it defaults to `wxBITMAP_TYPE_MACCURSOR_RESOURCE`; when specifying a string resource name, first a PNG and then a CUR image is searched in resources. under GTK, it defaults to `wxBITMAP_TYPE_XPM`. See the wxCursor(const wxImage& image) ctor for more info. under X11, it defaults to `wxBITMAP_TYPE_XPM`.
  hotSpotX | Hotspot x coordinate (relative to the top left of the image).
  hotSpotY | Hotspot y coordinate (relative to the top left of the image).

## ◆ wxCursor() [5/10]

wxCursor::wxCursor | ( | const wxString & | name,
|  | wxBitmapType | type,
|  | const wxPoint & | hotSpot
| ) |  |

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
  3.3.0

## ◆ wxCursor() [6/10]

wxCursor::wxCursor | ( | wxStockCursor | cursorId | ) |

Constructs a cursor using a cursor identifier.

Parameters

  cursorId | A stock cursor identifier. See wxStockCursor.

## ◆ wxCursor() [7/10]

wxCursor::wxCursor | ( | const wxImage & | image | ) |

Constructs a cursor from a wxImage.

If cursor are monochrome on the current platform, colors with the RGB elements all greater than 127 will be foreground, colors less than this background. The mask (if any) will be used to specify the transparent area.

In wxMSW the foreground will be white and the background black. If the cursor is larger than 32x32 it is resized.

In wxGTK, colour cursors and alpha channel are supported (starting from GTK+ 2.2). Otherwise the two most frequent colors will be used for foreground and background. In any case, the cursor will be displayed at the size of the image.

Under wxMac (Cocoa), large cursors are supported.

Notice that the image can define the cursor hot spot. To set it you need to use wxImage::SetOption() with `wxIMAGE_OPTION_CUR_HOTSPOT_X` or `wxIMAGE_OPTION_CUR_HOTSPOT_Y`, e.g.

image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotY);

## ◆ wxCursor() [8/10]

wxCursor::wxCursor | ( | const char *const * | xpmData | ) |

Constructs a cursor from XPM data.

In versions of wxWidgets until 3.1.6 constructing wxCursor from XPM data implicitly used wxImage constructor from XPM data and wxCursor constructor from wxImage. Since 3.1.6 this constructor overload is available to allow constructing wxCursor from XPM to still work, even though wxImage constructor from XPM is now `explicit`.

## ◆ wxCursor() [9/10]

wxCursor::wxCursor | ( | const char | bits[],
|  | int | width,
|  | int | height,
|  | int | hotSpotX = `-1`,
|  | int | hotSpotY = `-1`,
|  | const char | maskBits[] = `nullptr`,
|  | const wxColour * | fg = `nullptr`,
|  | const wxColour * | bg = `nullptr`
| ) |  |

wxGTK-specific constructor from data in XBM format.

The parameters fg and bg have an effect only on GTK+, and force the cursor to use particular background and foreground colours.

Parameters

  bits | An array of XBM data bits.
  width | Cursor width.
  height | Cursor height.
  hotSpotX | Hotspot x coordinate (relative to the top left of the image).
  hotSpotY | Hotspot y coordinate (relative to the top left of the image).
  maskBits | Bits for a mask bitmap.
  fg | Foreground colour.
  bg | Background colour.

Availability: only available for the wxGTK port.

wxPerl Note: In wxPerl use Wx::Cursor->newData(bits, width, height, hotSpotX = -1, hotSpotY = -1, maskBits = 0).

## ◆ wxCursor() [10/10]

wxCursor::wxCursor | ( | const wxCursor & | cursor | ) |

Copy constructor, uses reference counting.

Parameters

  cursor | Pointer or reference to a cursor to copy.

## Member Function Documentation

## ◆ GetHotSpot()

wxPoint wxCursor::GetHotSpot | ( |  | ) | const

Returns the coordinates of the cursor hot spot.

The hot spot is the point at which the mouse is actually considered to be when this cursor is used.

This method is currently implemented in wxMSW, wxGTK and wxOSX (since wxWidgets 3.3.0) and returns wxDefaultPosition in the other ports.

Since
  3.1.0

## ◆ IsOk()

virtual bool wxCursor::IsOk | ( |  | ) | const
| virtual

Returns true if cursor data is present.

## ◆ operator=()

wxCursor& wxCursor::operator= | ( | const wxCursor & | cursor | ) |

Assignment operator, using reference counting.