15,976
社区成员
发帖
与我相关
我的任务
分享
void CCountDClock::Init()
{
m_pDc = new CDC;//m_pDC是控件类CCountDClock的成员变量,类型为CDC*
m_pDc->CreateCompatibleDC(NULL);
CBitmap bmp;
bmp.CreateCompatibleBitmap(m_pDc, rect.Width(), rect.Height());
m_pDc->SelectObject(&bmp);
Graphics graphics(m_pDc->GetSafeHdc());
Image image_bckground(TEXT(".\\res\\student_background.png"));
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
bmp.DeleteObject();
}
void CCountDClock::OnPaint()
{
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
CPaintDC dc(this);
CDC memdc;
CBitmap bmp;
CRect rc;
int nWidth, nHeight;
CFont m_font1;
CFont m_font2;
memdc.CreateCompatibleDC(&dc);
GetClientRect(&rc);
nWidth = rc.Width();nHeight = rc.Height();
int nClock_width, nClock_height,nClockcenter_width, nClockcenter_height;
bmp.CreateCompatibleBitmap(&dc, nWidth, nHeight);
memdc.SelectObject(&bmp);
memdc.FillSolidRect(rc,RGB(0,0,0));
memdc.SetBkMode(TRANSPARENT);
Graphics graphics(memdc.GetSafeHdc());
// Image image_bckground(TEXT(".\\res\\student_background.png"));
memdc.BitBlt(0, 0, rc.Width(), rc.Height(), m_pDc, 0, 0, SRCCOPY);//此处想把之前的m_pDC指向的内存设备复制到memdc中,但没有成功
Image image_clockframe(TEXT(".\\res\\alarm_empty.png"));
nClock_width = image_clockframe.GetWidth();
nClock_height = image_clockframe.GetHeight();
// graphics.SetSmoothingMode(SmoothingModeAntiAlias);
// graphics.DrawImage(&image_bckground, 0, 0, nWidth, nHeight);//原来的绘图函数由于绘制频率较高,太占用CPU
.................................
}

CDC memdc;
CBitmap bmp;
CRect rc;
int nWidth, nHeight;
CFont m_font1;
CFont m_font2;
memdc.CreateCompatibleDC(&dc);
GetClientRect(&rc);
nWidth = rc.Width();nHeight = rc.Height();
int nClock_width, nClock_height,nClockcenter_width, nClockcenter_height;
bmp.CreateCompatibleBitmap(&dc, nWidth, nHeight);
memdc.SelectObject(&bmp);
然后绘制背景图:
Graphics graphics(memdc.GetSafeHdc());
Image image_bckground(TEXT(".\\res\\student_background.png"));
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
graphics.DrawImage(&image_bckground, 0, 0, nWidth, nHeight);
现在我不想每次在OnPaint函数中通过graphics.DrawImage(&image_bckground, 0, 0, nWidth, nHeight);来绘制背景图,而是想定义一个全局的CDC变量,在窗口创建的时候讲背景图先绘制在这个内存设备环境的位图上,然后每次OnPaint函数中通过bitblt将背景图绘制到现在的设备环境的位图上,再在这幅位图上进行其他部件的绘制。也就是说我需要调用两次BITBLT函数,一次是复制背景图,一次是将背景图以及在此基础上全部绘制好的整个画面复制到实际的DC中。两次调用bitblt函数的对象不同,一个是CDC memdc.bitblt(),一次是CPaintDC dc.bitblt。
表达能力比较差,还望见谅。
CPaintDC dc(this);
CRect rc;
int nWidth, nHeight;
GetClientRect(&rc);
nWidth = rc.Width();nHeight = rc.Height();
if(m_pDC ==NULL)
{
m_pDC = new CDC;
m_bmp.CreateCompatibleBitmap(&dc, nWidth, nHeight);
m_pDC.CreateCompatibleDC(&dc);
}
CBrush brush(RGB(255,255,255));
CBitmap* pold=m_pDC->SelectObject(&m_bmp);
m_pDC->FillRect(&rc&brush);
//这里自己写函数对内存DC操作 传参数为m_pDC;
dc.->BitBlt(0,0,nWidth,nHeight,m_pDC,0,0,SRCCOPY);
m_pDC->SelectObject(pold);
最后记得释放内存m_pDC和m_bmp资源