FontCurveCtl.cpp
资源名称:fontcurve.rar [点击查看]
上传用户:sz81710966
上传日期:2013-03-01
资源大小:409k
文件大小:16k
源码类别:
多国语言处理
开发平台:
Visual C++
- /*******************************************************************************
- 注意:
- 1. 此控件是以传入数据的各个bit位是否为1,来组建Grid队列;然后通过Grid队列对
- 矩形区域进行绘图;
- 2. 目前支持可变的属性有:背景色的变换、GRID网格颜色的变换、Grid边线颜色的变换、
- Grid水平个数变换、Grid垂直个数变换、显示方向的变换(水平还是垂直)、
- 显示图像形状(是矩形还是圆形);
- ********************************************************************************/
- #include "stdafx.h"
- #include "FontCurve.h"
- #include "FontCurveCtl.h"
- #include "FontCurvePpg.h"
- #include "..includeMemDC.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- IMPLEMENT_DYNCREATE(CFontCurveCtrl, COleControl)
- /////////////////////////////////////////////////////////////////////////////
- // Message map
- BEGIN_MESSAGE_MAP(CFontCurveCtrl, COleControl)
- //{{AFX_MSG_MAP(CFontCurveCtrl)
- ON_WM_NCCALCSIZE()
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // Dispatch map
- BEGIN_DISPATCH_MAP(CFontCurveCtrl, COleControl)
- //{{AFX_DISPATCH_MAP(CFontCurveCtrl)
- DISP_FUNCTION(CFontCurveCtrl, "SetBackColor", SetBackColor, VT_EMPTY, VTS_COLOR)
- DISP_FUNCTION(CFontCurveCtrl, "SetGridLineColor", SetGridLineColor, VT_EMPTY, VTS_COLOR)
- DISP_FUNCTION(CFontCurveCtrl, "SetGridColor", SetGridColor, VT_EMPTY, VTS_COLOR)
- DISP_FUNCTION(CFontCurveCtrl, "SetGridHoriNum", SetGridHoriNum, VT_EMPTY, VTS_I2)
- DISP_FUNCTION(CFontCurveCtrl, "SetGridVerNum", SetGridVerNum, VT_EMPTY, VTS_I2)
- DISP_FUNCTION(CFontCurveCtrl, "SetGridShowType", SetGridShowType, VT_EMPTY, VTS_I2)
- DISP_FUNCTION(CFontCurveCtrl, "SetGridDrawType", SetGridDrawType, VT_EMPTY, VTS_I2)
- DISP_FUNCTION(CFontCurveCtrl, "ShowGridData", ShowGridData, VT_BOOL, VTS_PBSTR VTS_I2 VTS_I2 VTS_I2)
- //}}AFX_DISPATCH_MAP
- END_DISPATCH_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // Event map
- BEGIN_EVENT_MAP(CFontCurveCtrl, COleControl)
- //{{AFX_EVENT_MAP(CFontCurveCtrl)
- // NOTE - ClassWizard will add and remove event map entries
- // DO NOT EDIT what you see in these blocks of generated code !
- //}}AFX_EVENT_MAP
- END_EVENT_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // Property pages
- // TODO: Add more property pages as needed. Remember to increase the count!
- BEGIN_PROPPAGEIDS(CFontCurveCtrl, 1)
- PROPPAGEID(CFontCurvePropPage::guid)
- END_PROPPAGEIDS(CFontCurveCtrl)
- /////////////////////////////////////////////////////////////////////////////
- // Initialize class factory and guid
- IMPLEMENT_OLECREATE_EX(CFontCurveCtrl, "FONTCURVE.FontCurveCtrl.1",
- 0x39ca78cf, 0xecfe, 0x4e88, 0x8b, 0x44, 0x51, 0xe9, 0x60, 0x18, 0x3a, 0x22)
- /////////////////////////////////////////////////////////////////////////////
- // Type library ID and version
- IMPLEMENT_OLETYPELIB(CFontCurveCtrl, _tlid, _wVerMajor, _wVerMinor)
- /////////////////////////////////////////////////////////////////////////////
- // Interface IDs
- const IID BASED_CODE IID_DFontCurve =
- { 0x680ae108, 0xf7ef, 0x45a1, { 0xbf, 0xdc, 0xfc, 0x29, 0xd0, 0xfb, 0xc6, 0x5c } };
- const IID BASED_CODE IID_DFontCurveEvents =
- { 0x7e93f453, 0xe502, 0x4f31, { 0xb7, 0xff, 0x8, 0xb3, 0x35, 0xb6, 0xc3, 0xa6 } };
- /////////////////////////////////////////////////////////////////////////////
- // Control type information
- static const DWORD BASED_CODE _dwFontCurveOleMisc =
- OLEMISC_ACTIVATEWHENVISIBLE |
- OLEMISC_SETCLIENTSITEFIRST |
- OLEMISC_INSIDEOUT |
- OLEMISC_CANTLINKINSIDE |
- OLEMISC_RECOMPOSEONRESIZE;
- IMPLEMENT_OLECTLTYPE(CFontCurveCtrl, IDS_FONTCURVE, _dwFontCurveOleMisc)
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl::CFontCurveCtrlFactory::UpdateRegistry -
- // Adds or removes system registry entries for CFontCurveCtrl
- BOOL CFontCurveCtrl::CFontCurveCtrlFactory::UpdateRegistry(BOOL bRegister)
- {
- // TODO: Verify that your control follows apartment-model threading rules.
- // Refer to MFC TechNote 64 for more information.
- // If your control does not conform to the apartment-model rules, then
- // you must modify the code below, changing the 6th parameter from
- // afxRegApartmentThreading to 0.
- if (bRegister)
- return AfxOleRegisterControlClass(
- AfxGetInstanceHandle(),
- m_clsid,
- m_lpszProgID,
- IDS_FONTCURVE,
- IDB_FONTCURVE,
- afxRegApartmentThreading,
- _dwFontCurveOleMisc,
- _tlid,
- _wVerMajor,
- _wVerMinor);
- else
- return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
- }
- /////////////////////////////////////////////////////////////////////////////
- // Licensing strings
- static const TCHAR BASED_CODE _szLicFileName[] = _T("FontCurve.lic");
- static const WCHAR BASED_CODE _szLicString[] =
- L"Copyright (c) 2004";
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl::CFontCurveCtrlFactory::VerifyUserLicense -
- // Checks for existence of a user license
- BOOL CFontCurveCtrl::CFontCurveCtrlFactory::VerifyUserLicense()
- {
- return AfxVerifyLicFile(AfxGetInstanceHandle(), _szLicFileName,
- _szLicString);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl::CFontCurveCtrlFactory::GetLicenseKey -
- // Returns a runtime licensing key
- BOOL CFontCurveCtrl::CFontCurveCtrlFactory::GetLicenseKey(DWORD dwReserved,
- BSTR FAR* pbstrKey)
- {
- if (pbstrKey == NULL)
- return FALSE;
- *pbstrKey = SysAllocString(_szLicString);
- return (*pbstrKey != NULL);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl::CFontCurveCtrl - Constructor
- CFontCurveCtrl::CFontCurveCtrl()
- {
- InitializeIIDs(&IID_DFontCurve, &IID_DFontCurveEvents);
- m_colorBackGround = RGB(0xFF, 0xFF, 0xFF);
- m_colorGridLine = RGB(0xC0, 0xC0, 0xC0); //灰色
- m_colorGrid = RGB(0x00, 0x00, 0x00);
- m_nGridHeightNum = GRID_LENGTH_16;
- m_nGridWidthNum = GRID_LENGTH_16;
- m_nGridLineWidth = WIDTH_GRID_LINE_INIT;
- memset(m_byShowBuf, 0x00, GRID_SHOW_BUF_LEN*sizeof(BYTE));
- m_nGridShowType = GRID_SHOW_POS_HORI;
- m_nGridDrawType = GRID_DRAW_RECT;
- m_nGridShowWidth = GRID_LENGTH_16;
- m_nGridShowHeight = GRID_LENGTH_16;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl::~CFontCurveCtrl - Destructor
- CFontCurveCtrl::~CFontCurveCtrl()
- {
- FreeAllResource();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl::OnDraw - Drawing function
- void CFontCurveCtrl::OnDraw(
- CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
- {
- //调整显示矩形区域
- int nWidth = rcBounds.Width();
- int nHeight = rcBounds.Height();
- if(nWidth < GRID_LENGTH_MIN)
- nWidth = GRID_LENGTH_MIN;
- if(nHeight < GRID_LENGTH_MIN)
- nHeight = GRID_LENGTH_MIN;
- if(nWidth > nHeight)
- nWidth = nHeight;
- else
- nHeight = nWidth;
- int nGridWidth = (nWidth - m_nGridLineWidth * (m_nGridWidthNum + 1))/m_nGridWidthNum;
- int nWidthResidue = (nWidth - m_nGridLineWidth * (m_nGridWidthNum + 1))%m_nGridWidthNum;
- CRect rc(0,0,0,0);
- CMemDC memDC(pdc);
- CBrush * pBrush = NULL;
- if(nWidthResidue != 0)
- {
- rc = CRect(rcBounds.left, rcBounds.top, rcBounds.left+nWidth - nWidthResidue,
- rcBounds.top+nHeight - nWidthResidue);
- CBrush bkBrush(m_colorBackGround);
- pBrush = memDC.SelectObject(&bkBrush);
- memDC.FillRect(CRect(rc.left+rc.Width(), rc.top, rcBounds.right, rcBounds.bottom), &bkBrush);
- memDC.FillRect(CRect(rc.left, rc.top+rc.Height(), rcBounds.right, rcBounds.bottom), &bkBrush);
- memDC.SelectObject(pBrush);
- }
- {
- rc = CRect(rcBounds.left, rcBounds.top, rcBounds.left+nWidth, rcBounds.top+nHeight);
- }
- //填充背景色
- CBrush backBrush(m_colorBackGround);
- pBrush = memDC.SelectObject(&backBrush);
- memDC.FillRect(rc, &backBrush);
- //memDC.DrawEdge(rc,EDGE_ETCHED, BF_RECT);
- memDC.SelectObject(pBrush);
- //画背景网格
- DrawGrid(&memDC, rc);
- //利用数据填充指定网格
- FillGrid(&memDC, rc, m_colorGrid, m_nGridDrawType);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl::DoPropExchange - Persistence support
- void CFontCurveCtrl::DoPropExchange(CPropExchange* pPX)
- {
- ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
- COleControl::DoPropExchange(pPX);
- // TODO: Call PX_ functions for each persistent custom property.
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl::OnResetState - Reset control to default state
- void CFontCurveCtrl::OnResetState()
- {
- COleControl::OnResetState();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFontCurveCtrl message handlers
- /////////////////////////////////////////////////////////////////////////////
- bool CFontCurveCtrl::DrawGrid(CDC * pDC, CRect &rc)
- {
- ASSERT(pDC != NULL);
- CPen linePen(PS_SOLID, m_nGridLineWidth, m_colorGridLine);
- CPen * pPen = pDC ->SelectObject(&linePen);
- CPoint pt(rc.left, rc.top);
- int nWidth = rc.Width();
- int nHeight = rc.Height();
- int nGridWidth = (nWidth-m_nGridLineWidth*(m_nGridWidthNum+1))/m_nGridWidthNum;
- int nGridHeight = (nHeight-m_nGridLineWidth*(m_nGridHeightNum+1))/m_nGridHeightNum;
- //画垂直线
- for(int i = (pt.x + nGridWidth); i < (pt.x+nWidth); i = i + nGridWidth+m_nGridLineWidth)
- {
- pDC ->MoveTo(i, pt.y);
- pDC ->LineTo(i, pt.y + m_nGridHeightNum*(nGridHeight+m_nGridLineWidth));
- }
- //画水平线
- for(int j = (pt.y+nGridHeight); j < (pt.y+nHeight); j = j+nGridHeight+m_nGridLineWidth)
- {
- pDC ->MoveTo(pt.x, j);
- pDC ->LineTo(pt.x + m_nGridWidthNum*(nGridWidth+m_nGridLineWidth), j);
- }
- pDC ->SelectObject(pPen);
- return true;
- }
- /*
- 注意:
- 根据提供的数据缓冲来组建Grid信息显示队列,然后根据Grid信息显示队列进行绘图
- 显示顺序:
- 水平显示:比如 8*8的点阵
- 1 2 3 4 5 6 7 8
- 9 10 11 12 13 14 15 16
- .......................
- 57 58 59 60 61 62 63 64
- 垂直显示:比如 8*8的点阵
- 1 9 . 57
- 2 10 . 58
- 3 11 . 59
- 4 12 . 60
- 5 13 . 61
- 6 14 . 62
- 7 15 . 63
- 8 16 . 64
- */
- bool CFontCurveCtrl::FillGrid(CDC * pDC, CRect &rc, COLORREF color, int nDrawType)
- {
- ASSERT(pDC != NULL);
- bool bIsEmpty = true;
- for(int i = 0; i <= GRID_SHOW_BUF_LEN; i++)
- {
- if(m_byShowBuf[i] != 0x00)
- bIsEmpty = false;
- }
- if(bIsEmpty)
- return true;
- int x = 0, y = 0;
- bool bDraw = false;
- CURVE_SHOW_INFO * pNewInfo = NULL;
- int nOneGridWidth = (rc.Width() - (m_nGridWidthNum+1)*m_nGridLineWidth)/m_nGridWidthNum;
- int nOneGridHeight = (rc.Height() - (m_nGridHeightNum+1)*m_nGridLineWidth)/m_nGridHeightNum;
- int nWidth8Count = m_nGridShowWidth/8;
- for(y = 0; y < m_nGridShowHeight; y++)
- {
- for(x = 0; x < m_nGridShowWidth; x++)
- {
- bDraw = (m_byShowBuf[nWidth8Count*y + x/8]>>(7-x%8)%8) & 0x01;
- if(bDraw)
- {
- pNewInfo = new CURVE_SHOW_INFO;
- if(m_nGridShowType == GRID_SHOW_POS_HORI) //水平扫描
- {
- pNewInfo ->gridRect.left = rc.left+(x+1)*m_nGridLineWidth+x*nOneGridWidth;
- pNewInfo ->gridRect.top = rc.top+(y+1)*m_nGridLineWidth+y*nOneGridHeight;
- pNewInfo ->gridRect.right = rc.left+(x+1)*m_nGridLineWidth+(x+1)*nOneGridWidth;;
- pNewInfo ->gridRect.bottom = rc.top+(y+1)*m_nGridLineWidth+(y+1)*nOneGridHeight;;
- pNewInfo ->gridX = x+1;
- pNewInfo ->gridY = y+1;
- }
- else if(m_nGridShowType == GRID_SHOW_POS_VER) //垂直扫描
- {
- pNewInfo ->gridRect.left = rc.left+(y+1)*m_nGridLineWidth+y*nOneGridHeight;
- pNewInfo ->gridRect.top = rc.top+(x+1)*m_nGridLineWidth+x*nOneGridWidth;
- pNewInfo ->gridRect.right = rc.left+(y+1)*m_nGridLineWidth+(y+1)*nOneGridHeight;
- pNewInfo ->gridRect.bottom = rc.top+(x+1)*m_nGridLineWidth+(x+1)*nOneGridWidth;
- pNewInfo ->gridX = y+1;
- pNewInfo ->gridY = x+1;
- }
- m_gridList.AddTail(pNewInfo);
- }
- }
- }
- CBrush backBrush(color);
- CBrush *pBrush = pDC ->SelectObject(&backBrush);
- POSITION pos = m_gridList.GetHeadPosition();
- while(pos)
- {
- CURVE_SHOW_INFO *pInfo = m_gridList.GetNext(pos);
- if(nDrawType == GRID_DRAW_RECT)
- pDC ->FillRect(pInfo ->gridRect, &backBrush);
- else if(nDrawType == GRID_DRAW_CIR)
- pDC ->FillRect(pInfo ->gridRect, &backBrush);
- }
- pDC ->SelectObject(pBrush);
- //释放所有队列资源
- FreeAllResource();
- return true;
- }
- bool CFontCurveCtrl::FreeAllResource()
- {
- POSITION pos = m_gridList.GetHeadPosition();
- while(pos)
- {
- CURVE_SHOW_INFO * pInfo = m_gridList.GetNext(pos);
- delete pInfo;
- }
- m_gridList.RemoveAll();
- return true;
- }
- bool CFontCurveCtrl::ShowData(BYTE * pInData, int nWidth, int nHeight, int nShowType)
- {
- if(pInData == NULL)
- return false;
- if((nWidth == 0) || (nHeight == 0))
- return false;
- memset(m_byShowBuf, 0x00, GRID_SHOW_BUF_LEN*sizeof(BYTE));
- memcpy(m_byShowBuf, pInData, nWidth*nHeight*sizeof(BYTE));
- m_nGridShowType = nShowType;
- m_nGridShowWidth = nWidth;
- m_nGridShowHeight = nHeight;
- InvalidateControl();
- SetModifiedFlag();
- return true;
- }
- bool CFontCurveCtrl::JudgeGridNum()
- {
- if(m_nGridWidthNum <= GRID_LENGTH_8)
- m_nGridWidthNum = GRID_LENGTH_8;
- else if(m_nGridWidthNum <= GRID_LENGTH_16)
- m_nGridWidthNum = GRID_LENGTH_16;
- else if(m_nGridWidthNum <= GRID_LENGTH_24)
- m_nGridWidthNum = GRID_LENGTH_24;
- else if(m_nGridWidthNum <= GRID_LENGTH_32)
- m_nGridWidthNum = GRID_LENGTH_32;
- if(m_nGridHeightNum <= GRID_LENGTH_8)
- m_nGridHeightNum = GRID_LENGTH_8;
- else if(m_nGridHeightNum <= GRID_LENGTH_16)
- m_nGridHeightNum = GRID_LENGTH_16;
- else if(m_nGridHeightNum <= GRID_LENGTH_24)
- m_nGridHeightNum = GRID_LENGTH_24;
- else if(m_nGridHeightNum <= GRID_LENGTH_32)
- m_nGridHeightNum = GRID_LENGTH_32;
- if(m_nGridWidthNum > m_nGridHeightNum)
- m_nGridWidthNum = m_nGridHeightNum;
- else
- m_nGridHeightNum = m_nGridWidthNum;
- return true;
- }
- void CFontCurveCtrl::SetBackColor(OLE_COLOR backColor)
- {
- m_colorBackGround = TranslateColor(backColor);
- InvalidateControl();
- SetModifiedFlag();
- }
- void CFontCurveCtrl::SetGridLineColor(OLE_COLOR gridlineColor)
- {
- m_colorGridLine = TranslateColor(gridlineColor);
- InvalidateControl();
- SetModifiedFlag();
- }
- void CFontCurveCtrl::SetGridColor(OLE_COLOR gridColor)
- {
- m_colorGrid = TranslateColor(gridColor);
- InvalidateControl();
- SetModifiedFlag();
- }
- void CFontCurveCtrl::SetGridHoriNum(short horiNum)
- {
- m_nGridWidthNum = horiNum;
- InvalidateControl();
- SetModifiedFlag();
- }
- void CFontCurveCtrl::SetGridVerNum(short verNum)
- {
- m_nGridHeightNum = verNum;
- InvalidateControl();
- SetModifiedFlag();
- }
- void CFontCurveCtrl::SetGridShowType(short showType)
- {
- m_nGridShowType = showType;
- InvalidateControl();
- SetModifiedFlag();
- }
- void CFontCurveCtrl::SetGridDrawType(short drawType)
- {
- m_nGridDrawType = drawType;
- InvalidateControl();
- SetModifiedFlag();
- }
- void CFontCurveCtrl::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
- {
- if((lpncsp ->rgrc[0].bottom - lpncsp ->rgrc[0].top) > (lpncsp ->rgrc[0].right - lpncsp ->rgrc[0].left))
- lpncsp ->rgrc[0].right = lpncsp ->rgrc[0].left+(lpncsp ->rgrc[0].bottom - lpncsp ->rgrc[0].top);
- else
- lpncsp ->rgrc[0].bottom = lpncsp ->rgrc[0].top + (lpncsp ->rgrc[0].right - lpncsp ->rgrc[0].left);
- COleControl::OnNcCalcSize(bCalcValidRects, lpncsp);
- }
- void CFontCurveCtrl::OnSize(UINT nType, int cx, int cy)
- {
- COleControl::OnSize(nType, cx, cy);
- }
- BOOL CFontCurveCtrl::ShowGridData(BSTR FAR* pInData, short nWidth, short nHeight, short nShowType)
- {
- return ShowData((BYTE *)pInData, nWidth, nHeight, nShowType);
- }