|
-
October 29th, 2004, 12:57 AM
#1
OnMouseMove in CView derived class
i have written a gdi application to draw some graphics. i have mapped the OnMouseMove() message handler to draw the selected shape as the mouse is dragged. the problem in this code execution is it creates multiple rectangles, after selecting the rectangle, even with the LBM clicked. How to avoid this problem?
the code is
void CJPG1View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// CREATE THE DEVICE CONTEXT .
m_cPoint = point;
CClientDC dc(this);
//select the pen of green color
CPen myPen(PS_SOLID,1,RGB(102,222,102));
// select a brush
CBrush myBrush(RGB(0,0,0));
// select a cursor similar to that of mspaint.
HCURSOR hCursor;
hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
m_bCursor = TRUE;
SetCursor(hCursor);
dc.SetROP2(R2_NOTXORPEN);
dc.SelectObject(&myPen);
SetCapture();
if(nFlags == MK_LBUTTON )//|| (this->m_LBtnDown && m_MousePoint != point) )
{
switch(m_nMenuOption)
{
case DRAW_LINE:
dc.MoveTo(m_MousePoint.x,m_MousePoint.y);
dc.LineTo(point.x,point.y);
break;
case DRAW_RECT:
dc.Rectangle(m_MousePoint.x, point.x, m_MousePoint.y, point.y);
break;
case DRAW_CIRCLE:
dc.Ellipse(m_MousePoint.x, point.x, m_MousePoint.y, point.y);
break;
}
}
ReleaseCapture();
m_MousePoint = point;
CView::OnMouseMove(nFlags, point);
}
Last edited by uma_rohini; October 29th, 2004 at 01:23 AM.
-
October 29th, 2004, 01:52 AM
#2
Re: OnMouseMove in CView derived class
I think the best solution would be to make the shapes persistent. You could store the shapes in a list (only store the latest rectangle when you move) and do the drawing in OnDraw where you can clear the view and redraw all shapes from the stored list.
Good luck
/ Johan
-
October 29th, 2004, 02:00 AM
#3
Re: OnMouseMove in CView derived class
Where are you storing the Position of the Previous Mouse Point ?
You need to first DELETE the previous Rectangle.
You can redraw it in the R2_NOT mode , from the Origin to the previous mouse position.
Then you need to Draw the rectangle from the Origin to the Current Mouse Position.
Consider entering this code in the LButtonDown :
void CJPG1View::OnLButtonDown(UINT nFlags, CPoint point)
{
...
m_pointOld = point;
...
}
And in the MouseMove function ,
First Draw the rectangle from m_pointOrigin to m_pointOld in the R2_NOT mode, and then in the R2_COPYPEN mode, draw it from PointOrigin to Current Point.
-
October 29th, 2004, 03:37 AM
#4
Re: OnMouseMove in CView derived class
ADDITIONAL NOTE:
As already thousand times said in this forum, do not draw inside mouse messages handlers.
Just call Invalidate or InvalidateRect and draw inside CView::OnDraw.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|