Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Draw a circle that follows the mouse cursor without latent circles

Posted on 2009-03-31
4
Medium Priority
?
1,006 Views
Last Modified: 2013-12-17
I have a form and would like to have a circle follow the mouse cursor.  Similarly to what is shown in Adobe Photoshop when you are in line brush mode and you can see the size of the brush you are about to draw.  My problem occurs when I place a background image on the form.

To use the example code below.  Create form and paste the code into the form (the size of my form is 804X584).  Run the application.  Initially you will have great performance.  However if you give the form a background image.  In my situation I just used C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.jpg and you will see some latent circles when you move the mouse very fast.  The attached picture shows you the latent circles.

I've tried using the OvalShape from Microsoft.VisualBasic.PowerPacks but the performance of that was even worse.

Some notes about the code below:
1.  I pretty draw the circle in the function ShowQualityCircle (called from MouseMove).  Here I call Invalidate without any arguments.  When I tried it with a rectangle I got a number of latent circles.  I even tried a larger rectangles but when I moved the mouse very fast I still got latent circles.
2.  Without the call to draw the circle in the Paint routine I got a number of latent rectangles, so that was necessary.

You help would be greatly appreciated.

Public Class Form1
  Private QCircle As New Rectangle(0, 0, 0, 0)
 
  Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    ShowQualityCircle((e.X), (e.Y), 20)
  End Sub
 
  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    If QCircle.Width > 0 AndAlso QCircle.Height > 0 Then
      DrawCircle(e.Graphics, Color.Red, QCircle.X, QCircle.Y, QCircle.Width, QCircle.Height, 0, 360)
    End If
  End Sub
 
  Private Sub DrawCircle(ByRef g As Graphics, ByVal currentColor As Color, ByVal x As Single, ByVal y As Single, ByVal width As Single, ByVal height As Single, ByVal startAngle As Single, ByVal sweepAngle As Single)
    Dim myBrush As New SolidBrush(currentColor)
    Dim myPen As New Pen(myBrush)
    myPen.Width = 1
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
    g.DrawArc(myPen, x, y, width, height, startAngle, sweepAngle)
  End Sub
 
  Private Sub ShowQualityCircle(ByVal imgX As Integer, ByVal imgY As Integer, ByVal mypenradius As Integer)
    QCircle = New Rectangle(imgX - mypenradius, imgY - mypenradius, mypenradius * 2, mypenradius * 2)
    Dim g As Graphics = Me.CreateGraphics()
    DrawCircle(g, Color.Red, QCircle.X, QCircle.Y, QCircle.Width, QCircle.Height, 0, 360)
    g.Dispose()
    'Me.Refresh()
    Me.Invalidate()
  End Sub
End Class

Open in new window

doublecircle.png
0
Comment
Question by:tedunni
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 24033304
Create a CURSOR instead of trying to draw that puppy all over the place...  ;)
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

Open in new window

0
 

Author Closing Comment

by:tedunni
ID: 31564963
Thanks, this solution is perfect.
0
 

Author Comment

by:tedunni
ID: 24051593
Idle_Mind, I'm going to ask a related question regarding this, but if I were to have a radius of say 48 it only draws part of the circle.  Additionally, if the radius gets quite large (try 576), then I get a GDI+ exception.  Any suggestions.  

Btw, the related question wizard keeps asking me to assign a title for the question (Even when I assign one), and so I'm posting here but once I get that figured out I'll post the link to the related question.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question