CText2View.cpp
上传用户:jxd368
上传日期:2013-06-20
资源大小:66k
文件大小:8k
源码类别:

文本生成

开发平台:

Visual C++

  1. // CText2View.cpp : CCText2View 类的实现
  2. //
  3. #include "stdafx.h"
  4. #include "CText2.h"
  5. #include "CText2Doc.h"
  6. #include "CText2View.h"
  7. #include ".ctext2view.h"
  8. #include "BitVector.h"
  9. #include <math.h>
  10. //#include "TextDialog.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #endif
  14. // CCText2View
  15. IMPLEMENT_DYNCREATE(CCText2View, CView)
  16. BEGIN_MESSAGE_MAP(CCText2View, CView)
  17. // 标准打印命令
  18. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  19. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  20. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  21. ON_WM_TIMER()
  22. ON_WM_CREATE()
  23. ON_COMMAND(ID_Menu, OnMenu)
  24. //ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
  25. ON_WM_ERASEBKGND()
  26. ON_COMMAND(ID_MYMENU_FONT, OnMymenuFont)
  27. ON_COMMAND(ID_ALIGN_LEFT, OnAlignLeft)
  28. ON_COMMAND(ID_ALIGN_RIGHT, OnAlignRight)
  29. ON_COMMAND(ID_ALIN_CENTER, OnAlinCenter)
  30. END_MESSAGE_MAP()
  31. // CCText2View 构造/析构
  32. CCText2View::CCText2View()
  33. {
  34. // TODO: 在此处添加构造代码
  35.    posX=0;
  36.    posY=0;
  37.    speed=50;
  38.    size=30;
  39.    model=1;
  40.    bModify=FALSE;
  41.    m_cTextColor=RGB(0,0,255);// 蓝色
  42.    m_lf.lfHeight=size;
  43.    m_lf.lfWeight=0;
  44.    m_lf.lfEscapement=0;
  45.    m_lf.lfOrientation=0;
  46.    m_lf.lfWeight=FW_NORMAL;
  47.    m_lf.lfItalic=FALSE; 
  48.    m_lf.lfUnderline=FALSE;
  49.    m_lf.lfStrikeOut=FALSE;
  50.    m_lf.lfCharSet=GB2312_CHARSET;
  51.    m_lf.lfOutPrecision=OUT_STROKE_PRECIS;
  52.    m_lf.lfClipPrecision=CLIP_STROKE_PRECIS;
  53.    m_lf.lfQuality=DEFAULT_QUALITY;
  54.    m_lf.lfPitchAndFamily=VARIABLE_PITCH|FF_MODERN;
  55.    strcpy(m_lf.lfFaceName,"仿宋_GB2312");
  56.    }
  57. CCText2View::~CCText2View()
  58. {
  59. }
  60. BOOL CCText2View::PreCreateWindow(CREATESTRUCT& cs)
  61. {
  62. // TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
  63. // 样式
  64. return CView::PreCreateWindow(cs);
  65. }
  66. // CCText2View 绘制
  67. void CCText2View::OnDraw(CDC*pDC)
  68. {
  69. CCText2Doc* pDoc = GetDocument();
  70. ASSERT_VALID(pDoc);
  71. if (!pDoc)
  72. return;
  73. int rowCount ;
  74.     CRect rect,tRect;
  75. GetClientRect(&rect);
  76.     width=rect.Width();
  77. height=rect.Height();
  78.     CDC* memDC=new CDC;
  79. memDC->CreateCompatibleDC(pDC);
  80. CBitmap* memBitMap=new CBitmap;
  81. memBitMap->CreateCompatibleBitmap(pDC,rect.right-rect.left,rect.bottom-rect.top);
  82. CBrush* pBrush=new CBrush;
  83. pBrush->CreateSolidBrush(RGB(255,255,255));
  84. CBitmap* pOldMap=memDC->SelectObject(memBitMap);
  85. memDC->FillRect(&rect,pBrush);
  86.   
  87. CFont myFont;
  88. myFont.CreateFontIndirect(&m_lf);
  89. memDC->SelectObject(&myFont);
  90. TEXTMETRIC tmetric;
  91. memDC->GetTextMetrics(&tmetric);
  92. columnCount=width/(2*tmetric.tmAveCharWidth);
  93. size=(int)fabs((float)m_lf.lfHeight);
  94. //if(bModify)
  95. //{
  96.  rowCount=count(oldString,columnCount);
  97. // bModify=false;
  98. //}
  99. //else
  100.    // rowCount=count(textString,columnCount);
  101. //rowCount=count(textString,columnCount);
  102. tRect.SetRect(posX,posY,posX+rect.Width(),posY+size*rowCount);
  103. if(posY<-size*(rowCount+1))
  104. posY=rect.Height()-1;
  105. //pDC->SetBkColor(RGB(255,0,0));
  106. memDC->SetTextColor(m_cTextColor);
  107. memDC->DrawText(textString,textString.GetLength(),&tRect,model);
  108.     pDC->BitBlt(0,0,rect.right-rect.left,rect.bottom-rect.top,memDC,0,0,SRCCOPY);
  109. memDC->SelectObject(pOldMap);
  110. delete memBitMap;
  111. delete memDC;
  112. delete pBrush;
  113. ReleaseDC(pDC);
  114.    
  115. }
  116. // CCText2View 打印
  117. BOOL CCText2View::OnPreparePrinting(CPrintInfo* pInfo)
  118. {
  119. // 默认准备
  120. return DoPreparePrinting(pInfo);
  121. }
  122. void CCText2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  123. {
  124. // TODO: 打印前添加额外的初始化
  125. }
  126. void CCText2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  127. {
  128. // TODO: 打印后添加清除过程
  129. }
  130. int CCText2View::count(CString string,int rowNum)
  131. {
  132.  int i,length;
  133.  int count=0;
  134.  int countNum=0;//记录一行中字符的个数最多有rowNum个
  135.  int line=0;
  136.  char *la;
  137.  int max=1;
  138. // int add;
  139.  CString tempString="";
  140.  textString=string;
  141.  length=textString.GetLength();
  142.  
  143.  la=new char[length];
  144.  memcpy((void*)la,(void*)(textString.GetBuffer(length)),length*sizeof(char));
  145.  
  146.  CBitVector bArray(length);
  147.  bArray.clearAll();
  148.  
  149.  
  150.    /*if(countNum<columnCount)
  151.    {
  152.    if(la[i]>=0&&la[i]<=255&&la[i]!='n')
  153.    countNum++;
  154.    else if(la[i]<0||la[i]>255)
  155.    {
  156.     countNum++;
  157. i++;
  158.    }
  159.    else {
  160.    count++;
  161.    countNum=0;
  162.    continue;
  163.     }
  164.    }
  165.    if(countNum==columnCount)
  166.    {
  167. textString.Insert(i,"nr");
  168.     countNum=0;
  169. count++;
  170.    }
  171.    if(count*size+posY>height*2)
  172.    {   delete[]la;
  173.    return count;}*/
  174.  for(i=0;i<length;i++)
  175.  { 
  176.   line++;
  177.   if(la[i]>=0&&la[i]<=255)
  178.   { 
  179.   bArray.set(i,true);
  180.   }
  181.   if(la[i]=='n'&&la[i+1]=='r')
  182.   {   count++;
  183.       int charNumber=columnCount*2;
  184.   int lf=0;
  185.   lf=i;
  186.   
  187.   if(line>charNumber)
  188.   {
  189.  int k=(line-1)/charNumber;
  190.  if(!((line-1)%charNumber))k--;
  191.  
  192.  int b;
  193.  
  194.  for(int j=1;j<=k;j++)
  195.  {
  196.   b=0;
  197.   int m;
  198.   for(m=0;m<charNumber;m++)
  199.   if(!bArray[lf-line+1+(j-1)*charNumber+m])
  200.   {
  201.   b++;
  202.   max=m+1;
  203.   }
  204.  CString string0=textString.Mid(lf-line+1+(j-1)*charNumber,charNumber);
  205.  if(b%2){string0.Delete(string0.GetLength()-1);string0.Insert(string0.GetLength(),"n");lf--;}
  206.  else string0.Insert(string0.GetLength(),"n");
  207.  tempString+=string0;
  208.  count++;
  209.  }
  210.  int add=line-k*charNumber+(i-lf);
  211.  CString string1=textString.Mid(lf-line+1+k*charNumber,add);
  212.  tempString+=string1;
  213.  max=0;
  214.   }
  215.   else
  216.   {
  217.    CString string2=textString.Mid(i-line+1,line);
  218.    tempString+=string2;
  219.   }
  220.  line=0;
  221. if((count-1)*size+posY>height)
  222.    {   textString=tempString;
  223. delete[]la;
  224. return count;
  225. }
  226.   }
  227.  }
  228.  textString=tempString;
  229.  delete[]la;
  230.  return count;
  231. }
  232. // CCText2View 诊断
  233. #ifdef _DEBUG
  234. void CCText2View::AssertValid() const
  235. {
  236. CView::AssertValid();
  237. }
  238. void CCText2View::Dump(CDumpContext& dc) const
  239. {
  240. CView::Dump(dc);
  241. }
  242. CCText2Doc* CCText2View::GetDocument() const // 非调试版本是内联的
  243. {
  244. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCText2Doc)));
  245. return (CCText2Doc*)m_pDocument;
  246. }
  247. #endif //_DEBUG
  248. // CCText2View 消息处理程序
  249. void CCText2View::OnTimer(UINT nIDEvent)
  250. {
  251. // TODO: 在此添加消息处理程序代码和/或调用默认值
  252. posY--;
  253. //SetTimer(1,speed,NULL);
  254.     Invalidate(false);
  255. CView::OnTimer(nIDEvent);
  256. }
  257. int CCText2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
  258. {
  259. if (CView::OnCreate(lpCreateStruct) == -1)
  260. return -1;
  261.    
  262. // TODO:  在此添加您专用的创建代码
  263. CRect rect;
  264. GetClientRect(&rect);
  265. posY=rect.Height()-1;
  266. OnMenu();
  267.     SetTimer(1,speed,NULL);
  268. return 0;
  269. }
  270. void CCText2View::OnMenu()
  271. {
  272. // TODO: 在此添加命令处理程序代码
  273. if(textDialog.DoModal()==IDOK)
  274. {
  275.  textString=textDialog.m_str;
  276.  oldString=textDialog.m_str;
  277.  if(textDialog.m_speed1>0)
  278.  speed=textDialog.m_speed1;
  279.  if(textDialog.m_iTextSize>0)
  280.  size=textDialog.m_iTextSize;
  281.  m_lf.lfHeight=size;
  282.   SetTimer(1,speed,NULL);
  283. }
  284. }
  285. BOOL CCText2View::OnEraseBkgnd(CDC* pDC)
  286. {
  287. // TODO: 在此添加消息处理程序代码和/或调用默认值
  288. //OnEraseBkgnd(pDC);
  289.     return true;
  290. }
  291. void CCText2View::OnMymenuFont()
  292. {
  293. // TODO: 在此添加命令处理程序代码
  294. CFontDialog dlg;
  295. dlg.m_cf.lpLogFont=&m_lf;
  296. dlg.m_cf.rgbColors=m_cTextColor;
  297. dlg.m_cf.Flags|=CF_INITTOLOGFONTSTRUCT;
  298.     bModify=true;
  299. if(dlg.DoModal()==IDOK)
  300. {
  301.  dlg.GetCurrentFont(&m_lf);
  302.  m_cTextColor=dlg.GetColor();
  303.  Invalidate();
  304.  }
  305. }
  306. void CCText2View::OnAlignLeft()
  307. {
  308. // TODO: 在此添加命令处理程序代码
  309. model=0;
  310. }
  311. void CCText2View::OnAlignRight()
  312. {
  313. // TODO: 在此添加命令处理程序代码
  314. model=2;
  315. }
  316. void CCText2View::OnAlinCenter()
  317. {
  318. // TODO: 在此添加命令处理程序代码
  319. model=1;
  320. }