AUTO_REGISTER_UMYMODULE(_T("@XXX"), xyzClass)

REGISTER_UMSWNDCLASS(_T("@XXX"), _T("XXX"), _T("XXX.png"), WndClassManager::TYPE_XXX)


//自动注册面板宏,放在CPP中

#define AUTO_REGISTER_UMYMODULE(__name__, __class__)\

MyClass* Create##__name__()\

{ return new __class__; } \

void Delete##__name__(MyClass*& pMyClass)\

{ DELETE_PTR(pMyClass); }\

const BOOL bRegistered##__class__ = uwpMyRegister(__name__, Create##__name__, Delete##__name__, 0, 0);


typedef MyClass* (*_pCreateMyWindow)(LPCTSTR clsName);

typedef void (*_pDeleteMyWindow)(MyClass*& pMyClass);


BOOL MY_WND_API uwpMyRegister(LPCTSTR clsName, _pCreateMyWindow procCreate, _pDeleteMyWindow procDel, long defWidth, long defHeight)

{

WNDCLASS wc={0};

if(!GetClassInfo(uwhInstance,clsName,&wc))

{

memset(&wc,0,sizeof(WNDCLASS));

wc.hCursor= ::LoadCursor(NULL,IDC_ARROW);

wc.hInstance= uwhInstance;

//LRESULT CALLBACK MyWindowMain(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)

wc.lpfnWndProc= MyWindowMain;

wc.lpszClassName= clsName;

RegisterClass(&wc);

}

return(clsMaster.Register(clsName, procCreate, procDel, defWidth, defHeight));

}


BOOL MY_WND_API uwpMyDeleteRegister(_pDeleteMyWindow pProcDelete)

{

clsMaster.RegisterDeleteMyWindow(pProcDelete);

return TRUE;

}


void MY_WND_API uwpGetDefaultWH(LPCTSTR clsName,long&defWidth,long&defHeight)

{

clsMaster.GetDefaultWH(clsName,defWidth,defHeight);

}


//注册类的自我管理

class __declspec(novtable) class_master

{

typedef struct tagItem

{

TCHARclsName[32];

longdefWidth;

longdefHeight;

_pCreateMyWindow procCreate;

_pDeleteMyWindow procDel;

tagItem*pNext;

}item,*pitem;

public:

BOOL Register(LPCTSTR clsName, _pCreateMyWindow procCreate, _pDeleteMyWindow procDel, long defWidth, long defHeight)

{

pitem pItem=new item;

if(!pItem) return(FALSE);

memset(pItem,0,sizeof(item));

lstrcpy(pItem->clsName,clsName);

pItem->defWidth = defWidth;

pItem->defHeight = defHeight;

pItem->procCreate = procCreate;

pItem->procDel = procDel;

pItem->pNext = __item__;

__item__ = pItem;

return(TRUE);

}


_pCreateMyWindow GetProc(LPCTSTR clsName)

{

for(pitem pItem=__item__; pItem; pItem=pItem->pNext)

{

if(!lstrcmpi(pItem->clsName,clsName))

return(pItem->procCreate);

}

return(NULL);

}


inline void RegisterDeleteMyWindow(_pDeleteMyWindow pProcDelete)

{

m_procDelete = pProcDelete;

}

inline _pDeleteMyWindow GetProcDelete() const

{

return m_procDelete;

}


_pDeleteMyWindow GetProcDelete(LPCTSTR clsName)

{

for(pitem pItem=__item__; pItem; pItem=pItem->pNext)

{

if(!lstrcmpi(pItem->clsName,clsName))

{

if (!pItem->procDel) break;

return(pItem->procDel);

}

}

return m_procDelete;

}


void GetDefaultWH(LPCTSTR clsName,long&defWidth,long&defHeight)

{

defWidth = defHeight = 0;

for(pitem pItem=__item__;pItem;pItem=pItem->pNext)

{

if(!lstrcmpi(pItem->clsName,clsName))

{

defWidth = pItem->defWidth;

defHeight = pItem->defHeight;

break;

}

}

}

class_master(void):__item__(NULL),m_procDelete(NULL)

{

}

~class_master(void)

{

for(pitem pNext=NULL;__item__;__item__=pNext)

{

pNext = __item__->pNext;

delete __item__;

}

}

pitem __item__;//指标链表头节点

private:

_pDeleteMyWindowm_procDelete;

}clsMaster;




