在Symbian中实现双缓冲技术首先需要在内存中建立一个位图缓冲区对象,然后再获取位图对象的设备上下文dc(Device Context),程序可以在任意的地方对内存缓冲位图的dc绘制图形。在Draw事件内,将缓冲区位图直接绘制在设备的dc上。
在 .h 中加入如下定义:
- CWsBitmap* iBufBmp;
- CFbsBitmapDevice* iBufDevice;
- CBitmapContext* iBufGc;
在 .cpp 中加入实现代码:
- /**
- * 初始化双缓冲区
- */
- void CTestAppView::InitDoubleBufferL()
- {
- iBufBmp = new(ELeave)CWsBitmap(CEikonEnv::Static()->WsSession());
- CleanupStack::PushL(iBufBmp);
- User::LeaveIfError(iBufBmp->Create(Rect().Size(), CEikonEnv::Static()->ScreenDevice()->DisplayMode()));
- iBufDevice = CFbsBitmapDevice::NewL(iBufBmp);
- CleanupStack::PushL(iBufDevice);
- User::LeaveIfError(iBufDevice->CreateBitmapContext(iBufGc));
- CleanupStack::Pop(2); // iDevice, iBufBmp
- }
- /**
- * 测试在缓冲区上绘制
- */
- void CTestAppView::DoTestDraw()
- {
- iBufGc->Clear(Rect());
- // 在buffer里画方块,而不是在屏幕上
- for (int i=0; i<100; i+=2)
- {
- iBufGc->DrawRect(TRect(TPoint(i, i), TSize(50, 50)));
- }
- }
- /**
- * View的重绘事件
- */
- void CTestAppView::Draw(const TRect& /*aRect*/) const
- {
- // 以下代码忽略
- // // Get the standard graphics context
- // CWindowGc& gc = SystemGc();
- //
- // // Gets the control’s extent
- // TRect drawRect(Rect());
- //
- // // Clears the screen
- // gc.Clear(drawRect);
- SystemGc().BitBlt(TPoint(0, 0), iBufBmp);
- }
说明:
1) iBufBmp为缓冲区位图对象,为CWsBitmap类型。类CWsBitmap继承自类CFbsBitmap,我们在此使用CWsBitmap的原因为因为它比较快,引入SDK中对CWsBitmap类的说明:
This is a bitmap to which the window server already has a handle. Functions
which take a window server bitmap are faster than equivalent functions which
take a CFbsBitmap.
2) iBufDevice为CFbsBitmapDevice类型的对象,CFbsBitmapDevice的官方解释可以简单的理解为管理文字和位图的图形设备:
A graphics device to which a bitmap managed by the font and bitmap server can be drawn.
3) iBufGc为CBitmapContext类型的对象,即位图对象的设备上下文dc,获取了iBufGc后,可以使用CBitmapContext中的方法对位图进行绘制。