msmeth Posted June 17, 2006 Posted June 17, 2006 (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 June 17, 2006 by msmeth Quote
Leaders snarfblam Posted June 17, 2006 Leaders Posted June 17, 2006 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. Quote [sIGPIC]e[/sIGPIC]
msmeth Posted June 17, 2006 Author Posted June 17, 2006 (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 June 17, 2006 by msmeth Quote
jo0ls Posted June 17, 2006 Posted June 17, 2006 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 Quote
msmeth Posted June 17, 2006 Author Posted June 17, 2006 Okay thanks...makes sense. I'll try and convert your code to C++. Thanks a lot. Quote
msmeth Posted June 19, 2006 Author Posted June 19, 2006 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? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.