static MyClass*__list_window__ = NULL;


//主回调函数--控制自注册类的回调函数

LRESULT CALLBACK MyWindowMain(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)

{

MyClass*pWindow=(MyClass*)GetWindowLong(hWnd,GWL_USERDATA);

switch(uMsg)

{

caseWM_ERASEBKGND:

if(pWindow && pWindow->drawType) return(1);

break;

caseWM_CREATE:

{

TCHAR clsName[32]={0};

::GetClassName(hWnd,clsName,32);

if(pWindow=clsMaster.GetProc(clsName)(clsName))

{

pWindow->_selfNew = TRUE;

pWindow->wndNext = __list_window__;

__list_window__ = pWindow;


LPCREATESTRUCT lpStruct=(LPCREATESTRUCT)lParam;

pWindow->m_hWnd = hWnd;

pWindow->m_hParent = lpStruct->hwndParent;

SetWindowLong(hWnd,GWL_USERDATA,(long)pWindow);


LRESULT rtCode=DefWindowProc(hWnd,uMsg,wParam,lParam);

pWindow->OnCreate();

if(__createByLoading__)

pWindow->_isVisible = ::IsWindowVisible(hWnd);

else

{

pWindow->OnEnvReset();

SetTimer(hWnd,0x8866,100,NULL);

PostMessage(hWnd,WM_TIMER,0x8866,0);

}

return(rtCode);

}

}

break;

caseWM_SIZE:

if(pWindow)

{

LRESULT lResult=DefWindowProc(hWnd,uMsg,wParam,lParam);

GetClientRect(hWnd,&pWindow->rtWindow);

pWindow->OnSize(pWindow->ObjWidth=pWindow->rtWindow.right-pWindow->rtWindow.left,

pWindow->ObjHeight=pWindow->rtWindow.bottom-pWindow->rtWindow.top);

return(lResult);

}

break;

caseWM_NCDESTROY:

pWindow->OnNcDestory();

MyClass*pItem=NULL,*pLast=NULL;

for(pItem=__list_window__;pItem;pItem=(pLast=pItem)->wndNext)

{

if(pItem==pWindow)

{

if(pLast) pLast->wndNext = pItem->wndNext;

else __list_window__ = pItem->wndNext;

break;

}

}

SetWindowLong(hWnd,GWL_USERDATA,0);

if(pWindow&&pWindow->_selfNew)

{

TCHAR clsName[32]={0};

::GetClassName(hWnd,clsName,32);

_pDeleteMyWindow pProcDelete=clsMaster.GetProcDelete(clsName);

if (pProcDelete)

pProcDelete(pWindow);

else

delete pWindow;

}

return (DefWindowProc(hWnd,uMsg,wParam,lParam));

}

return (pWindow?pWindow->OnMessage(uMsg,wParam,lParam):DefWindowProc(hWnd,uMsg,wParam,lParam));

}


//调用下面函数,后会发送MyWindowMain的WM_CREATE消息(MyWindowMain过程已经注册)

HWND _wndModule = ::CreateWindowEx(0,

_T("@XXX"),_T("XXX"),WS_CHILD,_rect.left,_rect.top,

_rect.right-_rect.left,_rect.bottom-_rect.top,hParent,NULL,uwhInstance,NULL);



//销毁窗口

if(_wndModule && IsWindow(_wndModule))

{

::DestroyWindow(_wndModule);

_wndModule = NULL;

}