Leo

生活不止苟且 还有诗和远方

C++ WIN32(游戏壳)

.h

#pragma once
#include<Windows.h>
#ifndef _GAMECTRL_H_
#define _GAMECTRL_H_    

class CGameCtrl
{

public:         

    virtual void OnCreateGame(){}                       // WM_CREATE    
    virtual void OnGameDraw  (){}                       // WM_PAINT
    virtual void OnGameRun   (WPARAM nTimeId){}         // WM_TIMER 需要判断哪个定时器,传参数wParam [in]The timer identifier.
            //键盘
    virtual void OnKeyDown(WPARAM nKey){}               // WM_KEYDOWN
    virtual void OnKeyUp(WPARAM nKey){}                 // WM_KEYUP
            //鼠标
    virtual void OnLButtonDown(POINT point){}           // WM_LBUTTONDOWN   
    virtual void OnLButtonUp(POINT point){}             // WM_LBUTTONUP
    virtual void OnMouseMove(POINT point){}             // WM_MOUSEMOVE
};
#endif //_GAMECTRL_H_

.cpp

#include<windows.h>
#include"GameCtrl.h"
//#include"SnakeCtrl.h"

CGameCtrl *pCtrl=NULL;
//CGameCtrl *pCtrl=new CSnakeCtrl;
LRESULT CALLBACK MyWinMain(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)//回调函数(消息的)
{
    switch(uMsg)
    {
    case WM_CREATE:
        pCtrl->OnCreateGame();
        break;
    case WM_PAINT:
        pCtrl->OnGameDraw();
        break;
    case WM_TIMER:
        pCtrl->OnGameRun(wParam);
        break;
    case WM_KEYDOWN:
        pCtrl->OnKeyDown(wParam);
        break;
    case WM_KEYUP:
        pCtrl->OnKeyUp(wParam);
        break;
    case WM_LBUTTONDOWN:
        {
            POINT point;
            point.x=LOWORD(lParam);
            point.y=HIWORD(lParam);
            pCtrl->OnLButtonDown(point);
        }
        break;
    case WM_LBUTTONUP:
        {
            POINT point;
            point.x=LOWORD(lParam);
            point.y=HIWORD(lParam);
            pCtrl->OnLButtonUp(point);
        }
        break;
    case WM_MOUSEMOVE:
        {
            POINT point;
            point.x=LOWORD(lParam);
            point.y=HIWORD(lParam);
            pCtrl->OnMouseMove(point);
        }
        break;

    case WM_CLOSE:          
        {
        ::PostQuitMessage(0);
        }

    }
    return  DefWindowProc(hWnd,uMsg,wParam,lParam); //返回消息的默认处理
}

int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
    //设置画刷
    HBRUSH hBrush=::CreateSolidBrush(RGB(62,71,98));

    //设计窗口
    WNDCLASSEX wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbSize=sizeof(wndclass);
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=hBrush;
    wndclass.hCursor=0;
    wndclass.hIcon=0;
    wndclass.hIconSm=0;
    wndclass.lpfnWndProc=MyWinMain;
    wndclass.lpszClassName="Leo";   //注册窗口时候要的名字,作为参数   
    wndclass.lpszMenuName=0;        //菜单名字
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.hInstance=hInstance;

    //注册窗口

    if(RegisterClassEx(&wndclass)== false)
    {
        MessageBox(NULL,"注册失败","提示",MB_OK);
        return 0;
    }
    //创建
    HWND hWnd=::CreateWindow("Leo","Small Test",WS_OVERLAPPEDWINDOW,0,200,500,500,NULL,NULL,hInstance,NULL);


    //显示窗口

    ShowWindow(hWnd,nCmdShow);

    //消息循环
    MSG msg;
    while(::GetMessage(&msg,0,0,0))
    {

        ::TranslateMessage(&msg);   //将得到的消息翻译
        ::DispatchMessage(&msg);    //翻译来的消息发送

    }

    return 0;
}
阅读更多
版权声明:18年毕业,在读硕士,自学c/c++ 邮箱:Lovesing@163.com https://blog.csdn.net/Loversing/article/details/54429960
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