下面代码是想实现一个图片的移动。
但整个界面在图片移动的时候却会有闪烁,我已经使用了双缓冲
不知道才能让界面不闪烁,而图片又能移动呢?Point g_ptMove( 0, 0 );
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
case WM_TIMER:
{
g_ptMove.X += 2; RECT rectWin;
GetClientRect( hwnd, &rectWin );
InvalidateRect( hwnd, &rectWin, TRUE ); //重绘
}
break; case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint( hwnd, &ps ); RECT rectWin;
GetClientRect( hwnd, &rectWin );
int iWinWidth = rectWin.right - rectWin.left;
int iWinHeight = rectWin.bottom - rectWin.top; Bitmap bitmap( iWinWidth, iWinHeight );
Graphics graphicsMem( &bitmap ); wchar_t * wstrImagePath = MyMultiByteToWideChar( "E:\\snake.jpg" );
Image * pImage = new Image( wstrImagePath );
if ( NULL != pImage )
{
graphicsMem.DrawImage( pImage, g_ptMove.X, g_ptMove.Y );
SAFE_DEL( pImage );
if( NULL != wstrImagePath )
delete []wstrImagePath;
} Graphics graphics( hDC );
graphics.DrawImage( &bitmap, g_ptMove.X, g_ptMove.Y );
EndPaint( hwnd, &ps );
}
break;
} return 0;
}
但整个界面在图片移动的时候却会有闪烁,我已经使用了双缓冲
不知道才能让界面不闪烁,而图片又能移动呢?Point g_ptMove( 0, 0 );
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
case WM_TIMER:
{
g_ptMove.X += 2; RECT rectWin;
GetClientRect( hwnd, &rectWin );
InvalidateRect( hwnd, &rectWin, TRUE ); //重绘
}
break; case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint( hwnd, &ps ); RECT rectWin;
GetClientRect( hwnd, &rectWin );
int iWinWidth = rectWin.right - rectWin.left;
int iWinHeight = rectWin.bottom - rectWin.top; Bitmap bitmap( iWinWidth, iWinHeight );
Graphics graphicsMem( &bitmap ); wchar_t * wstrImagePath = MyMultiByteToWideChar( "E:\\snake.jpg" );
Image * pImage = new Image( wstrImagePath );
if ( NULL != pImage )
{
graphicsMem.DrawImage( pImage, g_ptMove.X, g_ptMove.Y );
SAFE_DEL( pImage );
if( NULL != wstrImagePath )
delete []wstrImagePath;
} Graphics graphics( hDC );
graphics.DrawImage( &bitmap, g_ptMove.X, g_ptMove.Y );
EndPaint( hwnd, &ps );
}
break;
} return 0;
}
case WM_ERASEBKGND:
return TRUE;
首先还是case WM_ERASEBKGND:
return TRUE;其次,if ( NULL != pImage )
{
graphicsMem.DrawImage( pImage, g_ptMove.X, g_ptMove.Y );
SAFE_DEL( pImage );
if( NULL != wstrImagePath )
delete []wstrImagePath;
}
你是直接在客户区绘图,当case WM_ERASEBKGND:return TRUE;就是说不用系统帮你擦除背景,所以你得自己擦除背景。在graphicsMem.DrawImage( pImage, g_ptMove.X, g_ptMove.Y );之前,加个绘制背景的语句,比如graphicsMem.DrawImage( pImageBackGround, 0, 0 );