moving image and invalidate

msmeth

Member
Joined
Jun 17, 2006
Messages
6
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 Im using as pointing devices.

Im using Visual C++ .NET 2003. What I need to do is move around the cursors in the most efficient way possible and Im 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? Ive read about it and it looks like I need to use Invalidate(region) but havent been able to get it to work. Ive 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.
 
Last edited by a moderator:
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 dont see how a timer could come into play, unless you want to redraw at a given framerate, and even then, the two solutions arent 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.
 
Theyre within a window. Ill 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?
 
Last edited by a moderator:
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:


Code:
    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
 
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 isnt being redrawn but the line shows continously?
 
Back
Top