在透明背景上拖动图片,拖尾问题。
我自做了一个类,基类是STATIC,是透明背景,功能是在上面显示一个位图。而且能用鼠标拖动位图。现在问题是,我拖动位图时有拖尾,如何解决呢?
在OnPaint()调用函数Draw(),我的Draw()代码如下:
Draw()
{
CClientDC clientDC(this);
HDC hDC=::GetDC(this->m_hWnd);
CDC *pDC=CDC::FromHandle(hDC);
CRect crect;//client rect
GetClientRect(crect);
CBitmap bitmap;
CDC bitmapDC;
bitmapDC.CreateCompatibleDC(&clientDC);
CBitmap* pOldBitmap;
bitmap.LoadBitmap(IDB_VERTICAL_MY_THUMB);//
pOldBitmap = bitmapDC.SelectObject(&bitmap);
BITMAP m_MyBm;
bitmap.GetBitmap(&m_MyBm);
CRect m_ThumbRect;
int m_Width,m_Height;
m_Width = m_MyBm.bmWidth;
m_Height = m_MyBm.bmHeight;
m_ThumbRect.left=(m_rectClient.left+((m_rectClient.Width()-m_MyBm.bmWidth)/2));
m_ThumbRect.top=m_rectThumb.top;
DrawTransparent(pDC,m_ThumbRect.left,m_ThumbRect.top,RGB(255,0,0));
}
后来经过参考别人做法,在Draw()程序开头添加如下代码:
CClientDC clientDC(this);
//CDC *pDC=new CDC;
//pDC->Attach(clientDC.m_hDC);
HDC hDC=::GetDC(this->m_hWnd);
CDC *pDC=CDC::FromHandle(hDC);
//CDC* pDC; //表示控件窗口的设备描述表.DC
//pDC=GetDC();
CRect crect;//client rect
CRect wrect;//window rect
GetClientRect(crect);
GetWindowRect(wrect);
GetParent()->ScreenToClient(wrect);
GetParent()->InvalidateRect(crect);
GetParent()->UpdateWindow();
if (hDcBk == NULL)
{
hDcBk = CreateCompatibleDC(clientDC.m_hDC);
hBmpBk = CreateCompatibleBitmap(clientDC.m_hDC, crect.Width(), crect.Height());
hBmpBkOld = (HBITMAP)::SelectObject(hDcBk, hBmpBk);
::BitBlt(hDcBk, 0, 0, crect.Width(), crect.Height(), clientDC.m_hDC, wrect.left, wrect.top, SRCCOPY);
}
// //This bit does the tics marks transparently.
// //create a memory dc to hold a copy of the oldbitmap data that includes the tics,
// //because when we add the background in we will lose the tic marks
HDC hSaveHDC;
HBITMAP hSaveBmp;
int iWidth = crect.Width();
int iHeight = crect.Height();
hSaveHDC = ::CreateCompatibleDC(pDC->m_hDC);
hSaveBmp = ::CreateCompatibleBitmap(hSaveHDC, iWidth, iHeight);
HBITMAP hSaveCBmpOld = (HBITMAP)::SelectObject(hSaveHDC, hSaveBmp);
//set the colours for the monochrome mask bitmap
COLORREF crOldBack = ::SetBkColor(pDC->m_hDC, RGB(0,0,0));
COLORREF crOldText = ::SetTextColor(pDC->m_hDC, RGB(255,255,255));
::BitBlt(hSaveHDC, 0, 0, iWidth, iHeight, pDC->m_hDC, crect.left, crect.top, SRCCOPY);
::BitBlt(pDC->m_hDC, 0, 0, iWidth, iHeight, hDcBk, 0, 0, SRCCOPY);
::BitBlt(pDC->m_hDC, 0, 0, iWidth, iHeight, hSaveHDC, 0, 0, SRCAND);
//restore and clean up
::SetBkColor(pDC->m_hDC, crOldBack);
::SetTextColor(pDC->m_hDC, crOldText);
::SelectObject(hSaveHDC, hSaveCBmpOld);
::DeleteObject(hSaveBmp);
::DeleteDC(hSaveHDC);
即先保存透明背景DC,然后绘制透明背景,再绘制位图。拖尾是没有了,但透明背景却变成了桌面了,而且闪烁,我要的背景是对话框背景啊,如何是好呢?请高手指点迷津,跪谢!