CText2View.cpp
资源名称:CText2.rar [点击查看]
上传用户:jxd368
上传日期:2013-06-20
资源大小:66k
文件大小:8k
源码类别:
文本生成
开发平台:
Visual C++
- // CText2View.cpp : CCText2View 类的实现
- //
- #include "stdafx.h"
- #include "CText2.h"
- #include "CText2Doc.h"
- #include "CText2View.h"
- #include ".ctext2view.h"
- #include "BitVector.h"
- #include <math.h>
- //#include "TextDialog.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // CCText2View
- IMPLEMENT_DYNCREATE(CCText2View, CView)
- BEGIN_MESSAGE_MAP(CCText2View, CView)
- // 标准打印命令
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- ON_WM_TIMER()
- ON_WM_CREATE()
- ON_COMMAND(ID_Menu, OnMenu)
- //ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
- ON_WM_ERASEBKGND()
- ON_COMMAND(ID_MYMENU_FONT, OnMymenuFont)
- ON_COMMAND(ID_ALIGN_LEFT, OnAlignLeft)
- ON_COMMAND(ID_ALIGN_RIGHT, OnAlignRight)
- ON_COMMAND(ID_ALIN_CENTER, OnAlinCenter)
- END_MESSAGE_MAP()
- // CCText2View 构造/析构
- CCText2View::CCText2View()
- {
- // TODO: 在此处添加构造代码
- posX=0;
- posY=0;
- speed=50;
- size=30;
- model=1;
- bModify=FALSE;
- m_cTextColor=RGB(0,0,255);// 蓝色
- m_lf.lfHeight=size;
- m_lf.lfWeight=0;
- m_lf.lfEscapement=0;
- m_lf.lfOrientation=0;
- m_lf.lfWeight=FW_NORMAL;
- m_lf.lfItalic=FALSE;
- m_lf.lfUnderline=FALSE;
- m_lf.lfStrikeOut=FALSE;
- m_lf.lfCharSet=GB2312_CHARSET;
- m_lf.lfOutPrecision=OUT_STROKE_PRECIS;
- m_lf.lfClipPrecision=CLIP_STROKE_PRECIS;
- m_lf.lfQuality=DEFAULT_QUALITY;
- m_lf.lfPitchAndFamily=VARIABLE_PITCH|FF_MODERN;
- strcpy(m_lf.lfFaceName,"仿宋_GB2312");
- }
- CCText2View::~CCText2View()
- {
- }
- BOOL CCText2View::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
- // 样式
- return CView::PreCreateWindow(cs);
- }
- // CCText2View 绘制
- void CCText2View::OnDraw(CDC*pDC)
- {
- CCText2Doc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- if (!pDoc)
- return;
- int rowCount ;
- CRect rect,tRect;
- GetClientRect(&rect);
- width=rect.Width();
- height=rect.Height();
- CDC* memDC=new CDC;
- memDC->CreateCompatibleDC(pDC);
- CBitmap* memBitMap=new CBitmap;
- memBitMap->CreateCompatibleBitmap(pDC,rect.right-rect.left,rect.bottom-rect.top);
- CBrush* pBrush=new CBrush;
- pBrush->CreateSolidBrush(RGB(255,255,255));
- CBitmap* pOldMap=memDC->SelectObject(memBitMap);
- memDC->FillRect(&rect,pBrush);
- CFont myFont;
- myFont.CreateFontIndirect(&m_lf);
- memDC->SelectObject(&myFont);
- TEXTMETRIC tmetric;
- memDC->GetTextMetrics(&tmetric);
- columnCount=width/(2*tmetric.tmAveCharWidth);
- size=(int)fabs((float)m_lf.lfHeight);
- //if(bModify)
- //{
- rowCount=count(oldString,columnCount);
- // bModify=false;
- //}
- //else
- // rowCount=count(textString,columnCount);
- //rowCount=count(textString,columnCount);
- tRect.SetRect(posX,posY,posX+rect.Width(),posY+size*rowCount);
- if(posY<-size*(rowCount+1))
- posY=rect.Height()-1;
- //pDC->SetBkColor(RGB(255,0,0));
- memDC->SetTextColor(m_cTextColor);
- memDC->DrawText(textString,textString.GetLength(),&tRect,model);
- pDC->BitBlt(0,0,rect.right-rect.left,rect.bottom-rect.top,memDC,0,0,SRCCOPY);
- memDC->SelectObject(pOldMap);
- delete memBitMap;
- delete memDC;
- delete pBrush;
- ReleaseDC(pDC);
- }
- // CCText2View 打印
- BOOL CCText2View::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // 默认准备
- return DoPreparePrinting(pInfo);
- }
- void CCText2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: 打印前添加额外的初始化
- }
- void CCText2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: 打印后添加清除过程
- }
- int CCText2View::count(CString string,int rowNum)
- {
- int i,length;
- int count=0;
- int countNum=0;//记录一行中字符的个数最多有rowNum个
- int line=0;
- char *la;
- int max=1;
- // int add;
- CString tempString="";
- textString=string;
- length=textString.GetLength();
- la=new char[length];
- memcpy((void*)la,(void*)(textString.GetBuffer(length)),length*sizeof(char));
- CBitVector bArray(length);
- bArray.clearAll();
- /*if(countNum<columnCount)
- {
- if(la[i]>=0&&la[i]<=255&&la[i]!='n')
- countNum++;
- else if(la[i]<0||la[i]>255)
- {
- countNum++;
- i++;
- }
- else {
- count++;
- countNum=0;
- continue;
- }
- }
- if(countNum==columnCount)
- {
- textString.Insert(i,"nr");
- countNum=0;
- count++;
- }
- if(count*size+posY>height*2)
- { delete[]la;
- return count;}*/
- for(i=0;i<length;i++)
- {
- line++;
- if(la[i]>=0&&la[i]<=255)
- {
- bArray.set(i,true);
- }
- if(la[i]=='n'&&la[i+1]=='r')
- { count++;
- int charNumber=columnCount*2;
- int lf=0;
- lf=i;
- if(line>charNumber)
- {
- int k=(line-1)/charNumber;
- if(!((line-1)%charNumber))k--;
- int b;
- for(int j=1;j<=k;j++)
- {
- b=0;
- int m;
- for(m=0;m<charNumber;m++)
- if(!bArray[lf-line+1+(j-1)*charNumber+m])
- {
- b++;
- max=m+1;
- }
- CString string0=textString.Mid(lf-line+1+(j-1)*charNumber,charNumber);
- if(b%2){string0.Delete(string0.GetLength()-1);string0.Insert(string0.GetLength(),"n");lf--;}
- else string0.Insert(string0.GetLength(),"n");
- tempString+=string0;
- count++;
- }
- int add=line-k*charNumber+(i-lf);
- CString string1=textString.Mid(lf-line+1+k*charNumber,add);
- tempString+=string1;
- max=0;
- }
- else
- {
- CString string2=textString.Mid(i-line+1,line);
- tempString+=string2;
- }
- line=0;
- if((count-1)*size+posY>height)
- { textString=tempString;
- delete[]la;
- return count;
- }
- }
- }
- textString=tempString;
- delete[]la;
- return count;
- }
- // CCText2View 诊断
- #ifdef _DEBUG
- void CCText2View::AssertValid() const
- {
- CView::AssertValid();
- }
- void CCText2View::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CCText2Doc* CCText2View::GetDocument() const // 非调试版本是内联的
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCText2Doc)));
- return (CCText2Doc*)m_pDocument;
- }
- #endif //_DEBUG
- // CCText2View 消息处理程序
- void CCText2View::OnTimer(UINT nIDEvent)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- posY--;
- //SetTimer(1,speed,NULL);
- Invalidate(false);
- CView::OnTimer(nIDEvent);
- }
- int CCText2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: 在此添加您专用的创建代码
- CRect rect;
- GetClientRect(&rect);
- posY=rect.Height()-1;
- OnMenu();
- SetTimer(1,speed,NULL);
- return 0;
- }
- void CCText2View::OnMenu()
- {
- // TODO: 在此添加命令处理程序代码
- if(textDialog.DoModal()==IDOK)
- {
- textString=textDialog.m_str;
- oldString=textDialog.m_str;
- if(textDialog.m_speed1>0)
- speed=textDialog.m_speed1;
- if(textDialog.m_iTextSize>0)
- size=textDialog.m_iTextSize;
- m_lf.lfHeight=size;
- SetTimer(1,speed,NULL);
- }
- }
- BOOL CCText2View::OnEraseBkgnd(CDC* pDC)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- //OnEraseBkgnd(pDC);
- return true;
- }
- void CCText2View::OnMymenuFont()
- {
- // TODO: 在此添加命令处理程序代码
- CFontDialog dlg;
- dlg.m_cf.lpLogFont=&m_lf;
- dlg.m_cf.rgbColors=m_cTextColor;
- dlg.m_cf.Flags|=CF_INITTOLOGFONTSTRUCT;
- bModify=true;
- if(dlg.DoModal()==IDOK)
- {
- dlg.GetCurrentFont(&m_lf);
- m_cTextColor=dlg.GetColor();
- Invalidate();
- }
- }
- void CCText2View::OnAlignLeft()
- {
- // TODO: 在此添加命令处理程序代码
- model=0;
- }
- void CCText2View::OnAlignRight()
- {
- // TODO: 在此添加命令处理程序代码
- model=2;
- }
- void CCText2View::OnAlinCenter()
- {
- // TODO: 在此添加命令处理程序代码
- model=1;
- }
English
