[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

How to generate a cursor for a mousepointer in runtime

Hi,

I would like to generate a mousecursor in runtime.  (I don't have the .cur -file: it has to be created in runtime)
0
dekeyzer
Asked:
dekeyzer
  • 4
  • 3
  • 3
2 Solutions
 
planoczCommented:
do you want a mouse cursor on all of the form or per control or want?
I need just a little more info.
thanks
0
 
dekeyzerAuthor Commented:
all of the form.
0
 
planoczCommented:
Hi dekeyzer,

Here is an example that I got from Microsoft.
If you do a search on the net you can find free cur and ani files to use.
that is the only way that i know of using  extern curs.

Imports System.IO
Namespace Microsoft.Samples
    Public Class ctlCursors
        Inherits System.Windows.Forms.Form

        Private Const cursorColored As String = "harrow.cur"
        Private Const cursorAnimated As String = "dinosaur.ani"
#Region " Windows Form Designer generated code "
        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
        End Sub
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents DefaultOption As System.Windows.Forms.RadioButton
        Friend WithEvents CursorOption As System.Windows.Forms.RadioButton
        Friend WithEvents LoadOption As System.Windows.Forms.RadioButton
        Friend WithEvents CursorGroup As System.Windows.Forms.GroupBox
        Friend WithEvents AnimatedOption As System.Windows.Forms.RadioButton
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.CursorGroup = New System.Windows.Forms.GroupBox
            Me.AnimatedOption = New System.Windows.Forms.RadioButton
            Me.LoadOption = New System.Windows.Forms.RadioButton
            Me.CursorOption = New System.Windows.Forms.RadioButton
            Me.DefaultOption = New System.Windows.Forms.RadioButton
            Me.CursorGroup.SuspendLayout()
            Me.SuspendLayout()
            '
            'CursorGroup
            '
            Me.CursorGroup.Controls.Add(Me.AnimatedOption)
            Me.CursorGroup.Controls.Add(Me.LoadOption)
            Me.CursorGroup.Controls.Add(Me.CursorOption)
            Me.CursorGroup.Controls.Add(Me.DefaultOption)
            Me.CursorGroup.Location = New System.Drawing.Point(24, 16)
            Me.CursorGroup.Name = "CursorGroup"
            Me.CursorGroup.Size = New System.Drawing.Size(232, 160)
            Me.CursorGroup.TabIndex = 0
            Me.CursorGroup.TabStop = False
            Me.CursorGroup.Text = "Cursor"
            '
            'AnimatedOption
            '
            Me.AnimatedOption.Location = New System.Drawing.Point(24, 120)
            Me.AnimatedOption.Name = "AnimatedOption"
            Me.AnimatedOption.Size = New System.Drawing.Size(160, 24)
            Me.AnimatedOption.TabIndex = 3
            Me.AnimatedOption.Text = "&Animated Cursor"
            '
            'LoadOption
            '
            Me.LoadOption.Location = New System.Drawing.Point(24, 88)
            Me.LoadOption.Name = "LoadOption"
            Me.LoadOption.Size = New System.Drawing.Size(160, 24)
            Me.LoadOption.TabIndex = 2
            Me.LoadOption.Text = "&Load Cursor"
            '
            'CursorOption
            '
            Me.CursorOption.Location = New System.Drawing.Point(24, 56)
            Me.CursorOption.Name = "CursorOption"
            Me.CursorOption.Size = New System.Drawing.Size(160, 24)
            Me.CursorOption.TabIndex = 1
            Me.CursorOption.Text = "&Cursor Object"
            '
            'DefaultOption
            '
            Me.DefaultOption.Checked = True
            Me.DefaultOption.Location = New System.Drawing.Point(24, 24)
            Me.DefaultOption.Name = "DefaultOption"
            Me.DefaultOption.Size = New System.Drawing.Size(160, 24)
            Me.DefaultOption.TabIndex = 0
            Me.DefaultOption.TabStop = True
            Me.DefaultOption.Text = "&Default"
            '
            'MainForm
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(376, 238)
            Me.Controls.Add(Me.CursorGroup)
            Me.Name = "MainForm"
            Me.Text = "Colored Cursors"
            Me.CursorGroup.ResumeLayout(False)
            Me.ResumeLayout(False)

        End Sub
#End Region
        Private Sub DefaultOption_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles DefaultOption.CheckedChanged
            Me.Cursor = Cursors.Default
        End Sub
        Private Sub CursorOption_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles CursorOption.CheckedChanged
            Dim filename As String

            If CursorOption.Checked Then
                Me.Cursor = Cursors.WaitCursor
                'causes an error
                'filename = Path.Combine(Application.StartupPath, cursorColored)
                'Me.Cursor = New Cursor(filename)
            End If
        End Sub
        Private Sub LoadOption_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles LoadOption.CheckedChanged
            Dim filename As String

            If LoadOption.Checked Then
                filename = Path.Combine(Application.StartupPath, cursorColored)
                Me.Cursor = CursorFactory.Create(filename)
            End If
        End Sub
        Private Sub AnimatedOption_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles AnimatedOption.CheckedChanged
            Dim filename As String

            If AnimatedOption.Checked Then
                filename = Path.Combine(Application.StartupPath, cursorAnimated)
                Me.Cursor = CursorFactory.Create(filename)
            End If
        End Sub
    End Class
End Namespace
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
planoczCommented:
If you look at C:\WINDOWS\Cursors on your computer you will find a good selection..
0
 
dekeyzerAuthor Commented:
The problem is that we want to design our cursor in runtime as a result of what the user draws. He can really draw ANYTHING!  So even with a million cur-files in the  C:\WINDOWS\Cursors-folder, we don't have enough.

Thanks!

0
 
planoczCommented:
Here is a link you might want to look at...
Iam still searching for you..

http://www.webtropy.com/articles/art9-1.asp?f=CreateCursor
0
 
AlexFMCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Resources/Cursors/UsingCursors.asp

This Microsoft sample contains code which can create cursor from bitmap. You can create managed Bitmap object, get HBITMAP from it, convert HBITMAP to HCURSOR using Win32 code from this sample, and create Cursor from HCURSOR using constructor Public Sub New(IntPtr).

0
 
dekeyzerAuthor Commented:
Hi,

I found the solution, thanks for your help !
        Dim img As Bitmap
        Dim fr_bm As New Bitmap(100, 100)
        Dim wc As New System.Net.WebClient
        Dim gr As Graphics = Graphics.FromImage(fr_bm)
        gr.Clear(System.Drawing.Color.Empty)
        gr.draw.... (everthing you want)
        fr_bm.Save("c:\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
        img = New Bitmap(wc.OpenRead("c:\test.bmp"))
        Me.Cursor = New Cursor(img.GetHicon)
0
 
AlexFMCommented:
Very nice. Bitmap.GetHicon Method - this is a bridge betweenBbitmap and Cursor. You may replace also file with MemoryStream if you want.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now