www.pudn.com > cguitest7.15.rar > TREE.H
#include "stdafx.h" ////////////////////////////////////////////////////CList define////////////////////////////////////////////////////////// templateclass Cvector_10{ protected: struct CNode{ //list node type Type elem; //element CNode* next; //next pointer }; int nsize; CNode* head; CNode* tail; public: Cvector_10(){ nsize = 0; head = NULL; tail = NULL; } ~Cvector_10(){ while(this->size()){ this->erase(0); } } //------------------------------------------------------------------------------------------------------------------------ void push_back(const Type& elem){ CNode* p = new CNode; //create a home node for the elem p->elem = elem; p->next = NULL; if(tail){ //not empty tail->next = p; //add to the end of the list, become the new tail }else{ //empty list head = p; } tail = p; nsize++; } //------------------------------------------------------------------------------------------------------------------------- void erase(int nindex){ if(nindex>=0 && nindex next; delete q; }else{ CNode* p = head; for(int i=0; i next; } if(p->next==tail){ //target node is tail delete tail; tail = p; }else{ //target node is between head and tail CNode* r = p->next; p->next = p->next->next; delete r; } } nsize--; } } //------------------------------------------------------------------------------------------------------------------------- void erase(const Type& elem){ if( head && head->elem==elem){ //delete head node if(head==tail){ //has only one node delete head; head = NULL; tail = NULL; }else{ //has at least two nodes CNode* q = head; head = head->next; delete q; } nsize--; return; } CNode* p = NULL; CNode* q = NULL; //record the p's forhead for(p=head; p!=tail; q=p, p=p->next ){ if(p->elem==elem){ //find the target node q->next = p->next; //target is between head and tail delete p; nsize--; return; } } if(p && p==tail && p->elem==elem){ //delete tail node tail = q; delete p; nsize--; } } //------------------------------------------------------------------------------------------------------------------------- int size(){ return nsize; } Type& operator[](int i){ if(i>=0 && i next); return p->elem; } } }; ////////////////////////////////////////////////CTreeNode define////////////////////////////////////////////////////////// class CTreeNode{ public: void* pData;//无类型指针,可指向任何类型数据,用以保存额外的数据或对象 int nindex;//在兄弟结点中的序号 int id; char *pcaption;//标题 CTreeNode* pparent; Cvector_10 child_vector; // vector child_vector;//孩子结点列表 public: CTreeNode(); CTreeNode* getFirstChild(); CTreeNode* getSibl_forder(); CTreeNode* getNext_forder(); int AppendChildNode(CTreeNode* pchildNode);//加入一个孩子结点,并返回加入的位序 bool operator==(const CTreeNode&)const; }; ////////////////////////////////////////////////CTree define////////////////////////////////////////////////////////////// class CTree{ protected: CTreeNode* proot; public: CTree(){ proot = NULL; } void AppendNode(CTreeNode* pparentNode, CTreeNode* pchildNode ); void DeleteNode(CTreeNode* pNode); //删除pNode为根的子树 //------------------------------------------------------------------------------------------------------------------------ CTreeNode* root(){ return proot; } };//class ////////////////////////////////////////////////CWinManager/////////////////////////////////////////////////////////////// class CWinManager:public CTree{ protected: int nodeNum; //maxnum of active windows // static CTreeNode* message_map(int x, int y);//暂时设成CWindow*, int,以后改成void, message public: Cvector_10 activeWinArry; //------------------------------------------------------------------------------------------------------------------------ CWinManager():CTree(){ nodeNum = 1; // proot->id = 0; } //------------------------------------------------------------------------------------------------------------------------ void AppendNode(CTreeNode* pparentNode, CTreeNode* pchildNode); void DeleteNode(CTreeNode* pNode); void renewWinArry(CTreeNode* pNode){ activeWinArry.erase(pNode); activeWinArry.push_back(pNode); } //------------------------------------------------------------------------------------------------------------------------ int getnum(){ return nodeNum; } //------------------------------------------------------------------------------------------------------------------------ void winRedraw(){ // for(int i=0; i x = x; this->y = y; this->mssgtype = mssgtype; this->wparam = wparam; this->lparam = lparam; this->hwnd = hwnd; } }; ////////////////////////////////////////////////CWindow/////////////////////////////////////////////////////////////////// class CWindow{ private: CWindow* phwnd; //父窗口句柄 CWindow* hwnd; //窗口句柄 static CWindow* fhwnd; //焦点控件句柄 //private: public: static CWinManager* winMnger;//窗口管理器 CTreeNode* phomeNode;//窗口所属树结点 static int winNum; public: int x; //起始坐标 int y; //起始坐标 int width; //宽度 int lenth; //长度 char* caption; //标题 int wintype; //窗口类型 0:win,1:text,2:button void(*OnClick)(CMessage* pmssg);//单击响应函数 void(*OnKeyDown)(CMessage* pmssg);//按键响应函数 public: //------------------------------------------------------------------------------------------------------------------------ CWindow( int x, int y, int width, int lenth, CWindow* phwndL, char* caption); //------------------------------------------------------------------------------------------------------------------------ ~CWindow(){ delete phomeNode; winNum--; if(winNum==0){ delete CWindow::winMnger; } } void getfocus(); virtual void freefocus(){}; //------------------------------------------------------------------------------------------------------------------------ bool capture(int x, int y){ return (x>this->x && x x+width && y>this->y && y y+lenth); } virtual void drawin(){} //draw this win virtual void closewin(){} //close a win with some resource ,like CText void drawindow(); void showindow(); virtual void closewindow(){}; CWindow* get_hwnd(){return hwnd;} CWindow* get_phwnd(){return phwnd;} void set_phwnd(CWindow* phwnd); virtual void winProc(CMessage* pmssg); static CWindow* get_fhwnd(){return fhwnd;}; };//class CWindow ////////////////////////////////////////////////CButton/////////////////////////////////////////////////////////////////// class CButtn:public CWindow{ public: CButtn(int x=0, int y=0, int width=0, int lenth=0, CWindow* phwnd=NULL, char* caption="myButton") :CWindow(x, y, width, lenth, phwnd, caption){ wintype = 2; //button } void drawin(){ CDC *pDC = (AfxGetApp()->m_pMainWnd)->GetDC(); pDC->Rectangle(CRect(x, y+10, x+width, y+lenth)); pDC->TextOut(x+15, y+20, caption); } }; ////////////////////////////////////////////////CClsButton/////////////////////////////////////////////////////////////////// class CClsButtn:public CButtn{ public: CClsButtn(int x=0, int y=0, int width=0, int lenth=0, CWindow* phwnd=NULL, char* caption="X") :CButtn(x, y, width, lenth, phwnd, caption){}; private: void winProc(CMessage* pmssg){ CButtn::winProc(pmssg); switch(pmssg->mssgtype){ default: if(this->phomeNode->pparent){ CWindow* pwin = (CWindow*)this->phomeNode->pparent->pData; pwin->closewindow(); } } } }; ////////////////////////////////////////////////CPagdwnButton/////////////////////////////////////////////////////////////////// class CText; ////////////////////////////////////////////////CText////////////////////////////////////////////////////////////////////// //#define buffsize 500 static const int buttnsize = 30; static const int buffsize = 500; class CText:public CWindow{ public: int font_x; //字体尺寸 int font_y; private: char text[buffsize];//字符缓冲区 int curpos; //光标位置 int endpos; //结束字符位置 int curline; //光标在的行数 int curcol; //光标在的列数 int endline; //结束字符在的行数 int endcol; //结束字符在的列数 int linegap; //行距(font_y的倍数) int dspstartline; //显示起始行 // int dspendline;//显示结束行 int cperline; //一行最大字符数 int cpercol; //一例最大字符数 public: CText(int x=0, int y=0, int width=0, int lenth=0, CWindow* phwnd=NULL, char* caption= "myText") :CWindow(x, y, width, lenth, phwnd, caption){ wintype = 1; //text text[0] = '\0'; font_x = 8; font_y = 16; curpos = 0; endpos = 0; endline = 0; endcol = 0; dspstartline = 0; linegap = 1.2*font_y; cperline = (width)/font_x; cpercol = lenth/linegap; } //------------------------------------------------------------------------------------------------------------------------ void winProc(CMessage* pmssg); void input(char c); //------------------------------------------------------------------------------------------------------------------------ void clearscrn(int x0, int y0, int x1, int y1 ){ CDC *pDC = (AfxGetApp()->m_pMainWnd)->GetDC(); pDC->Rectangle(CRect(x0, y0, x1, y1)); } //------------------------------------------------------------------------------------------------------------------------ void textout(); void drawin(); void closewin(){ text[0] = '\0'; curpos = 0; endpos = 0; endline = 0; endcol = 0; dspstartline = 0; } //------------------------------------------------------------------------------------------------------------------------ void linedown(){ if(dspstartline endline){ dspstartline = endline; }else{ dspstartline += (cpercol-2); } } //------------------------------------------------------------------------------------------------------------------------ void pageup(){ if(dspstartline-(cpercol-2)<0){ dspstartline = 0; }else{ dspstartline -= (cpercol-2); } } //------------------------------------------------------------------------------------------------------------------------ void delet(){ int cperline = width/font_x; if(endpos>0){ for(int i=curpos; i<=endpos; i++){ text[i-1] = text[i]; } curpos--; endpos--; endcol = endpos%cperline; endline = endpos/cperline; curcol = (curpos)%cperline; curline = (curpos)/cperline; } } //------------------------------------------------------------------------------------------------------------------------ void moveleft(){ if(curpos>0){ curpos--; curcol = curpos%cperline; curline = curpos/cperline; } } //------------------------------------------------------------------------------------------------------------------------ void moveright(){ if(curpos m_pMainWnd)->GetDC(); pDC->TextOut(this->x+curcol*font_x+4, this->y+12+(curline-dspstartline)*linegap, " ");//erase the cursor } }; ////////////////////////////////////////////////CWin////////////////////////////////////////////////////////////////////// //void closewin (CMessage* pmssg){CWin::closewindow();}; class CWin: public CWindow{ private: CClsButtn* pclsBtn; // void closeWin(CMessage* pmssg){closeWindow();};//单击响应函数 public: CWin(int x=0, int y=0, int width=0, int lenth=0, CWindow* phwnd=NULL, char* caption="myWindow") :CWindow(x, y, width, lenth, phwnd, caption){ //add close button on the right-top conner if(winNum!=0){//desktop need not closebutton pclsBtn = new CClsButtn(x+width-30, y-10, 30, 40, this, "X"); } }; ~CWin(){ delete pclsBtn; } void drawin(){ CDC *pDC = (AfxGetApp()->m_pMainWnd)->GetDC(); pDC->Rectangle(CRect(x, y, x+width, y+lenth)); pDC->Rectangle(CRect(x, y, x+width, y+30)); pDC->TextOut(x+5, y+5, caption); } void closewindow(){ CWindow* pwin = NULL; Cvector_10 & theArry = CWindow::winMnger->activeWinArry; //hide the win and clear the buffer theArry.erase(this->phomeNode); Cvector_10 & thechildArry = this->phomeNode->child_vector; for(int j=0; j pData; pwin->closewin(); //release char in the text } //fresh the desktop pwin = (CWindow*)CWindow::winMnger->root()->pData; pwin->drawin(); for(int i=1; i pData; pwin->drawindow();//画出所有活动窗口(及所有子窗口),and pushu the main win to theArry 子窗口) } } }; ////////////////////////////////////////////////globe fuctiion//////////////////////////////////////////////////////////// void message_map(CMessage* pmssg);