关于win32的C++ 类封装

本文介绍了一种针对Win32程序的封装方法,通过创建App类来简化窗口管理和消息循环的过程。该类实现了窗口注册、创建及消息处理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了方便运行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类基本封装完成。

 

使用C++代码封装win32操作, 与MFC相似,对于学习SDK与C++是巨好的参考 Tutorials Menu of tutorials Tutorial 1: The Simplest Window Tutorial 2: Using Classes and Inheritance Tutorial 3: Using Messages to Create a Scribble Window Tutorial 4: Repainting the Window Tutorial 5: Wrapping a Frame around our Scribble Window Tutorial 6: Customising Window Creation Tutorial 7: Customising the Toolbar Tutorial 8: Loading and Saving Files Tutorial 9: Printing Tutorial 10: Finishing Touches Tutorial 1: The Simplest Window The following code uses Win32++ to create a window. This is all the code you need (in combination with Win32++) to create and display a simple window. Note that in order to add the Win32++ code to our program, we use an #include statement as shown below. #include "../Win32++/Wincore.h" INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { //Start Win32++ CWinApp MyApp; //Create a CWnd object CWnd MyWindow; //Create (and display) the window MyWindow.Create(); //Run the application return MyApp.Run(); } This program has four key steps: Start Win32++. We do this here by creating a CWinApp object called MyApp. Create a CWnd object called MyWindow. Create a default window by calling the Create function. Start the message loop, by calling the Run function. If you compile and run this program, you'll find that the application doesn't end when the window is closed. This is behaviour is normal. An illustration of how to use messages to control the windows behaviour (including closing the application) will be left until tutorial 3.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值