一个Windows API SetROP2(int nDrawMode)的使用
该函数的主要的作用是根据nDrawMode设置的方式重新设定绘图的方式,下面就不同的nDrawMode值具体解释绘图模式是如何改变的。
首先就nDrawMode的取值有以下的情况:
R2_BLACK Pixel is always black. //所有绘制出来的像素为黑色
R2_WHITE Pixel is always white. //所有绘制出来的像素为白色
R2_NOP Pixel remains unchanged. //任何绘制将不改变当前的状态
R2_NOT Pixel is the inverse of the screen color. //当前绘制的像素值设为屏幕像素值的反,这样可以覆盖掉上次的绘图,(自动擦除上次绘制的图形)
R2_COPYPEN Pixel is the pen color. //使用当前的画笔的颜色
R2_NOTCOPYPEN Pixel is the inverse of the pen color. //当前画笔的反色
public:
CTextDoc* GetDocument();
CPoint m_start,m_end;
bool m_line;
int m_step;
//初始化
m_line = false;
m_step = 0;
//
void CTextView::DrawRectangle(CPoint LeftTop, CPoint RightBottom, COLORREF color,CDC *pDC)
{
CPen pen(PS_DASH,1,color);
CPen* pOldPen=pDC->SelectObject(&pen);
CBrush *pOldBr=(CBrush *)pDC->SelectStockObject(NULL_BRUSH);
pDC->Rectangle(LeftTop.x,LeftTop.y,RightBottom.x,RightBottom.y);
pDC->SelectObject(pOldBr);
pDC->SelectObject(pOldPen);
}
/////////////////////////////////////////////////////////////////////////////
// CTextView message handlers
void CTextView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC=GetDC();//获取设备环境DC
switch (m_step)
{
case 0:
m_start = point;
m_end = point;
m_step++;
m_line = false;
Invalidate(NULL);
break;
case 1:
m_end = point;
m_step--;
m_line = true;
DrawRectangle(m_start,m_end,RGB(255,0,0),pDC);
break;
default:;
}
CView::OnLButtonDown(nFlags, point);
}
void CTextView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC=GetDC();//获取设备环境DC
int nDrawmode=pDC->SetROP2(R2_NOT);//设置绘图模式,并保存先前的绘图模式
//pDC->SelectStockObject(NULL_BRUSH);//将库存GDI对象选进设备环境DC
if(!m_line && m_step == 1)//第一次单击了鼠标左键,并且还没有绘制矩形(即等待第二次单击鼠标左键)
{
CPoint prePnt,curPnt;
prePnt=m_end;//获得鼠标所在的前一个位置
curPnt=point;
//绘制拖曳矩形
DrawRectangle(m_start,prePnt,RGB(255,0,0),pDC);
DrawRectangle(m_start,curPnt,RGB(255,0,0),pDC);
m_end = point;
}
pDC->SetROP2(nDrawmode);//恢复先前的绘图模式
ReleaseDC(pDC);//释放掉不再使用的DC
CView::OnMouseMove(nFlags, point);
}