15,978
社区成员
发帖
与我相关
我的任务
分享
class CtestView : public CView
{
DECLARE_DYNCREATE(CtestView)
// 重写
public:
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
protected:
DECLARE_MESSAGE_MAP()
CDC m_MemDC; //首先定义一个显示设备对象
CBitmap m_MemBitmap;//定义一个位图对象
int m_nWidth;
int m_nHeight;
bool m_bFirstDraw;
void MoveScreen();
};
//.cpp
CtestView::CtestView()
{
m_bFirstDraw = TRUE;
}
CtestView::~CtestView()
{
m_MemBitmap.DeleteObject();
m_MemDC.DeleteDC();
}
void CtestView::OnDraw(CDC* pDC)
{
if (m_bFirstDraw)
{
CRect rc;
GetClientRect(&rc);
m_nWidth = rc.Width();
m_nHeight = rc.Height();
//随后建立与屏幕显示兼容的内存显示设备
m_MemDC.CreateCompatibleDC(NULL);
//这时还不能绘图,因为没有地方画 ^_^
//下面建立一个与屏幕显示兼容的位图,至于位图的大小嘛,可以用窗口的大小
m_MemBitmap.CreateCompatibleBitmap(pDC,m_nWidth,m_nHeight);
//将位图选入到内存显示设备中
//只有选入了位图的内存显示设备才有地方绘图,画到指定的位图上
CBitmap *pOldBit=m_MemDC.SelectObject(&m_MemBitmap);
//先用背景色将位图清除干净,这里我用的是白色作为背景
//你也可以用自己应该用的颜色
m_MemDC.FillSolidRect(0,0,m_nWidth,m_nHeight,RGB(255,0,255));
//绘图
CRect rc1(0,m_nHeight/3,m_nWidth,m_nHeight/2);
m_MemDC.DrawText(L"text",-1,&rc1,DT_CENTER);
m_bFirstDraw = FALSE;
}
else
{
MoveScreen();
}
//将内存中的图拷贝到屏幕上进行显示
pDC->BitBlt(0,0,m_nWidth,m_nHeight,&m_MemDC,0,0,SRCCOPY);
}
//这个函数哪里有问题 ? 我的想法是把 MemDC上下两部分互换,结果屏幕一片黑。
void CtestView::MoveScreen()
{
CDC tmpDC;
//建立与显示兼容的内存显示设备
tmpDC.CreateCompatibleDC(&m_MemDC);
//这时还不能绘图,因为没有地方画 ^_^
//下面建立一个与屏幕显示兼容的位图
CBitmap tmpBitmap;
tmpBitmap.CreateCompatibleBitmap(&m_MemDC,m_nWidth,m_nHeight);
//将位图选入到内存显示设备中
//只有选入了位图的内存显示设备才有地方绘图,画到指定的位图上
CBitmap *pOldBit=m_MemDC.SelectObject(&tmpBitmap);
// copy image on tmpDC for backup
tmpDC.BitBlt(0,0,m_nWidth,m_nHeight,&m_MemDC,0,0,SRCCOPY);
// to redraw on MemDC
m_MemDC.BitBlt(0,0,m_nWidth,m_nHeight/2,&tmpDC,0,m_nHeight/2,SRCCOPY);
m_MemDC.BitBlt(0,m_nHeight/2,m_nWidth,m_nHeight/2,&tmpDC,0,0,SRCCOPY);
//绘图完成后的清理
tmpBitmap.DeleteObject();
tmpDC.DeleteDC();
}