VC 中怎么让加载的位图资源运动
我加载了一张小球的图片,现在我想让这个小球沿着与水平成45度夹角运动,当遇到边界的时候就反弹回来,仍与水平成45度。下面是写好的一部分代码:#include<window...
我加载了一张小球的图片,现在我想让这个小球沿着与水平成45度夹角运动,当遇到边界的时候就反弹回来,仍与水平成45度。
下面是写好的一部分代码:
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int i=0;
int x[50];
int y[50];
POINT pt;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//定义变量
HDC hdc; //设备环境句柄
HDC hdcmem; //内存设备环境句柄
HBITMAP hBm; //位图句柄
BITMAP bm; //BITMAP结构
int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd ;
MSG Msg ;
WNDCLASS wndclass ;
char lpszClassName[] = "位图";
char lpszTitle[]= "小球运动";
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = lpszClassName ;
if( !RegisterClass( &wndclass))
{
MessageBeep(0) ;
return FALSE ;
}
hwnd = CreateWindow( lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL) ;
//加载位图
hBm=LoadBitmap(hInstance,"pic");
//获取位图尺寸
GetObject( hBm, sizeof( BITMAP), (LPVOID)&bm) ;
ShowWindow( hwnd, nCmdShow) ;
UpdateWindow(hwnd);
while( GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage( &Msg) ;
DispatchMessage( &Msg) ;
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{ PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
//获取设备环境句柄
hdc=GetDC(hwnd);
//获取内存设备环境句柄
hdcmem=CreateCompatibleDC(hdc);
//释放设备环境句柄
ReleaseDC(hwnd,hdc);
SetTimer(hwnd,1,40,NULL);
GetCursorPos(&pt);
for(i=0;i<50;i++)
{
x[i]=pt.x;
y[i]=pt.y;
}
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
//将位图选入内存设备环境
SelectObject(hdcmem,hBm);
//调用函数BitBlt将内存设备环境中的位图拷贝到设备环境中
BitBlt(hdc,0,200,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
//释放设备环境句柄
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
//释放位图
DeleteObject(hBm);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
请帮忙看看。 展开
下面是写好的一部分代码:
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int i=0;
int x[50];
int y[50];
POINT pt;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//定义变量
HDC hdc; //设备环境句柄
HDC hdcmem; //内存设备环境句柄
HBITMAP hBm; //位图句柄
BITMAP bm; //BITMAP结构
int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd ;
MSG Msg ;
WNDCLASS wndclass ;
char lpszClassName[] = "位图";
char lpszTitle[]= "小球运动";
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = lpszClassName ;
if( !RegisterClass( &wndclass))
{
MessageBeep(0) ;
return FALSE ;
}
hwnd = CreateWindow( lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL) ;
//加载位图
hBm=LoadBitmap(hInstance,"pic");
//获取位图尺寸
GetObject( hBm, sizeof( BITMAP), (LPVOID)&bm) ;
ShowWindow( hwnd, nCmdShow) ;
UpdateWindow(hwnd);
while( GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage( &Msg) ;
DispatchMessage( &Msg) ;
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{ PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
//获取设备环境句柄
hdc=GetDC(hwnd);
//获取内存设备环境句柄
hdcmem=CreateCompatibleDC(hdc);
//释放设备环境句柄
ReleaseDC(hwnd,hdc);
SetTimer(hwnd,1,40,NULL);
GetCursorPos(&pt);
for(i=0;i<50;i++)
{
x[i]=pt.x;
y[i]=pt.y;
}
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
//将位图选入内存设备环境
SelectObject(hdcmem,hBm);
//调用函数BitBlt将内存设备环境中的位图拷贝到设备环境中
BitBlt(hdc,0,200,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
//释放设备环境句柄
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
//释放位图
DeleteObject(hBm);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
请帮忙看看。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询