ShapeTree.cpp
资源名称:DrawSys.zip [点击查看]
上传用户:mosfetic
上传日期:2022-06-16
资源大小:4612k
文件大小:5k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // ShapeTree.cpp : implementation file
- //
- #include "stdafx.h"
- #include "DrawSys.h"
- #include "ShapeTree.h"
- #include "DrawShape.h"
- #include "DrawRect.h"
- #include "DrawEllipse.h"
- #include "DrawCircle.h"
- #include "DrawFillRect.h"
- #include "DrawFillEllipse.h"
- #include "DrawLine.h"
- #include "MainFrm.h"
- #define WM_CHOOSE (WM_USER+1)
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CShapeTree
- CShapeTree::CShapeTree()
- {
- m_hRectRoot = NULL;
- m_hEllipseRoot = NULL;
- m_hCircleRoot = NULL;
- m_hFillRectRoot = NULL;
- m_hFillEllipseRoot = NULL;
- m_hLineRoot = NULL;
- m_iRectNum = 0;
- m_iEllipseNum = 0;
- m_iCircleNum = 0;
- m_iFillRectNum = 0;
- m_iFillEllipseNum = 0;
- m_iLineNum = 0;
- }
- CShapeTree::~CShapeTree()
- {
- m_lstBitmap.DeleteImageList();
- }
- BEGIN_MESSAGE_MAP(CShapeTree, CTreeCtrl)
- //{{AFX_MSG_MAP(CShapeTree)
- ON_WM_CREATE()
- ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
- ON_WM_RBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CShapeTree message handlers
- void CShapeTree::InsertShape(CDrawShape* pDrawShape, int iIndex)
- {
- CString strText;
- HTREEITEM hItem = NULL;
- if(pDrawShape)
- {
- if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawRect)))
- {
- if(!m_hRectRoot)
- {
- m_hRectRoot = InsertItem("矩形", 0, 0);
- SetItemData(m_hRectRoot, 0);
- }
- m_iRectNum++;
- MyFormatString(strText, m_iRectNum);
- hItem = InsertItem(strText, 0, 0, m_hRectRoot);
- SetItemData(hItem, iIndex);
- Expand(m_hRectRoot, TVE_EXPAND);
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawEllipse)))
- {
- if(!m_hEllipseRoot)
- {
- m_hEllipseRoot = InsertItem("椭圆", 1, 1);
- SetItemData(m_hEllipseRoot, 0);
- }
- m_iEllipseNum++;
- MyFormatString(strText, m_iEllipseNum);
- hItem = InsertItem(strText, 1, 1, m_hEllipseRoot);
- SetItemData(hItem, iIndex);
- Expand(m_hEllipseRoot, TVE_EXPAND);
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawCircle)))
- {
- if(!m_hCircleRoot)
- {
- m_hCircleRoot = InsertItem("圆", 2, 2);
- SetItemData(m_hCircleRoot, 0);
- }
- m_iCircleNum++;
- MyFormatString(strText, m_iCircleNum);
- hItem = InsertItem(strText, 2, 2, m_hCircleRoot);
- SetItemData(hItem, iIndex);
- Expand(m_hCircleRoot, TVE_EXPAND);
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawFillRect)))
- {
- if(!m_hFillRectRoot)
- {
- m_hFillRectRoot = InsertItem("填充矩形", 5, 5);
- SetItemData(m_hFillRectRoot, 0);
- }
- m_iFillRectNum++;
- MyFormatString(strText, m_iFillRectNum);
- hItem = InsertItem(strText, 5, 5, m_hFillRectRoot);
- SetItemData(hItem, iIndex);
- Expand(m_hFillRectRoot, TVE_EXPAND);
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawFillEllipse)))
- {
- if(!m_hFillEllipseRoot)
- {
- m_hFillEllipseRoot = InsertItem("填充椭圆", 4, 4);
- SetItemData(m_hFillEllipseRoot, 0);
- }
- m_iFillEllipseNum++;
- MyFormatString(strText, m_iFillEllipseNum);
- hItem = InsertItem(strText, 4, 4, m_hFillEllipseRoot);
- SetItemData(hItem, iIndex);
- Expand(m_hFillEllipseRoot, TVE_EXPAND);
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawLine)))
- {
- if(!m_hLineRoot)
- {
- m_hLineRoot = InsertItem("直线", 3, 3);
- SetItemData(m_hLineRoot, 0);
- }
- m_iLineNum++;
- MyFormatString(strText, m_iLineNum);
- hItem = InsertItem(strText, 3, 3, m_hLineRoot);
- SetItemData(hItem, iIndex);
- Expand(m_hLineRoot, TVE_EXPAND);
- }
- }
- }
- int CShapeTree::GetID(CDrawShape* pDrawShape)
- {
- if(pDrawShape)
- {
- if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawRect)))
- {
- return m_iRectNum+1;
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawEllipse)))
- {
- return m_iEllipseNum+1;
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawCircle)))
- {
- return m_iCircleNum+1;
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawFillRect)))
- {
- return m_iFillRectNum+1;
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawFillEllipse)))
- {
- return m_iFillEllipseNum+1;
- }
- else if(pDrawShape->IsKindOf(RUNTIME_CLASS(CDrawLine)))
- {
- return m_iLineNum+1;
- }
- }
- return 0;
- }
- void CShapeTree::MyFormatString(CString& strText, int i)
- {
- strText.Empty();
- if(i < 10)
- {
- strText.Format("00%d#", i);
- }
- else if(i < 100)
- {
- strText.Format("0%d#", i);
- }
- else
- {
- strText.Format("%d#", i);
- }
- }
- int CShapeTree::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- m_lstBitmap.Create(IDB_BITMAP_IMAGE, 16, 0, RGB(0, 0, 0));
- SetImageList(&m_lstBitmap, TVSIL_NORMAL);
- return 0;
- }
- void CShapeTree::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
- DWORD wNewIndex = 0;
- DWORD wOldIndex = 0;
- if(pNMTreeView->itemNew.hItem)
- {
- wNewIndex = GetItemData(pNMTreeView->itemNew.hItem);
- }
- if(pNMTreeView->itemOld.hItem)
- {
- wOldIndex = GetItemData(pNMTreeView->itemOld.hItem);
- }
- CWnd* pWnd = AfxGetMainWnd();
- if(wNewIndex > 0)
- {
- ((CMainFrame*)pWnd)->GetActiveView()->SendMessage(WM_CHOOSE, wNewIndex, wOldIndex);
- }
- *pResult = 0;
- }