window程序设计---编写WM消息制贪吃蛇的代码与经过

来源:互联网 发布:知乎英国金融专业大学 编辑:程序博客网 时间:2024/05/17 06:00
// win1.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "win1.h"
#define MAX_LOADSTRING 100
#define MAX_wide  840
#define MAX_long  600
// 全局变量:
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    // TODO: 在此放置代码。
 hbrush1 = CreateSolidBrush(Color_hbrush_1);   //创建蛇体颜色画刷
 hbrush2 = CreateSolidBrush(Color_hbrush_2);   //创建白色画刷
    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_WIN1, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);
    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }
    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN1));
    MSG msg;
    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int) msg.wParam;
}

//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;
 static TCHAR szAppName[] = TEXT("Checker3");
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN1));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_WIN1);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    return RegisterClassExW(&wcex);
}
//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中
   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT, 840, 600, nullptr, nullptr, hInstance, nullptr);
   if (!hWnd)
   {
      return FALSE;
   }
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   return TRUE;
}
//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的:    处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 PAINTSTRUCT ps;
 RECT rect;
 static POINT point;
 HDC hdc;
 static POINT pt[10000];
 static int iCount;
 static int she_head = 0;      //蛇移动状态
    switch (message)
    {
 case WM_CREATE:     //第一个消息
  SetTimer(hWnd, Time_1, Rate, NULL);    //设置时钟,1000毫秒
  point = { 300,300 };
  break;
 case WM_TIMER:     //定时器
  working_she_move(hWnd, she_head);
  break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
   default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
 case WM_CHAR:
  switch (wParam)
  {
  case ' ':
   she_head = she_wait;
   break;
  default:
   break;
  }
  break;
 case WM_KEYDOWN:
  switch (wParam)
  {
  case VK_RIGHT:
   she_head = she_right;
   break;
  case VK_LEFT:
   she_head = she_left;
   break;
  case VK_UP:
   she_head = she_up;
   break;
  case VK_DOWN:
   she_head = she_down;
   break;
  default:
   break;
  }
  break;
 case WM_LBUTTONDOWN:      //鼠标左键按下     
  iCount = 0;
  InvalidateRect(hWnd, NULL, FALSE);
  return 0;
 case WM_RBUTTONUP:       //鼠标右键按起
  InvalidateRect(hWnd, NULL, TRUE);  //鼠标按起,清除绘图
  return 0;
 case WM_MOUSEMOVE:       //鼠标移动
  if (wParam & MK_LBUTTON && iCount < 10000)
  {
   pt[iCount].x = LOWORD(lParam);
   pt[iCount++].y = HIWORD(lParam);
   //在鼠标经过的地方画下一点
   //hdc = GetDC(hWnd);
   //SetPixel(hdc, LOWORD(lParam), HIWORD(lParam), 0);
   //ReleaseDC(hWnd, hdc);
  }
  return 0;
 case WM_LBUTTONUP:       //鼠标左键按起
  InvalidateRect(hWnd, NULL, FALSE);
  return 0;
    case WM_PAINT:
        {
            hdc = BeginPaint(hWnd, &ps);  //获取设备环境句柄
            //TODO: 在此处添加使用 hdc 的任何绘图代码...
   GetClientRect(hWnd, &rect);
   working(hWnd, hdc, rect);                 //绘制图形的具体代码
            EndPaint(hWnd, &ps);    //释放设备环境句柄
   working_sheti_Initialize(hWnd);
        }
        break;
    case WM_DESTROY:
  KillTimer(hWnd, Time_1);    //结束时钟
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;
    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}




win1.h的代码

#pragma once

#include "resource.h"

//自定义函数声明
//初始化蛇体
void working_sheti_Initialize(RECT);   //初始化蛇体
void working_she_move(HWND,  POINT);     //蛇体的移动 绘图---- 蛇体的绘制

//定义
//
#define Time_1 1        //定时器1
#define Rate 1000       //初始速度1000毫秒
#define Color_hbrush_1 RGB(10,255,0)   //画刷颜色1
#define Color_hbrush_2 RGB(255,255,255)    //白色画刷

//定义射移动的方向选择
#define she_left 1       
#define she_right 2
#define she_up  3
#define she_down 4
#define she_wait 0

//变量
//
static int she_wide,she_long;
HBRUSH hbrush1,hbrush2;

//绘制图形
//
//目的:绘制处理
//

void working(HWND hWnd, HDC hdc, RECT rect)
{
 she_wide = rect.right / 84;
 she_long = rect.bottom / 60;
 MoveToEx(hdc, rect.right / 14 * 10, rect.top, NULL);
 LineTo(hdc, rect.right / 14 * 10, rect.bottom);

}


//定义链表来存蛇体
//
typedef struct SHE {
 struct SHE *link;
 POINT struct_point;
}she;
static she *head;
#define SHE_MALLOC (she *) malloc(sizeof(she))   //给链表分配空间
static int she_number = 1;      //蛇的长度


//初始化蛇体
//初始化蛇体
void working_sheti_Initialize(HWND hWnd)
{
 RECT rect;
 HDC  hdc;

 GetClientRect(hWnd, &rect);
 head  = SHE_MALLOC;
 head->struct_point.x = rect.right/84*30;
 head->struct_point.y = rect.bottom/60*30;
 head->link = NULL;


 /*hdc = GetDC(hWnd);
 SelectObject(hdc, GetStockObject(NULL_PEN));
 SelectObject(hdc, hbrush1);
 Rectangle(hdc, head->struct_point.x, head->struct_point.y,
  head->struct_point.x + she_wide, head->struct_point.y + she_long);
 ReleaseDC(hWnd, hdc);*/
}


//绘图---- 蛇体的绘制
//
//蛇体的移动
//
//
void working_she_move(HWND hWnd, int she_head)

 HDC hdc;
 static POINT point;
 static she *p1,*p2;

 point = { 0,0 };
 switch (she_head)
 {
 case she_left:
  point.x = -10;
  break;
 case she_right:
  point.x = 10;
  break;
 case she_up:
  point.y = -10;
  break;
 case she_down:
  point.y = 10;
  break;
 case she_wait:
  break;
 default:
  break;
 }
 if(she_head)
 {
  p1 = SHE_MALLOC;
  p1->struct_point.x = head->struct_point.x+point.x;
  p1->struct_point.y = head->struct_point.y + point.y;
  p1->link = head;
  head = p1;

  hdc = GetDC(hWnd);
  SelectObject(hdc, GetStockObject(NULL_PEN));
  SelectObject(hdc, hbrush1);
  Rectangle(hdc, head->struct_point.x, head->struct_point.y,
   head->struct_point.x + she_wide, head->struct_point.y + she_long);

  while (p1->link)
  {
   p2 = p1;
   p1 = p1->link;
  }
  SelectObject(hdc, hbrush2);
  Rectangle(hdc, p1->struct_point.x, p1->struct_point.y,
   p1->struct_point.x + she_wide, p1->struct_point.y + she_long);
  ReleaseDC(hWnd, hdc);
  p2->link = NULL;
  free(p1);
 }
 
 ////测试用
}





遇到了很多问题

贪吃蛇现在已经写到一节左右上下 的运动,其他的明天在改善


0 0
原创粉丝点击