把case WM_KEYDOWN 放进去这个函数里面if (PtInRect(&ra, pt)) 要怎么做?
目的:选择中小箱子的位置,然后通过键盘进行移动。
#include <windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
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 = TEXT("MyClass");
if (!RegisterClass(&wndclass))//(注册窗口类的API是RegisterClass)
{
MessageBox(NULL, TEXT("RegisterClass fail!"), szAppName, MB_ICONERROR);
return 0;
}
//窗口起始位置(100,100),大小300*400
hwnd = CreateWindow(
TEXT("MyClass"), // window class name
szAppName, // window caption
WS_OVERLAPPEDWINDOW, // window style
100, // initial x position
100, // initial y position
630, // initial x size
520, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
if (!hwnd)
{
MessageBox(NULL, TEXT("CreateWindow fail!!"), szAppName, MB_ICONERROR);
return 0;
}
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
HWND hwnd;
HDC hdc, hdcmen;
BITMAP bm;
HBITMAP hbm;
PAINTSTRUCT ps;
//窗口过程处理函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HPEN hPen, hOldPen;
srand((unsigned)time(NULL));
int n = rand() % 10;
int m = rand() % 10;
const int a = n * 50 + 20;
const int b = m * 50 + 20;
static int xMonster = a, yMonster = b;
const int step = 50;
switch (message)
{
case WM_LBUTTONDOWN:
{
//声明当前点变量
POINT pt;
//声明矩形框变量
RECT ra;
//矩形框变量赋值
ra.left = xMonster;
ra.top =yMonster;
ra.right = bm.bmWidth;
ra.bottom = bm.bmHeight;
pt.x = LOWORD(lParam); //当前鼠标位置
pt.y = HIWORD(lParam); //当前鼠标位置
if (PtInRect(&ra, pt)) { //点击检测函数
break;
}
}break;
case WM_LBUTTONUP: // 鼠标左键,抬起时刷新
{
}
case WM_KEYDOWN:
switch (wParam) {
case VK_UP:
yMonster -= step;
break;
case VK_DOWN:
yMonster += step;
break;
case VK_LEFT:
xMonster -= step;
break;
case VK_RIGHT:
xMonster += step;
break;
}
InvalidateRect(hwnd, NULL, true);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hdcmen = CreateCompatibleDC(hdc);
hbm = (HBITMAP)LoadImage(
NULL,
TEXT("box1.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);
hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 0));
hOldPen = (HPEN)SelectObject(hdc, hPen);
for (int i = 0; i < 550; i += 50) {
MoveToEx(hdc, 20, 20+i, NULL);
LineTo(hdc, 520, 20+i);
}
for (int i = 0; i < 550; i += 50) {
MoveToEx(hdc, 20+i, 20 , NULL);
LineTo(hdc, 20+i, 520 );
}
DeleteObject(hbm);
DeleteDC(hdcmen);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}