CMemDC这个类大家都用过吧,我在MM_TEXT模式下使用它是正常的,但在别的模式下使用就没显示了,不知道为什么,我的代码如下:
void CMyView::OnDraw(CDC* pDC)
{
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(100,100);
pDC->SetViewportExt(50,-50);
CMemDC dc(pDC);
for(int i=0;i<100;i++)
dc.Rectangle(i,i,100+i,100+i);
}
CMemDC类如下:
class CMemDC : public CDC
{
private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
public:
CMemDC(CDC* pDC, const CRect* pRect = NULL, bool boolToMemory = TRUE) : CDC()
{
ASSERT(pDC != NULL); // Some initialization
m_pDC = pDC;
m_oldBitmap = NULL;
if (boolToMemory)
m_bMemDC = !pDC->IsPrinting();
else
m_bMemDC = FALSE ; // Get the rectangle to draw
if (pRect == NULL)
{
pDC->GetClipBox(&m_rect);
}
else
{
m_rect = *pRect;
}
if (m_bMemDC)
{
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect); m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap);
SetMapMode(pDC->GetMapMode());
pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
}
else
{
// Make a copy of the relevent parts of the current DC for printing
if (pDC->IsPrinting())
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
} // Fill background
FillSolidRect(m_rect, pDC->GetBkColor());
}
~CMemDC()
{
if (m_bMemDC)
{
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY);
//Swap back the original bitmap.
SelectObject(m_oldBitmap);
}
else
{
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidently deleting the handles associated with
// the CDC that was passed to the constructor.
m_hDC = m_hAttribDC = NULL;
}
}
// Allow usage as a pointer
CMemDC* operator->()
{
return this;
} // Allow usage as a pointer
operator CMemDC*()
{
return this;
}
};
void CMyView::OnDraw(CDC* pDC)
{
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(100,100);
pDC->SetViewportExt(50,-50);
CMemDC dc(pDC);
for(int i=0;i<100;i++)
dc.Rectangle(i,i,100+i,100+i);
}
CMemDC类如下:
class CMemDC : public CDC
{
private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
public:
CMemDC(CDC* pDC, const CRect* pRect = NULL, bool boolToMemory = TRUE) : CDC()
{
ASSERT(pDC != NULL); // Some initialization
m_pDC = pDC;
m_oldBitmap = NULL;
if (boolToMemory)
m_bMemDC = !pDC->IsPrinting();
else
m_bMemDC = FALSE ; // Get the rectangle to draw
if (pRect == NULL)
{
pDC->GetClipBox(&m_rect);
}
else
{
m_rect = *pRect;
}
if (m_bMemDC)
{
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->LPtoDP(&m_rect); m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap);
SetMapMode(pDC->GetMapMode());
pDC->DPtoLP(&m_rect);
SetWindowOrg(m_rect.left, m_rect.top);
}
else
{
// Make a copy of the relevent parts of the current DC for printing
if (pDC->IsPrinting())
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
} // Fill background
FillSolidRect(m_rect, pDC->GetBkColor());
}
~CMemDC()
{
if (m_bMemDC)
{
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY);
//Swap back the original bitmap.
SelectObject(m_oldBitmap);
}
else
{
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidently deleting the handles associated with
// the CDC that was passed to the constructor.
m_hDC = m_hAttribDC = NULL;
}
}
// Allow usage as a pointer
CMemDC* operator->()
{
return this;
} // Allow usage as a pointer
operator CMemDC*()
{
return this;
}
};
pDC->SetWindowExt(100,100);
pDC->SetViewportExt(50,-50);这几句代码建议不要写在 OnDraw函数里
我想知道为什么换了映射模式后,CMemDC就失效了
在执行pDC->Rectangle(0,0,100,100)的时候,
会发生从逻辑坐标系到设备坐标系的映射,对吧,
这样绘制出来的矩形就不一定在窗口客户区的(0,0,100,100)这个位置上,
但是在执行将内存DC bitblt到屏幕上的这句代码的时候
两个DC的坐标都是逻辑坐标
那又会发生什么样的映射呢
这样绘制出来的矩形就不一定在窗口客户区的(0,0,100,100)这个位置上,对
看你的映射模式和视口原点,窗口原点的位置,和他们有关但是在执行将内存DC bitblt到屏幕上的这句代码的时候
两个DC的坐标都是逻辑坐标偶的理解把dc中的内容bitblt到屏幕上,而在屏幕上画的位置,是由bitblt中源dc的逻辑坐标决定的,把逻辑坐标转成屏幕坐标