123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- // CGraph.cpp: implementation of the CGraph class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- //#include <afxtempl.h>
- #include "drawobj.h"
- #include "graph.h"
- //---------------------------------------------- CGraph ---------
- CGraph::CGraph(CSize size)
- {
- m_Type = 1;
- m_sizePic.cx = size.cx;
- m_sizePic.cy = size.cy;
- m_bGrid = FALSE;
- m_sizeGrid.cx = 40;
- m_sizeGrid.cy = 40;
- m_clrBack = RGB(230, 241, 249);//RGB(255, 255, 255);//RGB(92, 92, 92);画面默认背景 颜色
- }
- CGraph::~CGraph(void)
- {
- ClearDrawList();
- }
- void CGraph::ClearDrawList()
- {
- CDrawObj *pDrawObj = NULL;
- while( !m_DrawObjList.IsEmpty() )
- {
- // RemoveTail删除元素并返回对象指针;
- pDrawObj = m_DrawObjList.RemoveTail();
- if ( pDrawObj )
- delete pDrawObj;
- pDrawObj = NULL;
- }
- }
- void CGraph::SetPicFile(IN LPCTSTR lpPicPath)
- {
- if ( lpPicPath != NULL )
- m_strPicFile = lpPicPath;
- }
- void CGraph::SetView(CView* pView)
- {
- m_pView = pView; // if ( pView == NULL )
- POSITION pos = m_DrawObjList.GetHeadPosition();
- while( pos != NULL )
- {
- CDrawObj *pDrawObj = m_DrawObjList.GetNext(pos);
- pDrawObj->m_pView = m_pView;
- }
- }
- BOOL CGraph::Load(void)
- {
- // 加载前,先清空以前的;
- ClearDrawList();
- if ( m_pView == NULL)
- return FALSE;
- // 从文件中读取;
- CFile file;
- if ( !file.Open(m_strPicFile,CFile::modeRead) )
- return FALSE;
- CArchive ar(&file,CArchive::load);
- ar >> m_Type;
- ar >> m_sizePic;
- ar >> m_sizeGrid;
- ar >> m_bGrid;
- ar >> m_clrBack;
- m_DrawObjList.Serialize(ar);
- // 编辑按钮控件数次,有撤销显示时,继续编辑,点击撤销,客户端会崩溃 解决;
- POSITION pos = m_DrawObjList.GetHeadPosition();
- while ( pos != NULL )
- {
- CDrawObj *pDrawObj = m_DrawObjList.GetNext(pos);
- pDrawObj->m_pView = m_pView;
- }
- // 释放资源;
- ar.Close();
- file.Close();
- return TRUE;
- }
- void CGraph::Store(void)
- {
- CFile file;
- if(file.Open(m_strPicFile,CFile::modeCreate|CFile::modeWrite))
- {
- CArchive ar(&file,CArchive::store);
- ar << m_Type;
- ar << m_sizePic;
- ar << m_sizeGrid;
- ar << m_bGrid;
- ar << m_clrBack;
- m_DrawObjList.Serialize(ar);
- ar.Flush();
- ar.Close();
- file.Flush();
- file.Close();
- }
- }
- /************************************************************************/
- /* 函数:[5/9/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- CRect CGraph::Draw(IN CDC* pDC, IN CString &sOCXPath, IN int &iOcxIndex, IN BOOL bPrint /* = FALSE */)
- {
- CRect rectRet=NULL;
- CRect rect;
- // 1.判断是否打印;
- if ( !bPrint )
- {
- if (g_bRun)
- {
- m_pView->GetClientRect(rect);
- CRect tmp = CRect(CPoint(0,0),m_sizePic);;
- rect.UnionRect( rect, tmp );
- pDC->FillSolidRect(rect,m_clrBack);
- }
- else
- {
- rect = CRect(CPoint(0,0),m_sizePic);
- pDC->FillSolidRect(rect,m_clrBack);
- }
- }
- pDC->SetBkMode(TRANSPARENT);
- // 2.如果没运行,并且不是打印状态,则画网格;
- if(!CDrawObj::bRuning && !pDC->IsPrinting() && m_bGrid && (!bPrint) )
- {
- int r = (64+GetRValue(m_clrBack)) % 256;
- int g = (64+GetGValue(m_clrBack)) % 256;
- int b = (64+GetBValue(m_clrBack)) % 256;
- CPen pen(PS_DOT,1,RGB(r,g,b));
- CPen* pOldPen = pDC->SelectObject(&pen);
- int Width = rect.Width();
- int Height = rect.Height();
- for(int w=0;w<Width;w+=m_sizeGrid.cx)
- {
- pDC->MoveTo(rect.left + w-1, rect.top);
- pDC->LineTo(rect.left + w-1, rect.bottom);
- }
- for(int h=0;h<Height;h+=m_sizeGrid.cy)
- {
- pDC->MoveTo(rect.left , rect.top + h-1);
- pDC->LineTo(rect.right, rect.top + h-1);
- }
- pDC->SelectObject(pOldPen);
- DeleteObject( pen );
- }
- // 3.GetClipBox取得当前需要重绘的矩形区域;
- // 并据此计算需要重绘那几行;
- CRect rectClip;
- pDC->GetClipBox(&rectClip);
- // 4.逐个图元判断是否相交,相交则绘制出图元;
- POSITION pos = m_DrawObjList.GetHeadPosition();
- while(pos!=NULL)
- {
- CDrawObj* pObj = m_DrawObjList.GetNext(pos);
- CRect rect = pObj->m_rect;
- rect.NormalizeRect();
- rect.InflateRect(5,5,5,5);
- if(rect.IntersectRect(&rect,&rectClip))
- {// 图元与裁剪区相交,则图元重绘;
- if ( pObj->m_strCaption == _T("OCX"))
- {
- sOCXPath = ((CDrawOCX *)pObj)->m_strOcxPath;
- iOcxIndex = ((CDrawOCX *)pObj)->m_nOcxIndex;
- rectRet = pObj->m_rect;
- }
- pObj->Draw(pDC);
- }
- }
- // 5.编辑状态下,且不是打印状态下,画整个图元视图的边框;
- if (!g_bRun && (!bPrint) )
- {
- CRect rect1;
- CBrush brush(RGB(202,202,202));
- CBrush *prb;
- prb=pDC->SelectObject(&brush);
- rect1 = CRect(0,0,m_sizePic.cx,5);
- pDC->FillRect(&rect1,&brush);
- rect1 = CRect(0,0,5,m_sizePic.cy);
- pDC->FillRect(&rect1,&brush);
- rect1 = CRect(0,m_sizePic.cy,m_sizePic.cx,m_sizePic.cy-5);
- pDC->FillRect(&rect1,&brush);
- rect1 = CRect(m_sizePic.cx,0,m_sizePic.cx-5,m_sizePic.cy);
- pDC->FillRect(&rect1,&brush);
-
- // 画8根线
- CPen pen(PS_SOLID,1,RGB(232,232,232));
- CPen* pOldPen = pDC->SelectObject(&pen);
- pDC->MoveTo(1, m_sizePic.cy-1);
- pDC->LineTo(1, 1);
- pDC->LineTo(m_sizePic.cx-1, 1);
-
- CPen pen1(PS_SOLID,1,RGB(64,64,64));
- pDC->SelectObject(&pen1);
- pDC->MoveTo(1, m_sizePic.cy-1);
- pDC->LineTo(m_sizePic.cx, m_sizePic.cy-1);
- pDC->LineTo(m_sizePic.cx, 1);
-
- CPen pen2(PS_SOLID,1,RGB(242,242,242));
- pDC->SelectObject(&pen2);
- pDC->MoveTo(5, m_sizePic.cy-5);
- pDC->LineTo(m_sizePic.cx-4, m_sizePic.cy-5);
- pDC->LineTo(m_sizePic.cx-4, 5);
-
- CPen pen3(PS_SOLID,1,RGB(96,96,96));
- pDC->SelectObject(&pen3);
- pDC->MoveTo(4, m_sizePic.cy-5);
- pDC->LineTo(4, 4);
- pDC->LineTo(m_sizePic.cx-5, 4);
-
- pDC->SelectObject(pOldPen);
- pDC->SelectObject(prb);
- brush.DeleteObject();
- pDC->SetBkMode(OPAQUE);
- DeleteObject( pen );
- DeleteObject( pen1 );
- DeleteObject( pen2 );
- DeleteObject( pen3 );
- DeleteObject( brush );
- }
-
- return rectRet;
- }
- void CGraph::Print(CDC* pDC)
- {
- ASSERT(m_pView);
- CString sTemp;
- int nTemp=0;
- CSize size = m_sizePic;
- CClientDC dc(m_pView);
- CDC dcMem;
- dcMem.CreateCompatibleDC(&dc);
- CBitmap* pBitmap = new CBitmap;
- pBitmap->CreateCompatibleBitmap(&dc,size.cx,size.cy);
- CBitmap* pOldBitmap = dcMem.SelectObject(pBitmap);
- Draw(&dcMem,sTemp,nTemp,TRUE);
- dcMem.InvertRect(CRect(CPoint(0,0),m_sizePic));
- for(int y=0;y<size.cy;y++)
- {
- int nX1 = 0;
- COLORREF clrPixel = ::GetPixel(dcMem.m_hDC,0,y);
- int nRValue0 = GetRValue(clrPixel);
- int nGValue0 = GetGValue(clrPixel);
- int nBValue0 = GetBValue(clrPixel);
- int x = 0;
- for( x=1;x<size.cx;x++)
- {
- COLORREF clrPixel = ::GetPixel(dcMem.m_hDC,x,y);
- int nRValue = GetRValue(clrPixel);
- int nGValue = GetGValue(clrPixel);
- int nBValue = GetBValue(clrPixel);
- int nRDiff = (nRValue<nRValue0)?nRValue0-nRValue:nRValue-nRValue0;
- int nGDiff = (nGValue<nGValue0)?nGValue0-nGValue:nGValue-nGValue0;
- int nBDiff = (nBValue<nBValue0)?nBValue0-nBValue:nBValue-nBValue0;
- if(nRDiff+nGDiff+nBDiff>32)
- {
- RECT rect;
- rect.left = nX1;
- rect.top = y;
- rect.right = x;
- rect.bottom = y+1;
- ::SetBkColor(pDC->m_hDC,RGB(nRValue0,nGValue0,nBValue0));
- ::ExtTextOut(pDC->m_hDC,0,0,ETO_OPAQUE,&rect,NULL,0,NULL);
- nX1 = x;
- nRValue0 = nRValue;
- nGValue0 = nGValue;
- nBValue0 = nBValue;
- }
- }
- RECT rect;
- rect.left = nX1;
- rect.top = y;
- rect.right = x;
- rect.bottom = y+1;
- ::SetBkColor(pDC->m_hDC,RGB(nRValue0,nGValue0,nBValue0));
- ::ExtTextOut(pDC->m_hDC,0,0,ETO_OPAQUE,&rect,NULL,0,NULL);
- }
- //2011-11-01 add
- DeleteObject( pBitmap );
- dcMem.SelectObject(pOldBitmap);
- dcMem.DeleteDC( );
- delete pBitmap;
- }
- void CGraph::Fresh(void)
- {
- #if 0
- if(!m_DrawObjList.IsEmpty())
- {
- CRect rect;
- for(POSITION pos = m_DrawObjList.GetHeadPosition();pos!=NULL;)
- {
- POSITION pos1 = pos ; //要在这里做一个备份
- if( (int)pos1 == 0xfeeefeee ) break; // 频繁单击画面会出现异常,暂时不知道什么解决
- CDrawObj* pObj = m_DrawObjList.GetAt(pos1);
- if( (int)pObj == 0xcdcdcdcd || (int)pObj == 0xfeeefeee || (int)pObj == 0x00000001 ) break; // 频繁单击画面会出现异常,暂时不知道什么解决
- if( (int)pObj->m_pView == 0xfeeefeee || (int)pObj->m_pView == 0x00000001 ) // 频繁单击画面会出现异常,暂时不知道什么解决
- {
- TRACE(g_strTRACE0);
- break;
- }
-
- if( pObj->Fresh(rect) )
- InvalidateRect( pObj->m_pView->GetSafeHwnd(),rect,true );
- m_DrawObjList.GetNext(pos);//他会改变 pos的值
- }
- }
- #else
- CRect rect;
- CDrawObj* pObj = NULL;
- POSITION pos = m_DrawObjList.GetHeadPosition();
- while( pos != NULL)
- {
- pObj = m_DrawObjList.GetNext(pos);
- if ( pObj->Fresh(rect) )
- InvalidateRect( pObj->m_pView->GetSafeHwnd(), rect, true);
- }
- #endif
- }
|