WinMain.cpp
#include<windows.h>
#include "Mali.h"
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HINSTANCE hIns = 0;
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nShowCmd)
{
HWND hwnd = 0;
MSG msg;
WNDCLASSEX wndclass;
hIns = hInstance;
LPCSTR lClassName = "yue";
int nx = 0;
int ny = 0;
int nwidth = 0;
int nheigh = 0;
const CHAR* strTitle = "yue";
int pos_x = nx;
if (pos_x < 0) {
pos_x = 0;
}
int pos_y = ny;
if (pos_y < 0) {
pos_y = 0;
}
int nWidth = nwidth;
if (nWidth <= 0) {
nWidth = 500;
}
int nHeight = nheigh;
if (nHeight <= 0) {
nHeight = 500;
}
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.cbSize = sizeof(wndclass);
wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
wndclass.hCursor = LoadCursor(0, MAKEINTRESOURCE(IDC_ARROW));
wndclass.hIcon = 0;
wndclass.hIconSm = 0;
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = lClassName;
wndclass.lpszMenuName = 0;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
if (RegisterClassEx(&wndclass) == FALSE)
{
MessageBox(0, "注册失败", "提示", MB_OK);
return 0;
}
hwnd = CreateWindow(lClassName, strTitle, WS_OVERLAPPEDWINDOW, pos_x, pos_y, nWidth, nHeight, 0, 0, hInstance, 0);
if (hwnd == 0)
{
MessageBox(0, "创建失败", "提示", MB_OK);
return 0;
}
ShowWindow(hwnd, SW_SHOW);
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
CMali* pMali = NULL;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
if (pMali == NULL) {
pMali = new CMali;
pMali->InitPeople(hIns);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps = { 0 };
HDC hdc = ::BeginPaint(hwnd, &ps);
if (pMali != NULL)
{
pMali->ShowPeople(hdc);
}
::EndPaint(hwnd, &ps);
}
break;
case WM_KEYDOWN:
{
if (pMali != NULL)
{
pMali->MovePeople(wParam);
}
RECT rect = { 0,0,500,500 };
::InvalidateRect(hwnd,&rect,TRUE);
}
break;
case WM_CLOSE:
{
int res = ::MessageBox(NULL, "[WM_CLOSE] 是否确定退出", "提示", MB_OKCANCEL);
if (res == IDOK) {
}
else if (res == IDCANCEL) {
return 0;
}
else {
return 0;
}
}
break;
case WM_DESTROY:
{
if (pMali)
{
delete pMali;
pMali = NULL;
}
PostQuitMessage(0);
}
break;
case WM_QUIT:
{
::MessageBox(NULL, "WM_QUIT", "提示", MB_OK);
}
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Mali.h
#pragma once
#include <windows.h>
#include "resource.h"
class CMali
{
public:
int m_nx;
int m_ny;
int m_nDirection;
HBITMAP m_hUp;
HBITMAP m_hDown;
HBITMAP m_hRight;
HBITMAP m_hLeft;
CMali() {
m_nx = 0;
m_ny = 0;
m_nDirection = VK_LEFT;
m_hUp = NULL;
m_hDown = NULL;
m_hRight = NULL;
m_hLeft = NULL;
}
~CMali() {
::DeleteObject(m_hUp);
m_hUp = NULL;
::DeleteObject(m_hDown);
m_hDown = NULL;
::DeleteObject(m_hLeft);
m_hLeft = NULL;
::DeleteObject(m_hRight);
m_hRight = NULL;
}
void InitPeople(HINSTANCE hIns) {
m_nx = 200;
m_ny = 200;
m_hUp = ::LoadBitmap(hIns, MAKEINTRESOURCE(IDB_Up));
m_hDown = ::LoadBitmap(hIns, MAKEINTRESOURCE(IDB_Down));
m_hRight = ::LoadBitmap(hIns, MAKEINTRESOURCE(IDB_Right));
m_hLeft = ::LoadBitmap(hIns, MAKEINTRESOURCE(IDB_Left));
}
void ShowPeople(HDC hdc) {
HDC compDc = ::CreateCompatibleDC(hdc);
if (m_nDirection == VK_LEFT)
{
::SelectObject(compDc, m_hLeft);
}else if (m_nDirection == VK_RIGHT)
{
::SelectObject(compDc, m_hRight);
}
else if (m_nDirection == VK_UP)
{
::SelectObject(compDc, m_hUp);
}
else if (m_nDirection == VK_DOWN)
{
::SelectObject(compDc, m_hDown);
}
::BitBlt(hdc, m_nx, m_ny, 60, 60, compDc, 0, 0, SRCAND);
::DeleteDC(compDc);
compDc = NULL;
}
void MovePeople(int direct, int step = 4) {
switch (direct) {
case VK_LEFT:
{
if (m_nx - step <= 0)
{
m_nx = 0;
}
else {
m_nx -= step;
}
m_nDirection = VK_LEFT;
}
break;
case VK_RIGHT:
{
if (m_nx + step >= 440)
{
m_nx = 440;
}
else {
m_nx += step;
}
m_nDirection = VK_RIGHT;
}
break;
case VK_UP:
{
if (m_ny - step <= 0)
{
m_ny = 0;
}
else {
m_ny -= step;
}
m_nDirection = VK_UP;
}
break;
case VK_DOWN:
{
if (m_ny + step >= 440)
{
m_ny = 440;
}
else {
m_ny += step;
}
m_nDirection = VK_DOWN;
}
break;
}
}
protected:
private:
};