Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hey all.

 

I need to have two cursors on my screen, but neither are controlled by a mouse. They are controlled by two external devices I'm using as pointing devices.

 

I'm using Visual C++ .NET 2003. What I need to do is move around the cursors in the most efficient way possible and I'm not sure how to do this. What is the best way to display the images (bitmaps but I can change that) and to update the screen without the entire thing being redrawn all the time? I've read about it and it looks like I need to use Invalidate(region) but haven't been able to get it to work. I've also read about using a timer but am confused as to how to really do it. Could someone could give me an example or a good link please......thanks in advance.

Edited by msmeth
  • Leaders
Posted

Are the cursors on the screen or within a window? Assuming that the answer is within a window, I would use the Invalidate(Rectangle) overload. It is very simple: you call it on a specific control, specifying a rectangle that needs to be redrawn. The control will redraw only that rectangle. I don't see how a timer could come into play, unless you want to redraw at a given framerate, and even then, the two solutions aren't mutually exclusive.

 

The signifigance of the difference between drawing to the screen versus to a window is that the Invalidate method can only work on a window that belongs to a .Net application.

[sIGPIC]e[/sIGPIC]
Posted (edited)
They're within a window. I'll try the Invalidate(rect). What about loading the image. I load the image and specify to redraw the rectangle where the image is right? Is there a best way to do that and specifying the rectange area? And do I call Invalidate in the Paint event? Edited by msmeth
Posted

Calling invalidate fires the paint event - so you want to do it when you need to redraw the cursor.

Calling invalidate(rec) fires the paint event, any drawing in the paint event that is outside the rectangle is just ignored.

 

So: do all painting in the paint event.

 

Note: Each time you draw the cursor you need to invalidate the previous position of the cursor so that the background is refreshed, and invalidate the area for the new position so that it is drawn. So you could invalidate twice, or invalidate a rectangle that encompasses both the old and new cursor bounds. It is simpler code in this case to use a Region instead of a Rectangle, as it has methods to combine areas:

 

 

    Private x, y As Integer
   Private lastCursorBounds As Rectangle

   Sub New()
       InitializeComponent()
       Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)
       Me.UpdateStyles()
       x = Windows.Forms.Cursor.Position.X
       y = Windows.Forms.Cursor.Position.Y
       lastCursorBounds = New Rectangle(x, y, 21, 21)
   End Sub

   Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
       x = e.X
       y = e.Y
       ' So we need to invalidate the previous area:
       Dim areaToInvalidate As New Region(lastCursorBounds)
       ' And the new area (not sure why this needs to be 21!)
       Dim newCursorBounds As New Rectangle(e.X, e.Y, 21, 21)
       areaToInvalidate.Union(newCursorBounds)
       Invalidate(areaToInvalidate)
       lastCursorBounds = newCursorBounds
   End Sub

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       e.Graphics.DrawEllipse(Pens.Red, x, y, 20, 20)
   End Sub

Posted

drawing line as cursor moves

 

Hi. I got that first issue dealt with just fine. Now I have another. I need to draw lines similar to drawing lines using a mouse (click to start the line and click to end the line. There is no holding down like dragging a mouse). How do I use invalidate so the entire screen isn't being redrawn but the line shows continously?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...