"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
RobinS wrote:
>This works great when you keep drawing the rectangle bigger.
But if you backtrack while dragging, it leaves the remnants
of the rectangles you have already drawn behind.
How do I handle that?
The biggest thing wrong that I see with your code is that you are drawing
the rectangle in the MouseMove event handler. You should only be drawing
anything, selection rectangle included, in your Paint event handler.
(replace "XXX event handler" with "OnXXX override method" as appropriate
:) ).
The second version of MouseMove handling you posted appears to me that it
would have the opposite problem, erasing the newly-drawn rectangle just
after you've drawn it. Sticking as closely to the code you've posted, I
think this would be better:
Rectangle m_oldRectangle;
//in the MouseMove event
if (m_isDragging)
{
m_newX = e.X;
m_newY = e.Y;
int firstX = (m_newX < m_oldX) ? m_newX : m_oldX;
int firstY = (m_newY < m_oldY) ? m_newY : m_oldY;
m_diffX = Math.Abs(m_oldX - m_newX);
m_diffY = Math.Abs(m_oldY - m_newY);
Rectangle myRectangle =
new Rectangle(firstX, firstY, m_diffX, m_diffY);
// Invalidate the combined rectangles, to ensure that
// the old rectangle is erased and the new one is drawn
Invalidate(Rectangle.Union(m_oldRectangle, myRectangle));
//save the old rectangle
m_oldRectangle = myRectangle;
}
Then in the Paint event:
e.Graphics.DrawRectangle(m_myPen, m_oldRectangle);
Of course, you probably would want to rename m_oldRectangle so that it
more accurate reflects the "current" nature of the rectangle. :)
Pete
Thanks for your quick response.
1) The events are actual events. I just didn't want to put the
signature in there because I was re-typing rather than copying
and pasting. I keep trying to drag text over from my desktop
(work) machine to my laptop (personal) machine, but it just won't
go past the edge of the desktop, dang it.
2) Ah, the paint event. So here's the deal. I have a picture,
and I have a bunch of attributes that are applied to the picture.
(This is all part of an image editor.) So the user can add text,
rotate, draw rectangles, crop, etc. All of those attributes are
stored, and when the picture is displayed, there is a routine
called by the Paint event that loads the picture, applies the
attributes, and returns a finished bitmap, which is displayed
by the Paint event. In this way, we can allow the user to go
back and edit the attributes w/o losing any clarity or anything
else about the original picture.
The MouseDown draws the rectangle "temporarily". After the user
lets go of the mouse, the panel is invalidated, invoking the
Paint event, which calls the routine to load the picture
and apply the attributes, one of which is the newly stored
rectangle (storing X, Y, width, and height % in case they
change the size of the image).
All of that is working great. I'm just having trouble
with the interim MouseMove drawing of the rectangle on the
picture.
Despite what you think it might do, the code in my original
post works except when you reverse direction when drawing
the rectangle.
I tried changing Invalidate(myRectangle)
or Invalidate(oldRectangle)
to
Invalidate(Rectangle.Union(m_oldRectangle, m_Rectangle));
but it didn't help.
Any other ideas? Is there a way to invalidate just that area
so the entire thing doesn't repaint? Am I really going to
have to rewrite my Paint event to handle this, and how would
I invoke it from MouseMove? I don't really want to invalidate
the whole panel if I can help it.
Thx,
Robin S.