用双缓冲,还是出现闪烁,该如何处理
用双缓冲,还是出现闪烁
void CTetris::KeyDown( WPARAM &wParam )
{
switch( wParam )
{
case 0x25:
Move( LEFT );
break;
case 0x26:
if( ThisID != 3 )
{
TurnTo( m_now );
}
break;
case 0x27:
Move( RIGHT );
break;
case 0x28:
Move( DOWN );
break;
case 0x20:
Move( FALL );
break;
}
InvalidateRect( m_hWnd , &ClientRect, true );
}
void CTetris::DrawBKG( HDC &hdc )
{
SetBkMode(hdc, TRANSPARENT);
HDC hdcmem = CreateCompatibleDC( hdc );
SelectObject( hdcmem, BackgroundBmp );
BitBlt( hdc, ClientRect.left, ClientRect.top, ClientRect.right, ClientRect.bottom, hdcmem, 0, 0 , SRCCOPY );
HPEN hPen = CreatePen( PS_SOLID, 10, RGB( 255, 255, 0 ) );
SelectObject( hdc, hPen );
Rectangle( hdc, HostFrame.left - 5, HostFrame.top - 5, HostFrame.right + 26, HostFrame.bottom + 26 );
Rectangle (hdc, SeverFrame.left - 5 , SeverFrame.top - 5 , SeverFrame.right + 17 * m_size + 26 , SeverFrame.bottom + 26 );
//Rectangle( hdc, NextShapePos.x * m_size, NextShapePos.y * m_size, ( NextShapePos.x + 4 ) * m_size, ( NextShapePos.y + 4 ) * m_size );
SelectObject( hdcmem, FrameBmp );
BitBlt( hdc, HostFrame.left, HostFrame.top, HostFrame.right, HostFrame.bottom, hdcmem, 0, 0, SRCCOPY );
BitBlt( hdc, SeverFrame.left, SeverFrame.top, SeverFrame.right, SeverFrame.bottom, hdcmem, 0, 0, SRCCOPY );
LPWSTR LineStr, ScoreStr, SpeedStr, TimeStr;
LineStr = new WCHAR [30];
ScoreStr = new WCHAR [30];
SpeedStr = new WCHAR [30];
TimeStr = new WCHAR [30];
swprintf( LineStr, L" 行数: %d", Line );
swprintf( ScoreStr,L" 得分: %d", Score );
swprintf( SpeedStr,L" 速度: %d", Speed );
swprintf( TimeStr, L" 游戏进行时间: %d", Time );
TextOutW( hdc, HostFrame.right + 2 * m_size, 15 * m_size, LineStr, wcslen( LineStr ) );
TextOutW( hdc, HostFrame.right + 2 * m_size, 16 * m_size, ScoreStr, wcslen( ScoreStr ) );
TextOutW( hdc, HostFrame.right + 2 * m_size, 17 * m_size, SpeedStr, wcslen( SpeedStr ) );
TextOutW( hdc, HostFrame.right + 2 * m_size, 19 * m_size, TimeStr, wcslen( TimeStr ) );
DeleteDC( hdcmem );
}
void CTetris::DrawBuffer( HDC hdc )
{
HDC hdcBuf = CreateCompatibleDC( hdc );
HBITMAP hBMp = CreateCompatibleBitmap( hdc, ClientRect.right, ClientRect.bottom );
oldhBMp = ( HBITMAP ) SelectObject( hdcBuf, hBMp );
DrawBKG( hdcBuf );
DrawSquares( hdcBuf );
BitBlt( hdc, 0, 0, ClientRect.right, ClientRect.bottom, hdcBuf, 0, 0 ,SRCCOPY );
SelectObject( hdcBuf, oldhBMp );
DeleteObject( hBMp );
DeleteDC( hdcBuf );
}
有时会出现小小的闪烁,但在我一直按住一个移动键的时候屏幕就会严重的闪烁。刷新太频繁了? 如何改?
------解决方案--------------------
有处理WM_ERASEBKGND吗?
void CTetris::KeyDown( WPARAM &wParam )
{
switch( wParam )
{
case 0x25:
Move( LEFT );
break;
case 0x26:
if( ThisID != 3 )
{
TurnTo( m_now );
}
break;
case 0x27:
Move( RIGHT );
break;
case 0x28:
Move( DOWN );
break;
case 0x20:
Move( FALL );
break;
}
InvalidateRect( m_hWnd , &ClientRect, true );
}
void CTetris::DrawBKG( HDC &hdc )
{
SetBkMode(hdc, TRANSPARENT);
HDC hdcmem = CreateCompatibleDC( hdc );
SelectObject( hdcmem, BackgroundBmp );
BitBlt( hdc, ClientRect.left, ClientRect.top, ClientRect.right, ClientRect.bottom, hdcmem, 0, 0 , SRCCOPY );
HPEN hPen = CreatePen( PS_SOLID, 10, RGB( 255, 255, 0 ) );
SelectObject( hdc, hPen );
Rectangle( hdc, HostFrame.left - 5, HostFrame.top - 5, HostFrame.right + 26, HostFrame.bottom + 26 );
Rectangle (hdc, SeverFrame.left - 5 , SeverFrame.top - 5 , SeverFrame.right + 17 * m_size + 26 , SeverFrame.bottom + 26 );
//Rectangle( hdc, NextShapePos.x * m_size, NextShapePos.y * m_size, ( NextShapePos.x + 4 ) * m_size, ( NextShapePos.y + 4 ) * m_size );
SelectObject( hdcmem, FrameBmp );
BitBlt( hdc, HostFrame.left, HostFrame.top, HostFrame.right, HostFrame.bottom, hdcmem, 0, 0, SRCCOPY );
BitBlt( hdc, SeverFrame.left, SeverFrame.top, SeverFrame.right, SeverFrame.bottom, hdcmem, 0, 0, SRCCOPY );
LPWSTR LineStr, ScoreStr, SpeedStr, TimeStr;
LineStr = new WCHAR [30];
ScoreStr = new WCHAR [30];
SpeedStr = new WCHAR [30];
TimeStr = new WCHAR [30];
swprintf( LineStr, L" 行数: %d", Line );
swprintf( ScoreStr,L" 得分: %d", Score );
swprintf( SpeedStr,L" 速度: %d", Speed );
swprintf( TimeStr, L" 游戏进行时间: %d", Time );
TextOutW( hdc, HostFrame.right + 2 * m_size, 15 * m_size, LineStr, wcslen( LineStr ) );
TextOutW( hdc, HostFrame.right + 2 * m_size, 16 * m_size, ScoreStr, wcslen( ScoreStr ) );
TextOutW( hdc, HostFrame.right + 2 * m_size, 17 * m_size, SpeedStr, wcslen( SpeedStr ) );
TextOutW( hdc, HostFrame.right + 2 * m_size, 19 * m_size, TimeStr, wcslen( TimeStr ) );
DeleteDC( hdcmem );
}
void CTetris::DrawBuffer( HDC hdc )
{
HDC hdcBuf = CreateCompatibleDC( hdc );
HBITMAP hBMp = CreateCompatibleBitmap( hdc, ClientRect.right, ClientRect.bottom );
oldhBMp = ( HBITMAP ) SelectObject( hdcBuf, hBMp );
DrawBKG( hdcBuf );
DrawSquares( hdcBuf );
BitBlt( hdc, 0, 0, ClientRect.right, ClientRect.bottom, hdcBuf, 0, 0 ,SRCCOPY );
SelectObject( hdcBuf, oldhBMp );
DeleteObject( hBMp );
DeleteDC( hdcBuf );
}
有时会出现小小的闪烁,但在我一直按住一个移动键的时候屏幕就会严重的闪烁。刷新太频繁了? 如何改?
------解决方案--------------------
有处理WM_ERASEBKGND吗?