最近刚学MFC,打算做一个类似WIN XP画图软件,遇到几个问题,请高手们解决啊
第一个:画直线的时候在鼠标移动过程中之前的线没有清除
第二个:画矩形或者椭圆鼠标移动重叠过程中先前的矩形或者椭圆一部分会被遮住
的三个:画矩形或者椭圆时先在鼠标移动时选的颜色不起作用 鼠标松开才变颜色 但是我改了之后中间又出现一些别的颜色
我的原代码如下:
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_suishou)startPT=point;
if(m_rectangle||m_ellipse||m_line){startPT=point;endPT=point;SetCapture();} CView::OnLButtonDown(nFlags, point);
}void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.SelectObject(&pen);
if(m_suishou)startPT=-1;
if(m_rectangle)
{
ReleaseCapture();
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(startPT.x,startPT.y,point.x,point.y);
dc.SelectObject(old);
} if(m_ellipse)
{
ReleaseCapture();
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
dc.Ellipse(startPT.x,startPT.y,point.x,point.y);
dc.SelectObject(old);
} if(m_line)
{
ReleaseCapture();
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
dc.MoveTo(startPT);
dc.LineTo(point);
dc.SelectObject(old);
}
CView::OnLButtonUp(nFlags, point);
}void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.SelectObject(&pen);
//随手线
if(m_suishou&&startPT.x>0&&(nFlags&&MK_LBUTTON))
{
dc.SelectObject(&pen);
endPT=point;
dc.MoveTo(startPT);
dc.LineTo(endPT);
startPT.x=endPT.x;
startPT.y=endPT.y;
}
//画方
if(m_rectangle&&(nFlags&&MK_LBUTTON))
{
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
int mode=dc.GetROP2();
dc.SetROP2(R2_NOTCOPYPEN);
dc.Rectangle(endPT.x,endPT.y,startPT.x,startPT.y);
dc.SetROP2(mode);
dc.Rectangle(startPT.x,startPT.y,point.x,point.y);
dc.SelectObject(old);
endPT=point;
}
//画圆
if(m_ellipse&&(nFlags&&MK_LBUTTON))
{
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
int mode=dc.GetROP2();
dc.SetROP2(R2_NOTCOPYPEN);
dc.Ellipse(endPT.x,endPT.y,startPT.x,startPT.y);
dc.SetROP2(mode);
dc.Ellipse(startPT.x,startPT.y,point.x,point.y);
dc.SelectObject(old);
endPT=point;
}
//画线
if(m_line&&(nFlags&&MK_LBUTTON))
{
dc.MoveTo(startPT);
dc.LineTo(point);
}
CView::OnMouseMove(nFlags, point);
}void CMyView::penchoice(UINT id)
{ static DWORD penstyle;
static COLORREF color;
switch(id)
{
case ID_black:color=black;break;
case ID_red:color=red;break;
case ID_green:color=green;break;
case ID_blue:color=blue;break;
case ID_pen:ResetAllFlag();m_suishou=true;break;
case ID_tuoyuan:ResetAllFlag();m_ellipse=true;break;
case ID_rect:ResetAllFlag();m_rectangle=true;break;
case ID_line:ResetAllFlag();m_line=true;break;
}
pen.DeleteObject();
pen.CreatePen(penstyle,1,color);
}
第一个:画直线的时候在鼠标移动过程中之前的线没有清除
第二个:画矩形或者椭圆鼠标移动重叠过程中先前的矩形或者椭圆一部分会被遮住
的三个:画矩形或者椭圆时先在鼠标移动时选的颜色不起作用 鼠标松开才变颜色 但是我改了之后中间又出现一些别的颜色
我的原代码如下:
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_suishou)startPT=point;
if(m_rectangle||m_ellipse||m_line){startPT=point;endPT=point;SetCapture();} CView::OnLButtonDown(nFlags, point);
}void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.SelectObject(&pen);
if(m_suishou)startPT=-1;
if(m_rectangle)
{
ReleaseCapture();
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(startPT.x,startPT.y,point.x,point.y);
dc.SelectObject(old);
} if(m_ellipse)
{
ReleaseCapture();
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
dc.Ellipse(startPT.x,startPT.y,point.x,point.y);
dc.SelectObject(old);
} if(m_line)
{
ReleaseCapture();
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
dc.MoveTo(startPT);
dc.LineTo(point);
dc.SelectObject(old);
}
CView::OnLButtonUp(nFlags, point);
}void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.SelectObject(&pen);
//随手线
if(m_suishou&&startPT.x>0&&(nFlags&&MK_LBUTTON))
{
dc.SelectObject(&pen);
endPT=point;
dc.MoveTo(startPT);
dc.LineTo(endPT);
startPT.x=endPT.x;
startPT.y=endPT.y;
}
//画方
if(m_rectangle&&(nFlags&&MK_LBUTTON))
{
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
int mode=dc.GetROP2();
dc.SetROP2(R2_NOTCOPYPEN);
dc.Rectangle(endPT.x,endPT.y,startPT.x,startPT.y);
dc.SetROP2(mode);
dc.Rectangle(startPT.x,startPT.y,point.x,point.y);
dc.SelectObject(old);
endPT=point;
}
//画圆
if(m_ellipse&&(nFlags&&MK_LBUTTON))
{
CGdiObject *old=dc.SelectStockObject(NULL_BRUSH);
int mode=dc.GetROP2();
dc.SetROP2(R2_NOTCOPYPEN);
dc.Ellipse(endPT.x,endPT.y,startPT.x,startPT.y);
dc.SetROP2(mode);
dc.Ellipse(startPT.x,startPT.y,point.x,point.y);
dc.SelectObject(old);
endPT=point;
}
//画线
if(m_line&&(nFlags&&MK_LBUTTON))
{
dc.MoveTo(startPT);
dc.LineTo(point);
}
CView::OnMouseMove(nFlags, point);
}void CMyView::penchoice(UINT id)
{ static DWORD penstyle;
static COLORREF color;
switch(id)
{
case ID_black:color=black;break;
case ID_red:color=red;break;
case ID_green:color=green;break;
case ID_blue:color=blue;break;
case ID_pen:ResetAllFlag();m_suishou=true;break;
case ID_tuoyuan:ResetAllFlag();m_ellipse=true;break;
case ID_rect:ResetAllFlag();m_rectangle=true;break;
case ID_line:ResetAllFlag();m_line=true;break;
}
pen.DeleteObject();
pen.CreatePen(penstyle,1,color);
}
3.绘图前设置画笔的颜色