WinApp.h
============================================================================================================
#include <windows.h>/*--------------------------------------------------------------------------------------------------
*
* ClassName: cBaseGui
*   Comment: all window gui base class
*
--------------------------------------------------------------------------------------------------*/
class cBaseGui
{
public:
cBaseGui *pPrevObj;
cBaseGui *pNextObj;
HWND hWnd;

cBaseGui(void);

virtual void onCreate(void);
virtual void onDestroy(void);
virtual void onClick(void);
};/*--------------------------------------------------------------------------------------------------
*
* ClassName: cBaseWindow
*   Comment:
*
--------------------------------------------------------------------------------------------------*/
class cBaseWindow:public cBaseGui
{
private:
static bool reCheck;

static LRESULT CALLBACK WindowProc(HWND hWnd,UINT uMessageCode,WPARAM wParam,LPARAM lParam);
public:
LPCSTR lpcstrCaption;

cBaseWindow(void);
~cBaseWindow(void);

void Show(void);
void Hide(void);
HWND Create(LPCSTR lpcstrNewCaption,
int nXpos,
int nYpos,
int nWidth,
int nHeight,
HWND  hWndParent);

virtual void onCreate(void);
virtual void onDestroy(void);
virtual void onClick(void);
};/*--------------------------------------------------------------------------------------------------
*
* ClassName: cWindows
*   Comment:
*
--------------------------------------------------------------------------------------------------*/
class cWindows
{
private:
cBaseGui* pHomeWindow;
cBaseGui* pTialWindow;
public:
int nCount;

cWindows(void);
~cWindows(void);

void  AppendWindow(cBaseWindow* pWindow);
cBaseGui*  GetWndClsObjFromHwnd(HWND hWnd);
void RemoveWindow(cBaseWindow* pWindow);
};/*--------------------------------------------------------------------------------------------------
*
* ClassName: cApplication
*   Comment:
*
--------------------------------------------------------------------------------------------------*/
class cApplication
{
public:
static bool reCheck;
HINSTANCE hThisInstance;
HINSTANCE hPrevInstance;
LPSTR lpstrCommandLine;
int nShowCmd;
cWindows* pWindows; cApplication(void);
~cApplication(void);

virtual int InitApplication(void);
virtual int MessageLoop(void);
};
/*------------------------------------------------------------------------------------------------*/
WinApp.cpp
===================================================================================================

