none
vc++MFC-SDI如何实现打开文件然后在view显示? RRS feed

  • 问题

  • 我实现了打开文件在view显示,但是一滚动文字有些就没有了,该如何解决。实现代码如下:

    void C路桥工程View::OnFileOpen()
    {
     // TODO:  在此添加命令处理程序代码
     if (!sum2.empty())
     {
      sum2.clear();
     }

     TCHAR szFilters[] = _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||");
     CFileDialog fileDlg(TRUE, _T("Text Files"), _T("*.txt"),
      OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

     /*if (fileDlg.DoModal() == IDOK)
     {
      CString pathName = fileDlg.GetPathName();

      // Implement opening and reading file in here.

      //Change the window's title to the opened file's title.
      CString fileName = fileDlg.GetFileTitle();

      SetWindowText(fileName);
     }*/

     /*CString title1;
     title1 = "打开文件";
     CString filter1;
     filter1 = "Text Files(*.txt)";
     fileDlg.m_ofn.lpstrTitle = title1;
     fileDlg.m_ofn.lpstrFilter = filter1;*/
      
     if (IDOK == fileDlg.DoModal())
     {
      CFile file(fileDlg.GetFileName(), CFile::modeRead);
      char *pBuf;
      CString duzi1;
      DWORD dwFileLen;
      int wzcd1;
      dwFileLen = file.GetLength();
      wzcd1 = file.GetLength();
      pBuf = new char[dwFileLen+1];
      pBuf[dwFileLen] = 0;
      file.Read(pBuf, dwFileLen);
      file.Close();
      //MessageBox((CString)pBuf);

      duzi1 = pBuf;
      int number1=duzi1.GetLength();
      //char* array=new char(number1);
      CString array1;
      for(int i2=0;i2<number1;i2++)
      { 
       array1 = duzi1.GetAt(i2);//输出的字符
       sum2.push_back(array1);
      }

      CDC* pDC1=GetDC();
      TEXTMETRIC tm2;
      pDC1->GetTextMetrics(&tm2);
      int i1=0;
      int p1=0;
      int f1=0;
      int i3 = sum2.size();
      int p2 = (int)(1024 / tm2.tmMaxCharWidth);
      int f2 = (int)(i3 / p2);
      while (i1 < i3)
      {
       for (f1 = 0; f1 <= f2; f1++)
       {
        for (p1 = 0; p1 < p2&i1<i3; p1++)
        {
         pDC1->TextOut(tm2.tmMaxCharWidth*p1, tm2.tmHeight*f1, sum2[i1]);
         i1++;
              
        }


       }
       
      }
      SetScrollSizes(MM_TEXT, CSize(1024, tm2.tmHeight*f1));
        
             
     }

    }

    2015年2月9日 6:54

答案