关于win32的C++ 类封装

来源:互联网 发布:sai绘画软件官网 编辑:程序博客网 时间:2024/06/06 07:39

为了方便运行win32程序,进行类封装是必要,下面介绍App类的基本实现:

首先包含文件:

         #include <windows.h>
         #include <stdio.h>
         #include <stdlib.h>

        #pragma comment(lib,"winmm.lib")

然后写App类:

    #define WINCLASSNAME "Game"                                                                                                                        //应用程序名称
    LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );                //声明窗口函数

     class MyEngine_App
     {
     private:
         WNDCLASSEX wc; 
          MSG        m_msg;
            int           m_width;
            int           m_height;
        HWND       m_hWnd;

    public:

              MyEngine_App();
             ~MyEngine_App();
 
    bool RegWnd(HINSTANCE hInst,UINT Icon=0,UINT cur=0);                                      
//注册窗口
    bool CreateWnd(LPCTSTR title,HINSTANCE PrevInst,bool isFull);                          
//创建窗口
    bool Run();                                                                                                                         
//消息循环
    void DeleteWnd();                                                                                                            
//结束应用程序
};

 

//-----------------------------------------------------------------------
//注册窗口
//----------------------------------------------------------------------

bool MyEngine_App::RegWnd(HINSTANCE hInst,UINT Icon,UINT cur)
{
   // 定义一个Windows类,指定消息的处理函数为MsgProc
 wc.cbSize                 =sizeof(WNDCLASSEX);
 wc.style                     =CS_CLASSDC;
 wc.lpfnWndProc       =MsgProc;
 wc.cbClsExtra          =0;
 wc.cbWndExtra       =0;
 wc.hInstance           =hInst;
 wc.lpszClassName=WINCLASSNAME;
 wc.hbrBackground=CreateSolidBrush(RGB(0,0,255));
 if(Icon)
 {
         wc.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(Icon));                 
//标题栏上的图标
         wc.hIconSm=LoadIcon(hInst,MAKEINTRESOURCE(Icon));           
//应用程序图标
 }
 if(cur)  wc.hCursor=LoadCursor(hInst,MAKEINTRESOURCE(cur));      
//定义鼠标图标
 
    // 注册这个窗口类
    RegisterClassEx( &wc );
    return true;

}

 

//-------------------------------------------------------------------------------------------
//创建窗口
//-------------------------------------------------------------------------------------------

bool MyEngine_App::CreateWnd(LPCTSTR title,HINSTANCE PrevInst,bool isFull)
{
 if(PrevInst) return false;                 
//判断是否已经有相同的应用实体在运行
 int m_x,m_y;
 if(isFull)                                           
//全屏显示 
 {
    m_x=0;
    m_y=0;
    m_width=GetSystemMetrics(SM_CXSCREEN);
    m_height=GetSystemMetrics(SM_CYSCREEN);
 }
 else
 {
 m_x=20;
 m_y=20;
 m_width=900;
 m_height=620;
 }
    // 创建应用程序窗口
 m_hWnd = CreateWindow( WINCLASSNAME,title,
                              WS_OVERLAPPEDWINDOW, m_x, m_y, m_width, m_height,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL);
 if(!m_hWnd)  return false;
 
 // 显示窗口
     ShowWindow( m_hWnd, SW_SHOWDEFAULT );
     UpdateWindow( m_hWnd );
  return true;
}

 

 


//----------------------------------------------------------------------------------------------
//运行,消息循环
//----------------------------------------------------------------------------------------------
bool MyEngine_App::Run()
{
      
        if( m_msg.message != WM_QUIT)
        {
   if(PeekMessage( &m_msg, NULL, 0U, 0U, PM_REMOVE ))
   {
    TranslateMessage( &m_msg );
    DispatchMessage( &m_msg );
   }
   return true;

        }
  else return false;

 
}

//-------------------------------------------------------------------------------

 // 注销窗口类

//--------------------------------------------------------------------------------
void MyEngine_App::DeleteWnd()


     UnregisterClass( WINCLASSNAME, wc.hInstance );
}

 

以上App类基本封装完成。