这样子是不是使用GetKeyState实现向右移动
#include <windows.h>
#pragma comment(lib, "msimg32.lib")
#define KEY_RIGHT(VK_RIGHT) (GetKeyState(VK_RIGHT) & 0x80) > 0 ? true : false;
//函数声明
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);//窗口处理函数
//变量定义
HWND hwnd;
HDC hdc, hdcmen;
BITMAP bm;
HBITMAP hbm;
PAINTSTRUCT ps;
//主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
//设计窗口类
WNDCLASS wndclass = {
CS_HREDRAW | CS_VREDRAW,
WndProc,
0,
0,
hInstance,
LoadIcon(NULL, IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW),
(HBRUSH)GetStockObject(WHITE_BRUSH),
NULL,
TEXT("MYDEMO")
};
//注册窗口类
RegisterClass(&wndclass);
//创建窗口类
hwnd = CreateWindow(
TEXT("MYDEMO"),
TEXT("MyDemo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
//显示窗口
ShowWindow(hwnd, nCmdShow);
//更新窗口
UpdateWindow(hwnd);
//消息循环
MSG Msg;
BOOL bRet;
PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE);
while (Msg.message != WM_QUIT)
{
bRet = PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE);
if (bRet)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return Msg.wParam;
}
//窗口过程处理函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int xMonster = 400, yMonster = 400;
const int step = 10;
const int ssep = 5;
switch (message)
{
case WM_KEYDOWN:
switch (wParam) {
case VK_LEFT:
xMonster -= step;
break;
case VK_RIGHT:
xMonster += ssep;
break;
}
InvalidateRect(hwnd, NULL, true);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hdcmen = CreateCompatibleDC(hdc);
hbm = (HBITMAP)LoadImage(
NULL,
TEXT("car.bmp"),
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE);
SelectObject(hdcmen, hbm);
GetObject(hbm, sizeof(BITMAP), &bm);
BitBlt(hdc, xMonster, yMonster, bm.bmWidth, bm.bmHeight, hdcmen, 0, 0, SRCCOPY);
DeleteObject(hbm);
DeleteDC(hdcmen);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}