Wednesday, April 1, 2009

Change the cursor of the mouse to that of a circle

I needed a tool similar to one in a paint program where you can change the mouse cursor to that of a circle.  Initially my solution was to draw a circle around the mouse and to adjust this circle on mousemove.  Therefore you had a circle that followed the mouse around the screen.  However that was not a good solution as there were latent circles all over the image (it actually works if you don't have a background image).  I then tried using the OvalShape tool that came with the PowerPack3 but again that wasn't good with a background image.  Below is a solution that works.


Public Class Form1 
    Private radius As Integer = 20
    Private startAngle As Integer = 0
    Private sweepAngle As Integer = 360
    Private currentColor As Color = Color.Red
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateCursor()
    End Sub
 
    Private Sub CreateCursor()
        Dim bmp As New Bitmap(radius * 2, radius * 2)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim myPen As New Pen(currentColor, 1)
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
        g.DrawArc(myPen, 0, 0, bmp.Width - 1, bmp.Height - 1, startAngle, sweepAngle)
        g.Dispose()
        myPen.Dispose()
 
        Dim C As New Cursor(bmp.GetHicon)
        Me.Cursor = C
    End Sub

End Class


No comments: