做一个项目,要求在地图上显示某个运动的图标或者自己绘的小圆点之类的,本人新手,不懂绘图方面的东西,所以感到有点问题,我正试着做个实验:
在定时器中递增坐标,然后在onpaint函数中采用双缓冲的方法绘图,但是有个问题就是图标在移动的时候,后面留了个尾巴,感觉是地图没重绘造成的,但又不知道错在哪,感谢各位热心朋友帮忙,谢谢,下面是代码: 另外看网上说添加wm_erasebkgnd消息响应函数OnEraseBkgnd,返回值为true可以防止闪烁,闪烁的情况是没有了,可以它把我对话框上其他的控件都变透明了,这有什么好的解决方法呢? void CTESTDlg::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
{
CClientDC dc(this);
CDC dcCompatible; //定义一个显示设备对象
CBitmap MemBitmap; //定义一个位图对象 dcCompatible.CreateCompatibleDC(&dc); //随后建立兼容DC
// MemBitmap.CreateCompatibleBitmap(&dc,100,100); // CBitmap *pold=dcCompatible.SelectObject(&MemBitmap);
CBitmap *ppold=dcCompatible.SelectObject(&bmp); //将位图选入到兼容dc中
//dcCompatible.FillSolidRect(&rect,RGB(255,255,255)); dcCompatible.DrawIcon(i-rect.left,j-rect.top,hicon); //rect是我用来显示地图的某个控件 dc.BitBlt(rect.left,rect.top,rect.right,rect.bottom,&dcCompatible,0,0,SRCCOPY);
//cWnd->InvalidateRect(&rect,true); MemBitmap.DeleteObject();
dcCompatible.DeleteDC(); CDialog::OnPaint(); }
} // The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTESTDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTESTDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default i+=2;
j+=2; //ij的初试化都是100
cWnd->InvalidateRect(&rect,true);
CDialog::OnTimer(nIDEvent);
} BOOL CTESTDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
//return CDialog::OnEraseBkgnd(pDC);
return true;
}
在定时器中递增坐标,然后在onpaint函数中采用双缓冲的方法绘图,但是有个问题就是图标在移动的时候,后面留了个尾巴,感觉是地图没重绘造成的,但又不知道错在哪,感谢各位热心朋友帮忙,谢谢,下面是代码: 另外看网上说添加wm_erasebkgnd消息响应函数OnEraseBkgnd,返回值为true可以防止闪烁,闪烁的情况是没有了,可以它把我对话框上其他的控件都变透明了,这有什么好的解决方法呢? void CTESTDlg::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
{
CClientDC dc(this);
CDC dcCompatible; //定义一个显示设备对象
CBitmap MemBitmap; //定义一个位图对象 dcCompatible.CreateCompatibleDC(&dc); //随后建立兼容DC
// MemBitmap.CreateCompatibleBitmap(&dc,100,100); // CBitmap *pold=dcCompatible.SelectObject(&MemBitmap);
CBitmap *ppold=dcCompatible.SelectObject(&bmp); //将位图选入到兼容dc中
//dcCompatible.FillSolidRect(&rect,RGB(255,255,255)); dcCompatible.DrawIcon(i-rect.left,j-rect.top,hicon); //rect是我用来显示地图的某个控件 dc.BitBlt(rect.left,rect.top,rect.right,rect.bottom,&dcCompatible,0,0,SRCCOPY);
//cWnd->InvalidateRect(&rect,true); MemBitmap.DeleteObject();
dcCompatible.DeleteDC(); CDialog::OnPaint(); }
} // The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTESTDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTESTDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default i+=2;
j+=2; //ij的初试化都是100
cWnd->InvalidateRect(&rect,true);
CDialog::OnTimer(nIDEvent);
} BOOL CTESTDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
//return CDialog::OnEraseBkgnd(pDC);
return true;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货