首先,你应该定义一个CBitmap和CDC的全局变量,用来在View创建时将这个背景位图加载到内存中。而不是在每次绘制时加载。原因是加快函数处理速度,降低屏幕的闪烁。注意,如果是大区域绘制,即使是光凭把位图加载到内存中来处理也还是不够的。 以下是大致处理: 加载:这个你该会吧,我就不说了。// 显示 void CXXXXView::OnDraw(CDC* pDC) { CRect rect; pDC->GetClipBox (rect); DrawBackBitmap(pDC,rect); }// 绘制,例如可以添加一些文字信息等等,最后把内存DC复制到屏幕DC中 void CXXXXView::DrawBackBitmap(CDC *pDC,CRect &rect) { // pDC->StretchBlt(......); }如果想尽可能降低闪烁,可以借助下面这个类。 #ifndef _MEMDC_H_ #define _MEMDC_H_////////////////////////////////////////////////// // CMemDC - memory DC // // This class implements a memory Device Contextclass CMemDC : public CDC { private: CBitmap m_bitmap; // Offscreen bitmap CBitmap* m_oldBitmap; // bitmap originally found in CMemDC CDC* m_pDC; // Saves CDC passed in constructor CRect m_rect; // Rectangle of drawing area. BOOL m_bMemDC; // TRUE if CDC really is a Memory DC. public: CMemDC(CDC* pDC) : CDC(), m_oldBitmap(NULL), m_pDC(pDC) { ASSERT(m_pDC != NULL); // If you asserted here, you passed in a NULL CDC. m_bMemDC = !pDC->IsPrinting(); if (m_bMemDC){ // Create a Memory DC CreateCompatibleDC(pDC); pDC->GetClipBox(&m_rect); m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height()); m_oldBitmap = SelectObject(&m_bitmap); SetWindowOrg(m_rect.left, m_rect.top); } else { // Make a copy of the relevent parts of the current DC for printing m_bPrinting = pDC->m_bPrinting; m_hDC = pDC->m_hDC; m_hAttribDC = pDC->m_hAttribDC; } } ~CMemDC() { if (m_bMemDC) { // Copy the offscreen bitmap onto the screen. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(), this, m_rect.left, m_rect.top, SRCCOPY); //Swap back the original bitmap. SelectObject(m_oldBitmap); } else { // All we need to do is replace the DC with an illegal value, // this keeps us from accidently deleting the handles associated with // the CDC that was passed to the constructor. m_hDC = m_hAttribDC = NULL; } } // Allow usage as a pointer CMemDC* operator->() {return this;} // Allow usage as a pointer operator CMemDC*() {return this;} };#endif使用方法很简单。例如: void CXXXXView::OnDraw(CDC* pDC) { CRect rect; pDC->GetClipBox (rect); CMemDC memdc(pDC); DrawBackBitmap(&memdc,rect); }这样就可以了。
pMemDC->CreateCompatibleDC(&dc);
pMemDC->SetBkMode(TRANSPARENT); CBitmap* pBitmap=new CBitmap;
pBitmap->LoadBitmap(IDB_SPLASH); // Prepare DC
CBitmap* pOldBitmap = pMemDC->SelectObject(pBitmap); dc.BitBlt(0, 0, 640, 480, pMemDC, 0, 0, SRCCOPY);
pMemDC->SelectObject(pOldBitmap); // Destroy pMenDC,pBitmap
delete pMemDC;
delete pBitmap;
如果要伸缩方式的话,把BitBlt改成StretchBlt,当然,参数也是要改的
把代码放在OnSize()里
不要问我其他 我没写过 我只是提供思路
TO: maoxianwang
Thank you first,
I want the code also.
my email is:
[email protected]
ocean69
CScrollView::GetWindowRect ,
CDC::GetClipBox
等取得绘图区域的函数的用法啊?
比如,我想取得CScrollView中所有绘制区域(包括ScrollBar尚未滚出来的区域),该怎么办啊?
以下是大致处理:
加载:这个你该会吧,我就不说了。// 显示
void CXXXXView::OnDraw(CDC* pDC)
{
CRect rect;
pDC->GetClipBox (rect);
DrawBackBitmap(pDC,rect);
}// 绘制,例如可以添加一些文字信息等等,最后把内存DC复制到屏幕DC中
void CXXXXView::DrawBackBitmap(CDC *pDC,CRect &rect)
{
//
pDC->StretchBlt(......);
}如果想尽可能降低闪烁,可以借助下面这个类。
#ifndef _MEMDC_H_
#define _MEMDC_H_//////////////////////////////////////////////////
// CMemDC - memory DC
//
// This class implements a memory Device Contextclass CMemDC : public CDC {
private:
CBitmap m_bitmap; // Offscreen bitmap
CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
CDC* m_pDC; // Saves CDC passed in constructor
CRect m_rect; // Rectangle of drawing area.
BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
public:
CMemDC(CDC* pDC) : CDC(), m_oldBitmap(NULL), m_pDC(pDC)
{
ASSERT(m_pDC != NULL); // If you asserted here, you passed in a NULL CDC. m_bMemDC = !pDC->IsPrinting(); if (m_bMemDC){
// Create a Memory DC
CreateCompatibleDC(pDC);
pDC->GetClipBox(&m_rect);
m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
m_oldBitmap = SelectObject(&m_bitmap);
SetWindowOrg(m_rect.left, m_rect.top);
} else {
// Make a copy of the relevent parts of the current DC for printing
m_bPrinting = pDC->m_bPrinting;
m_hDC = pDC->m_hDC;
m_hAttribDC = pDC->m_hAttribDC;
}
} ~CMemDC()
{
if (m_bMemDC) {
// Copy the offscreen bitmap onto the screen.
m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
this, m_rect.left, m_rect.top, SRCCOPY);
//Swap back the original bitmap.
SelectObject(m_oldBitmap);
} else {
// All we need to do is replace the DC with an illegal value,
// this keeps us from accidently deleting the handles associated with
// the CDC that was passed to the constructor.
m_hDC = m_hAttribDC = NULL;
}
} // Allow usage as a pointer
CMemDC* operator->() {return this;} // Allow usage as a pointer
operator CMemDC*() {return this;}
};#endif使用方法很简单。例如:
void CXXXXView::OnDraw(CDC* pDC)
{
CRect rect;
pDC->GetClipBox (rect);
CMemDC memdc(pDC);
DrawBackBitmap(&memdc,rect);
}这样就可以了。
在CScrollView中绘图出现的问题始终不能解决,并且
当窗口被其他窗口覆盖时,也会出现问题。我想:也许是我的
void CMyScrollView::DrawBackBitmap(CDC *pDC,CRect &rect)
{
CDC* memDC = new CDC;
memDC->CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = memDC->SelectObject(&m_bitmap);
pDC->BitBlt(rect.top,rect.left,
rect.Width(),rect.Height(),
memDC,
0,0,
SRCCOPY);
memDC->SelectObject(pOldBitmap);
memDC->DeleteDC();
delete memDC;
}
没处理好,请问,你能给个具体能实现的例子代码吗?