解决方案 »

  1.   

    WinApp.cpp 
    ===================================================================================================
    #include "WinApp.h"bool cBaseWindow::reCheck = false;
    bool cApplication::reCheck = false;cApplication* theApplication;/*--------------------------------------------------------------------------------------------------
    *
    * ClassName: cBaseGui
    *   Comment: all window gui base class
    *
    --------------------------------------------------------------------------------------------------*/
    cBaseGui::cBaseGui()
    {
    this->pNextObj = NULL;
    this->pPrevObj = NULL;
    }void cBaseGui::onCreate(void)
    {
    }void cBaseGui::onDestroy(void)
    {
    }void cBaseGui::onClick(void)
    {
    }/*--------------------------------------------------------------------------------------------------
    *
    * ClassName: cBaseWindow
    *   Comment:
    *
    --------------------------------------------------------------------------------------------------*/
    cBaseWindow::cBaseWindow(void)
    {
    WNDCLASSEX WndClEx; if(!cBaseWindow::reCheck)
    {
    WndClEx.cbSize = sizeof(WNDCLASSEX);
    WndClEx.cbClsExtra = 0;
    WndClEx.cbWndExtra = 0;
    WndClEx.style = CS_HREDRAW|CS_VREDRAW|CS_BYTEALIGNWINDOW;
    WndClEx.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
    WndClEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    WndClEx.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClEx.hInstance = theApplication->hThisInstance;
    WndClEx.lpszMenuName = NULL;
    WndClEx.lpszClassName = "WndFrame_BlueMouse";
    WndClEx.lpfnWndProc = this->WindowProc;
    RegisterClassEx(&WndClEx);
    cBaseWindow::reCheck = false;
    }
    theApplication->pWindows->AppendWindow(this);
    }cBaseWindow::~cBaseWindow(void)
    {
    DestroyWindow(this->hWnd);
    theApplication->pWindows->RemoveWindow(this);
    }HWND cBaseWindow::Create(LPCSTR lpcstrNewCaption,
     int nXpos,
     int nYpos,
     int nWidth,
     int nHeight,
     HWND hWndParent)
    {
    this->hWnd = CreateWindowEx(0,
    "WndFrame_BlueMouse",
    lpcstrNewCaption,
    WS_OVERLAPPEDWINDOW,
    nXpos,
    nYpos,
    nWidth,
    nHeight,
    hWndParent,
    NULL,
    theApplication->hThisInstance,
    this
       );
    }LRESULT CALLBACK cBaseWindow::WindowProc(HWND hWnd,UINT uMessageCode,WPARAM wParam,LPARAM lParam)
    {
    cBaseWindow *pWindow; if(uMessageCode==WM_NCCREATE)
    {
    pWindow = (cBaseWindow*)((LPCREATESTRUCT) lParam)->lpCreateParams;
    SetWindowLong(hWnd,GWL_USERDATA,(long)pWindow);
    }
    else
    {
    pWindow = (cBaseWindow*)GetWindowLong(hWnd,GWL_USERDATA);
    }    switch(uMessageCode)
        {
         case WM_CREATE:
    pWindow->onCreate();
    break;
    case WM_DESTROY:
    pWindow->onDestroy();
    break;
    case WM_LBUTTONUP:
    pWindow->onClick();
    break;
            default:
                return DefWindowProc (hWnd, uMessageCode, wParam, lParam);
        }
        return 0;
    }void cBaseWindow::Show(void)
    {
    ShowWindow(this->hWnd,SW_SHOWDEFAULT);
    }void cBaseWindow::Hide(void)
    {
    ShowWindow(this->hWnd,SW_HIDE);
    }void cBaseWindow::onCreate(void)
    {
    }void cBaseWindow::onDestroy(void)
    {
    }void cBaseWindow::onClick(void)
    {
    }/*--------------------------------------------------------------------------------------------------
    *
    * ClassName: cWindows
    *   Comment:
    *
    --------------------------------------------------------------------------------------------------*/
    cWindows::cWindows(void)
    {
    this->pHomeWindow = NULL;
    this->pTialWindow = NULL;
    }cWindows::~cWindows(void)
    {
    cBaseGui* pWindow;
    cBaseGui* pNextWindow;

    pWindow = this->pHomeWindow;
    while(pWindow!=NULL)
    {
    pNextWindow = pWindow->pNextObj;
    this->RemoveWindow((cBaseWindow*)pWindow);
    pWindow = pNextWindow;
    }
    }void cWindows::AppendWindow(cBaseWindow* pWindow)
    {
    if(this->nCount<=0)
    {
    this->pHomeWindow = pWindow;
    this->pTialWindow = pWindow;
    pWindow->pPrevObj = NULL;
    pWindow->pNextObj = NULL;
    }
    else
    {
    pWindow->pPrevObj = this->pTialWindow;
    pWindow->pNextObj = NULL;
    this->pTialWindow = pWindow;
    }
    this->nCount++;
    }cBaseGui* cWindows::GetWndClsObjFromHwnd(HWND hWnd)
    {
    cBaseGui* pThisWindow; pThisWindow = this->pHomeWindow;
    while(pThisWindow!=NULL)
    {
    if(pThisWindow->hWnd==hWnd)
    {
    break;
    }
    pThisWindow = pThisWindow->pNextObj;
    }
    return pThisWindow;
    }void cWindows::RemoveWindow(cBaseWindow* pWindow)
    {
    if(pWindow->pPrevObj!=NULL)
    {
    if(pWindow->pNextObj!=NULL)
    {
    pWindow->pPrevObj->pNextObj = pWindow->pNextObj;
    }
    else
    {
    pWindow->pPrevObj->pNextObj = NULL;
    this->pTialWindow = pWindow->pPrevObj;
    }
    }
    else
    {
    if(pWindow->pNextObj!=NULL)
    {
    pWindow->pNextObj->pPrevObj = NULL;
    this->pHomeWindow = pWindow->pNextObj;
    }
    else
    {
    this->pHomeWindow = NULL;
    this->pTialWindow = NULL;
    }
    }
    this->nCount--;
    }/*--------------------------------------------------------------------------------------------------
    *
    * ClassName: cApplication
    *   Comment:
    *
    --------------------------------------------------------------------------------------------------*/
    cApplication::cApplication(void)
    {
    if(cApplication::reCheck)
    {
    MessageBox(NULL,"Re create object from capplicaton class.","error",MB_OK|MB_ICONERROR);
    exit(-1);
    }
    else
    {
    theApplication  = this;
    theApplication->reCheck = true;
    this->pWindows = new cWindows(); }
    }
    cApplication::~cApplication(void)
    {
    delete this->pWindows;
    }int cApplication::InitApplication(void)
    {
    }
    int cApplication::MessageLoop(void)
    {
    MSG Messages;    while(GetMessage(&Messages,NULL,0,0))
        {
            TranslateMessage(&Messages);
            DispatchMessage(&Messages);
        }
        return Messages.wParam;
    }/*--------------------------------------------------------------------------------------------------
    *
    * ProcName: WinMain
    *  Comment: windows gui program main proc
    *
    --------------------------------------------------------------------------------------------------*/
    int WINAPI WinMain(HINSTANCE  hThisInstance,
       HINSTANCE  hPrevInstance,
       LPSTR  lpstrCommandLine,
       int  nShowCmd)
    {
    int Retval; theApplication->hThisInstance = hThisInstance;
    theApplication->hPrevInstance = hPrevInstance;
    theApplication->lpstrCommandLine = lpstrCommandLine;
    theApplication->nShowCmd = nShowCmd;
    Retval = theApplication->InitApplication(); if(theApplication->pWindows->nCount>0)
    {
    Retval = theApplication->MessageLoop();
    delete  theApplication;
    }
    return Retval;
    }
    /*------------------------------------------------------------------------------------------------*/
      

  2.   

    main.h
    =====================================================
    #include "WinApp.h"class MyApp:public cApplication
    {
    public:
    int InitApplication(void);
    };class MainWindow:public cBaseWindow
    {
    public:
    void onCreate(void);
    void onDestroy(void);
    void onClick(void);
    };
    main.cpp
    ====================================================#include "main.h"MyApp theApp;int MyApp::InitApplication(void)
    {
    MainWindow* pMainWindow;

    MessageBox(NULL,
       "This is a 'InitApplication' method from 'MyApp' class.",
       "Information",
       MB_OK|MB_ICONINFORMATION
      );
    pMainWindow = new MainWindow();
    pMainWindow->Create("MainWindow caption.",CW_USEDEFAULT,CW_USEDEFAULT,500,400,HWND_DESKTOP);
    pMainWindow->Show();
    }void MainWindow::onCreate(void)
    {
    MessageBox(NULL,
       "This is a 'onCreate' event from 'MainWindow' class.",
       "Information",
       MB_OK|MB_ICONINFORMATION
      );
    }void MainWindow::onDestroy(void)
    {
    MessageBox(NULL,
       "This is a 'onDestroy' event from 'MainWindow' class.",
       "Information",
       MB_OK|MB_ICONINFORMATION
      );
    PostQuitMessage(0);
    }void MainWindow::onClick(void)
    {
    MessageBoxA(NULL,
    "This is a 'onClick' event from 'MainWindow' class.",
    "Information",
    MB_OK|MB_ICONINFORMATION
       );
    }当程序执行到 MainWindow::onDestroy 中的 PostQuitMessage(0)  出现了非法操作!不知道是哪里的问题!
      

  3.   

    没大看明白,
    PostQuitMessage之前,最好把该释放的资源都释放了,特别是跟窗口有关的.