14.2.4 Class Cursor

The Cursor class implements custom mouse pointers. Please note that a mouse pointer is always bound to a specific component. The mouse pointer is the image that indicates the position of the mouse on the screen. For example, if you want to use your own mouse pointer, you must first specify that you want to replace the mouse pointer for a particular component with a user-defined mouse pointer. A new cursor object is then created, to which a small image is assigned as the appropriate mouse pointer. As an option, the coordinates of the mouse tip can also be specified if necessary.

You can select appropriate mouse pointers from these sources:

In the presented project 'Cursor' special mouse pointers are used for selected components, which originate from the three sources mentioned above. You will also learn how to use the mouse to draw in a DrawArea while holding down the left mouse button.

Cursor - Pencil
Figure 14.2.4.1: User-defined mouse pointer - drawing pencil - in the 'Cursor' project

The source text is completely specified and then commented on:
[1] ' Gambas class file
[2]
[3] Public bStart As Boolean
[4]
[5] Public Sub Form_Open()
[6]
[7]   FMain.Center
[8]   FMain.Resizable = False
[9]   DrawingArea1.Cached = True
[10]   txtMouseX.Mouse = Mouse.Blank ' TextBox without mouse pointer
[11]   txtMouseY.Mouse = Mouse.Blank
[12]   btnClose.Mouse = Mouse.Pointing ' Mouse constant (hand)
[13]
[14]   btnClear.Mouse = Mouse.Custom
[15]   btnClear.Cursor = New Cursor(Picture["Mouse pointer/erase.png"])
[16]
[17] ' Standard mouse tip is the upper left corner of the mouse image
[18] ' With x=0 and y=15 the mouse tip lies on the pencil lead
[19]   DrawingArea1.Mouse = Mouse.Custom
[20]   DrawingArea1.Cursor = New Cursor(Picture["icon:/16/pen"], 0, 15)
[21]
[22]   Draw.Begin(DrawingArea1)
[23]     Draw.Foreground = Color.Red
[24]     Draw.Font.Size = 30
[25]     Draw.Font.Bold = True
[26]   ' Text centered on the drawing sheet in the specified rectangle
[27]     Draw.Text("Are you afraid of mice?", 0, 0, DrawingArea1.ClientW, DrawingArea1.ClientH, Align.Center)
[28]   Draw.End
[29]   bStart = True
[30]
[31] End ' Form_Open()
[32]
[33] Public Sub DrawingArea1_MouseMove()
[34]
[35]   If bStart = True Then
[36]      Wait 0.2
[37]      bStart = False
[38]      DrawingArea1.Clear
[39]   Endif '  bStart = True?
[40]   txtMouseX.Text = Mouse.X
[41]   txtMouseY.Text = Mouse.Y
[42]   If Mouse.Left = True Then
[43]     Draw.Begin(DrawingArea1)
[44]     If Mouse.X>5 And Mouse.X<DrawingArea1.Width-5 And Mouse.Y>5 And Mouse.Y<DrawingArea1.Height-5 Then
[45]         Draw.Point(Mouse.X, Mouse.Y)
[46]     Endif
[47]     Draw.End
[48]    Endif ' Mouse.Left = True?
[49] End ' DrawingArea1_MouseMove()
[50]
[51] Public Sub btnClear_Click()
[52]
[53]   DrawingArea1.Clear
[54]   txtMouseX.Text = "0"
[55]   txtMouseY.Text = "0"
[56]
[57] End ' btnClear_Click()
[58]
[59] Public Sub btnClose_Click()
[60]   FMain.Close
[61] End ' btnClose_Click()

Comments:

Bild-Editor IDE
Figure 14.2.4.2: Mouse pointer in the image editor

In the project folder you will find custom mouse pointers that you can use for your own projects.

Download