R
RobinS
I am drawing a rectangle on a picture that has already been drawn
on the graphics area (a user control). It works something like this:
//in the MouseDown event
m_isDragging = true;
m_oldX = e.X; //save original positions
m_oldY = e.Y;
//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);
Graphics g = Graphics.FromHwnd(this.Handle);
Rectangle myRectangle =
new Rectangle(firstX, firstY, m_diffX, m_diffY);
g.DrawRectangle(m_myPen, myRectangle);
g.Dispose();
Invalidate(myRectangle);
}
//in the MouseUp event
if (m_isDragging)
{
m_isDragging = false;
//save the position and size of the rectangle (code snipped)
//invalidate the whole usercontrol (which is just the panel
// on which the picture was already painted)
this.Invalidate();
}
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?
I've tried saving thd old rectangle and invalidating that
when drawing the new rectangle, like this:
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);
Graphics g = Graphics.FromHwnd(this.Handle);
Rectangle myRectangle =
new Rectangle(firstX, firstY, m_diffX, m_diffY);
g.DrawRectangle(m_myPen, myRectangle);
g.Dispose();
//I'd probably want to use whichever rectangle is larger here,
// but I'm just trying this to see what it does.
Invalidate(m_oldRectangle); //instead of myRectangle
//save the old rectangle
m_oldRectangle =
new Rectangle(firstX, firstY, m_diffX, m_diffY);
}
This doesn't work either.
Any other ideas? I'd appreciate any help you can provide.
Thanks in advance,
Robin S.
on the graphics area (a user control). It works something like this:
//in the MouseDown event
m_isDragging = true;
m_oldX = e.X; //save original positions
m_oldY = e.Y;
//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);
Graphics g = Graphics.FromHwnd(this.Handle);
Rectangle myRectangle =
new Rectangle(firstX, firstY, m_diffX, m_diffY);
g.DrawRectangle(m_myPen, myRectangle);
g.Dispose();
Invalidate(myRectangle);
}
//in the MouseUp event
if (m_isDragging)
{
m_isDragging = false;
//save the position and size of the rectangle (code snipped)
//invalidate the whole usercontrol (which is just the panel
// on which the picture was already painted)
this.Invalidate();
}
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?
I've tried saving thd old rectangle and invalidating that
when drawing the new rectangle, like this:
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);
Graphics g = Graphics.FromHwnd(this.Handle);
Rectangle myRectangle =
new Rectangle(firstX, firstY, m_diffX, m_diffY);
g.DrawRectangle(m_myPen, myRectangle);
g.Dispose();
//I'd probably want to use whichever rectangle is larger here,
// but I'm just trying this to see what it does.
Invalidate(m_oldRectangle); //instead of myRectangle
//save the old rectangle
m_oldRectangle =
new Rectangle(firstX, firstY, m_diffX, m_diffY);
}
This doesn't work either.
Any other ideas? I'd appreciate any help you can provide.
Thanks in advance,
Robin S.