⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wndgraph.cpp

📁 Template functions for serializing arbitrary linked nodes. 串行化连接节点的模板函数(源码)(25KB)
💻 CPP
字号:
// WndGraph.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "GenericSerializeDemo.h"
#include "WndGraph.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWndGraph

CWndGraph::CWndGraph(vectorGraph& rvecGraph,CWnd* pParent,UINT nID,CRect rc)
                            :m_vecGraph(rvecGraph)
{
    Create(NULL,NULL,(WS_CHILD|WS_VISIBLE|WS_BORDER),rc,pParent,nID);
}

CWndGraph::~CWndGraph()
{
}


BEGIN_MESSAGE_MAP(CWndGraph, CWnd)
	//{{AFX_MSG_MAP(CWndGraph)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Behandlungsroutinen f黵 Nachrichten CWndGraph 

void CWndGraph::DrawConnections(CDC& dc,Graph* pGraph)
{
    
    for(unsigned i=0;i<pGraph->vecConnections.size();i++){
        for(unsigned j=0;j<m_vecGraph.size();j++){
            dc.MoveTo(m_vecGraph[j]->x+10,m_vecGraph[j]->y+10);
            dc.LineTo(pGraph->x+10,pGraph->y+10);
        }
    }
}

void CWndGraph::DrawCircle(CDC& dc,Graph* pGraph)
{
    CBrush br;
    br.CreateSolidBrush(RGB(0,255,255));
    CBrush* pBr= dc.SelectObject(&br);
    dc.Ellipse(pGraph->x,pGraph->y,pGraph->x+20,pGraph->y+20);
    if( pBr ){
        dc.SelectObject(pBr);
    }
}

void CWndGraph::OnPaint() 
{
	CPaintDC dc(this); // device context for paintin

    unsigned i;
    for(i=0;i<m_vecGraph.size();i++){
        DrawCircle(dc,m_vecGraph[i]);
    }
    for(i=0;i<m_vecGraph.size();i++){
        DrawConnections(dc,m_vecGraph[i]);
    }

    
//    DrawRect(dc,m_vecIntPoints[i],1);

}

BOOL CWndGraph::OnEraseBkgnd(CDC* pDC) 
{	
  
    CRect rc;
    GetClientRect(&rc);
    pDC->Rectangle(rc);

    return TRUE;
}

bool CWndGraph::IsInGraphNode(CPoint point,int& nInd)
{
    for(unsigned i=0;i<m_vecGraph.size();i++){
        CRect rc(m_vecGraph[i]->x-20,m_vecGraph[i]->y-20,
                                m_vecGraph[i]->x+20,m_vecGraph[i]->y+20);
        if( rc.PtInRect(point) ){
            nInd= i;
            return true;
        }
    }
    return false;
}

void CWndGraph::OnLButtonDown(UINT nFlags, CPoint point) 
{
    int nInd;
    if( IsInGraphNode(point,nInd) ){
        if( AfxMessageBox(_T("Delete node"),MB_YESNO)==IDYES ){
            Graph* pToDelete= m_vecGraph[nInd];
            for(unsigned i=0;i<m_vecGraph.size();i++){
                for(int j=m_vecGraph[i]->vecConnections.size()-1;j>=0;j--){
                    if( m_vecGraph[i]->vecConnections[j]==pToDelete ){
                        m_vecGraph[i]->vecConnections.erase(m_vecGraph[i]->vecConnections.begin()+j);
                    }
                }
            }
            m_vecGraph.erase(m_vecGraph.begin()+nInd);
            delete pToDelete;
            Invalidate();
            UpdateWindow();
            GetParent()->SendMessage(WM_MODIFIED);
        }
    }else{
        if(AfxMessageBox(_T("Insert new node"),MB_YESNO)==IDYES){
            m_vecGraph.push_back(new Graph(point.x,point.y));
            srand( (unsigned)time( NULL ) );
            if( rand()%5 > 3 ){ 
                int nOfConnections= rand()%2;
                for(int i=0;i<nOfConnections;i++){
                    m_vecGraph[m_vecGraph.size()-1]->vecConnections.push_back(
                                                m_vecGraph[rand()%m_vecGraph.size()]);
                }
            }
            Invalidate();
            UpdateWindow();
            GetParent()->SendMessage(WM_MODIFIED);
        }
    }
	
	CWnd::OnLButtonDown(nFlags, point);
}

void CWndGraph::PostNcDestroy() 
{
	
	CWnd::PostNcDestroy();
    delete this;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -