高分求解,关于VC++
我用win32API写了个用键盘控制动画移动的程序,但问题是动画移动后会留下痕迹,问题出现在哪啊!!!麻烦帮我看一下。代码如下:运行时需要张图片,图片命名为:tank.b...
我用win32API写了个用键盘控制动画移动的程序,但问题是动画移动后会留下痕迹,问题出现在哪啊!!!麻烦帮我看一下。代码如下:运行时需要张图片,图片命名为:tank.bmp
#include<stdio.h>
#include<windows.h>
HBITMAP tank;
HDC mdc;
int x=0,y=0;
HDC hdc;
void MyPaint(HDC hdc);
LRESULT CALLBACK wndproc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pstr, int nCmdShow )
{
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
wndclass.style=CS_VREDRAW|CS_HREDRAW;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=wndproc;
wndclass.lpszClassName="ha";
wndclass.lpszMenuName=NULL;
RegisterClass(&wndclass);
hwnd=CreateWindow("ha","标题",WS_OVERLAPPEDWINDOW, 150,150,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void MyPaint(HDC hdc)
{
mdc=CreateCompatibleDC(hdc);
tank=(HBITMAP)LoadImage(NULL,"tank.bmp",IMAGE_BITMAP,39,37,LR_LOADFROMFILE);
SelectObject(mdc,tank);
BitBlt(hdc,150+x,150+y,39,37,mdc,0,0,SRCCOPY);
}
LRESULT CALLBACK wndproc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
PAINTSTRUCT ps;
HDC hdc;
switch(message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
MyPaint(hdc);
EndPaint(hwnd,&ps);
break;
case WM_KEYDOWN:
hdc=GetDC(hwnd);
switch(LOWORD(wparam))
{
case VK_UP:
y=y-10;
MyPaint(hdc);
break;
case VK_DOWN:
y=y+10;
MyPaint(hdc);
break;
case VK_LEFT:
x=x-10;
MyPaint(hdc);
break;
case VK_RIGHT:
x=x+10;
MyPaint(hdc);
break;
}
ReleaseDC(hwnd,hdc);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wparam,lparam);
}
return 0;
}
就是刷新不掉啊!!!怎么个刷新法!!!把(MyPaint(hdc);)放在WinMain函数里面?我试过没用啊!!! 展开
#include<stdio.h>
#include<windows.h>
HBITMAP tank;
HDC mdc;
int x=0,y=0;
HDC hdc;
void MyPaint(HDC hdc);
LRESULT CALLBACK wndproc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pstr, int nCmdShow )
{
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
wndclass.style=CS_VREDRAW|CS_HREDRAW;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=wndproc;
wndclass.lpszClassName="ha";
wndclass.lpszMenuName=NULL;
RegisterClass(&wndclass);
hwnd=CreateWindow("ha","标题",WS_OVERLAPPEDWINDOW, 150,150,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void MyPaint(HDC hdc)
{
mdc=CreateCompatibleDC(hdc);
tank=(HBITMAP)LoadImage(NULL,"tank.bmp",IMAGE_BITMAP,39,37,LR_LOADFROMFILE);
SelectObject(mdc,tank);
BitBlt(hdc,150+x,150+y,39,37,mdc,0,0,SRCCOPY);
}
LRESULT CALLBACK wndproc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
PAINTSTRUCT ps;
HDC hdc;
switch(message)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
MyPaint(hdc);
EndPaint(hwnd,&ps);
break;
case WM_KEYDOWN:
hdc=GetDC(hwnd);
switch(LOWORD(wparam))
{
case VK_UP:
y=y-10;
MyPaint(hdc);
break;
case VK_DOWN:
y=y+10;
MyPaint(hdc);
break;
case VK_LEFT:
x=x-10;
MyPaint(hdc);
break;
case VK_RIGHT:
x=x+10;
MyPaint(hdc);
break;
}
ReleaseDC(hwnd,hdc);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wparam,lparam);
}
return 0;
}
就是刷新不掉啊!!!怎么个刷新法!!!把(MyPaint(hdc);)放在WinMain函数里面?我试过没用啊!!! 展开
3个回答
展开全部
移动之前要重绘一下背景,在MyPaint函数里这样改:
void MyPaint(HDC hdc)
{
HBRUSH brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
int nMaxX=GetSystemMetrics(SM_CXSCREEN);
int nMaxY=GetSystemMetrics(SM_CYSCREEN);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = nMaxX;
rect.bottom = nMaxY;
FillRect(hdc, &rect, brush);
mdc=CreateCompatibleDC(hdc);
tank=(HBITMAP)LoadImage(NULL,"tank.bmp",IMAGE_BITMAP,39,37,LR_LOADFROMFILE);
SelectObject(mdc,tank);
BitBlt(hdc,150+x,150+y,39,37,mdc,0,0,SRCCOPY);
}
void MyPaint(HDC hdc)
{
HBRUSH brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
int nMaxX=GetSystemMetrics(SM_CXSCREEN);
int nMaxY=GetSystemMetrics(SM_CYSCREEN);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = nMaxX;
rect.bottom = nMaxY;
FillRect(hdc, &rect, brush);
mdc=CreateCompatibleDC(hdc);
tank=(HBITMAP)LoadImage(NULL,"tank.bmp",IMAGE_BITMAP,39,37,LR_LOADFROMFILE);
SelectObject(mdc,tank);
BitBlt(hdc,150+x,150+y,39,37,mdc,0,0,SRCCOPY);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把
case VK_UP:
case VK_DOWN:
case VK_LEFT:
case VK_RIGHT:
里的MyPaint(hdc);换成RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);试试看
case VK_UP:
case VK_DOWN:
case VK_LEFT:
case VK_RIGHT:
里的MyPaint(hdc);换成RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);试试看
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我没看程序,不过你移动后应该刷新掉原来的图像吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询