CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    [2005] How do I work with the cursor?

    Q: How do I change the cursor on the Form in Design Time ?

    A: In Design Time, you can set the Cursor property of the Form, this will change the cursor for the control on the form as well

    Q: How do I change the cursor for one control on the form only, in Design time?

    A: Simply set the control's Cursor Porperty to the cursor you want

    Q: How do I change the cursor during Runtime?

    A: For the form, you can do the following :
    [code]Me.Cursor = Cursors.AppStarting 'Change cursor to Appstarting ( Arrow and Hourglass )

    For the particular control, you'd need to specify the name of the control, then use the Cursor property :
    Code:
    Button1.Cursor = AppStarting ' Set cursor for Button1
    Q: What Cursors are there?

    A: Have a look at the following list for the available Cursors and their descriptions :
    • AppStarting Gets the cursor that appears when an application starts.
    • Arrow Gets the arrow cursor.
    • Cross Gets the crosshair cursor.
    • Default Gets the default cursor, which is usually an arrow cursor.
    • Hand Gets the hand cursor, typically used when hovering over a Web link.
    • Help Gets the Help cursor, which is a combination of an arrow and a question mark.
    • HSplit Gets the cursor that appears when the mouse is positioned over a horizontal splitter bar.
    • IBeam Gets the I-beam cursor, which is used to show where the text cursor appears when the mouse is clicked.
    • No Gets the cursor that indicates that a particular region is invalid for the current operation.
    • NoMove2D Gets the cursor that appears during wheel operations when the mouse is not moving, but the window can be scrolled in both a horizontal and vertical direction.
    • NoMoveHoriz Gets the cursor that appears during wheel operations when the mouse is not moving, but the window can be scrolled in a horizontal direction.
    • NoMoveVert Gets the cursor that appears during wheel operations when the mouse is not moving, but the window can be scrolled in a vertical direction.
    • PanEast Gets the cursor that appears during wheel operations when the mouse is moving and the window is scrolling horizontally to the right.
    • PanNE Gets the cursor that appears during wheel operations when the mouse is moving and the window is scrolling horizontally and vertically upward and to the right.
    • PanNorth Gets the cursor that appears during wheel operations when the mouse is moving and the window is scrolling vertically in an upward direction.
    • PanNW Gets the cursor that appears during wheel operations when the mouse is moving and the window is scrolling horizontally and vertically upward and to the left.
    • PanSE Gets the cursor that appears during wheel operations when the mouse is moving and the window is scrolling horizontally and vertically downward and to the right.
    • PanSouth Gets the cursor that appears during wheel operations when the mouse is moving and the window is scrolling vertically in a downward direction.
    • PanSW Gets the cursor that appears during wheel operations when the mouse is moving and the window is scrolling horizontally and vertically downward and to the left.
    • PanWest Gets the cursor that appears during wheel operations when the mouse is moving and the window is scrolling horizontally to the left.
    • SizeAll Gets the four-headed sizing cursor, which consists of four joined arrows that point north, south, east, and west.
    • SizeNESW Gets the two-headed diagonal (northeast/southwest) sizing cursor.
    • SizeNS Gets the two-headed vertical (north/south) sizing cursor.
    • SizeNWSE Gets the two-headed diagonal (northwest/southeast) sizing cursor.
    • SizeWE Gets the two-headed horizontal (west/east) sizing cursor.
    • UpArrow Gets the up arrow cursor, typically used to identify an insertion point.
    • VSplit Gets the cursor that appears when the mouse is positioned over a vertical splitter bar.
    • WaitCursor Gets the wait cursor, typically an hourglass shape.


    Q: How do I create a Non-Standard cursor, a cursor with a Picture?

    A: To create a cursor, from a sepaarate cursor file ( .cur), you could do the following :
    Code:
            Me.Cursor = New Cursor("Bullseye.cur") 'Load Cursor From File
    The above code assumes you have your own cursor named Bullseye.cur

    Q: How do I create an Animated Cursor?

    A: To Add an Animated cursor to your Form, follwo these steps :
    • Because we are going to need the LoadCursorFromFile API, we will need to use the following Import, in order to communicate with the API properly. Add the following to the top of your Form's Class :
      Code:
      Imports System.Runtime.InteropServices 'APIs
    • Then, add the LoadCursorFromFile API in ( Declarations ) :
      Code:
            _
          Private Shared Function LoadCursorFromFile(ByVal filename As String) As IntPtr
          End Function
    • Thirdly, create a function to load & change our cursor :
      Code:
          Private Shared Function CreateCursor(ByVal filename As String) As Cursor
              Dim hCursor As IntPtr 'Handle To The Cursor
              Dim result As Cursor = Nothing 'Returned Cursor
      
              Try
                  hCursor = LoadCursorFromFile(filename) 'Load
                  If Not IntPtr.Zero.Equals(hCursor) Then 'If Exists
                      result = New Cursor(hCursor) 'Obtain New Cursor
                  Else
                      'Could Not Create Cursor
                      Throw New ApplicationException("Could not create cursor from file " & filename)
                  End If
              Catch ex As Exception
                  'Log Your Exception
              End Try
      
              Return result 'Return The Cursor
          End Function
    • Finally, call the function when you want to change the Cursor :
      Code:
              Me.Cursor = CreateCursor(Application.StartupPath & "\dinosaur.ani")
      The above code creates the animated cursor from the dinosaur.ani file, and it assumes it is located at the same place where your application's exe is


    Q: How do I change the cursor's position on the screen?

    A: You just need to set the Position property of the cursor to a new X and Y coordinate ( A New Point ) :
    Code:
            System.Windows.Forms.Cursor.Position = New Point(500, 500) 'Set New Position
    Q: Say for example, I need to load a combobox with all the avaliable Cursor names on the system, how do I do that?

    A: Firstly, you'll need the System.Reflection namespace :
    Code:
    'The System.Reflection Namespace Contains Classes And Interfaces That 
    'Provide A Managed View Of Loaded Types, Methods, And Fields, With The 
    'Ability To Dynamically Create And Invoke Types.
    Imports System.Reflection
    Then, in Form_Load, you can add the following :
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'Load System Cursors From System Using Reflection
            For Each p As PropertyInfo In GetType(Cursors).GetProperties
                If (p.PropertyType Is GetType(Cursor)) Then
                    cboCursors.Items.Add("Cursors." & p.Name)
                End If
            Next
        End Sub
    Q: Can I change the speed of the cursor, when blinking?

    A: Yes, you can. You can use the SetCaretBlinkTime API. In the included sample this is demonstarted. You can enter a new value into the Textbox, then the Cursor's blinking rate will be changed.

    Q: Can I hide the cursor on the Form?

    A: Yes, you can hide the cursor on the form, by useing the ShowCursor API :
    Code:
         _
            Private Shared Function ShowCursor(ByVal bShow As Int32) As Int32
        End Function 'Show Cursor
    Once tyhe API is in place, you could call it like :
    Code:
            ShowCursor(False) 'Hide Cursor On Form
    Just remember, to call this API, specifying True, when you want to display the cursor again.

    Q: Can I hide the cursor Entirely, meaning in all applications?

    A: Yes, you can. You see, the ShowCursor API will only hide the cursor on the form only. What you must do is to use the Clip property of the cursor. This will allow you to set the bounds of the cursor, meaning, we set up a specified area for the cursor to be shown. What I've done in the sample project is to set the cursors boundries to the dimensions of the form ( constraining it to the form only ), then Move the curosr to a certain area on the form. You must just remember to restore the cursor's dimensions again
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured