这里贴上代码:
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this); // device context for paintingBITMAP bm;
CBitmap bmp;//定义CBitmap 类的对象
bmp.LoadBitmap(IDB_BITMAP2);//装入ID号为IDB_BITMAP1的位图
CDC memdc;//定义CDC类的对象
CRect rect;
memdc.CreateCompatibleDC(NULL);//创建与显示DC相兼容的内存DC
bmp.GetBitmap(&bm);
CBitmap *bmpold=memdc.SelectObject(&bmp);//将选定的位图选入内存DC
this->GetClientRect(&rect);
//从内存DC向显示DC复制,rect.left,rect.top为图像左上角的坐标,SRCCOPY表示直接将源位图拷贝到目的位图,不作修改
dc.BitBlt(rect.left+x,rect.top+y,bm.bmWidth,bm.bmHeight,&memdc,0,0,SRCCOPY);
memdc.SelectObject(bmpold);
memdc.DeleteDC();
bmp.DeleteObject();
CDialog::OnPaint();
}
}
void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
a=1;
m_point=point;
CDialog::OnLButtonDown(nFlags, point);
}
void CMyDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
a=0;
InvalidateRect(NULL,true);
CDialog::OnLButtonUp(nFlags, point);
}
void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if(a==1){
x=x+point.x-m_point.x;
y=y+point.y-m_point.y;
m_point=point;
RedrawWindow();
}
CDialog::OnMouseMove(nFlags, point);
}
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this); // device context for paintingBITMAP bm;
CBitmap bmp;//定义CBitmap 类的对象
bmp.LoadBitmap(IDB_BITMAP2);//装入ID号为IDB_BITMAP1的位图
CDC memdc;//定义CDC类的对象
CRect rect;
memdc.CreateCompatibleDC(NULL);//创建与显示DC相兼容的内存DC
bmp.GetBitmap(&bm);
CBitmap *bmpold=memdc.SelectObject(&bmp);//将选定的位图选入内存DC
this->GetClientRect(&rect);
//从内存DC向显示DC复制,rect.left,rect.top为图像左上角的坐标,SRCCOPY表示直接将源位图拷贝到目的位图,不作修改
dc.BitBlt(rect.left+x,rect.top+y,bm.bmWidth,bm.bmHeight,&memdc,0,0,SRCCOPY);
memdc.SelectObject(bmpold);
memdc.DeleteDC();
bmp.DeleteObject();
CDialog::OnPaint();
}
}
void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
a=1;
m_point=point;
CDialog::OnLButtonDown(nFlags, point);
}
void CMyDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
a=0;
InvalidateRect(NULL,true);
CDialog::OnLButtonUp(nFlags, point);
}
void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if(a==1){
x=x+point.x-m_point.x;
y=y+point.y-m_point.y;
m_point=point;
RedrawWindow();
}
CDialog::OnMouseMove(nFlags, point);
}
CDC tempDC;
使用GetClientRect获取客户区大小,也就是对话框内部大小。
CBitmap bmp;
bmp.CreateCompatibleBitmap(DC*, width, height)。
刷新tempDC后把你加载的BMP的DC,BitBlt到这个tempDC。也就是在tempDC上画。
画完后(除了画你的图片,你还可以画很多其他东西),最后再将tempDC直接画到dc上。这才算是双缓冲。你原来的画法是把自己的图片直接画到DC上,实际上没有缓冲的。
GetClientRect(&rtClt);
long h = (rtClt.bottom - rtClt.top);
long w = (rtClt.right - rtClt.left); CDC tempDC;
CBitmap tempBmp, *pTmpBmp;
tempDC.CreateCompatibleDC(&dc);
tempBmp.CreateCompatibleBitmap(&dc, w, h);
pTmpBmp = tempDC.SelectObject(&tempBmp); // 刷新tempDC背景
// .. 这时在tempDC上绘制 dc.BitBlt(0, 0, w, h, &tempDC, 0, 0, SRCCOPY); tempDC.SelectObject(pTmpBmp);
tempBmp.DeleteObject();
tempDC.DeleteDC();
WM_ERASEBKGND还是按原来的做,因为有时的确需要刷新整个对话框背景。