• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 29,comments - 10,trackbacks - 0
            1、建立名為HexViewer的SDI項目,在創建單文檔時把Base Class(基類)改為CScrollView。CScrollView就是CView的一個派生類,通過它可以很容易地處理水平和垂直滾動。
            2、打開和讀取文件
                  首先,在CHexViewerDoc類中定義兩個成員變量,分別用于檢索要打開文件的指針以及文件的大小,并初始化這些值,同時添加用于清除的代碼。
            class CHexViewerDoc : public CDocument
            {
            ..
            public:
                CFile
            * m_pFile;
                LONG m_lFileSize;
            ..
            };

            CHexViewerDoc::CHexViewerDoc()
            {
                m_pFile
            =NULL;
                m_lFileSize
            =0L;
            }

            CHexViewerDoc::
            ~CHexViewerDoc()
            {
                
            if (m_pFile!=NULL)
                {
                    m_pFile
            ->Close();
                    delete m_pFile;
                    m_pFile
            =NULL;
                    m_lFileSize
            =0L;
                }
            }
                  在使用MFC框架的因公程序中打開文檔后,還會調用CHexViewerDoc::OnOpenDocument虛函數。
            BOOL CHexViewerDoc::OnOpenDocument(LPCTSTR lpszPathName) 
            {
                
            if (!CDocument::OnOpenDocument(lpszPathName))
                    
            return FALSE;

                
            if (m_pFile!=NULL)
                {
                    m_pFile
            ->Close();
                    delete m_pFile;
                    m_pFile
            =NULL;
                    m_lFileSize
            =0L;
                }

                
            try
                {
                    m_pFile
            =new CFile(lpszPathName,CFile::modeRead|CFile::typeBinary);
                }
                
            catch(CFileException* e)
                {
                    CString strError;
                    strError.Format(_T(
            "Couldn't open file:%d"),_sys_errlist[e->m_lOsError]);
                    AfxMessageBox(strError);
                    
            return FALSE;
                }

                m_lFileSize
            =m_pFile->GetLength();
                
            return TRUE;
            }
                  最后添加一個輔助成員函數,視圖調用該函數從打開的文件中讀取一行數據。
            BOOL CHexViewerDoc::ReadLine(CString& strLine,int nLength,LONG lOffset/* =-1L */)
            {
                LONG lPosition;

                
            if(lOffset!=-1L)
                    lPosition
            =m_pFile->Seek(lOffset,CFile::begin);
                
            else
                    lPosition
            =m_pFile->GetPosition();

                
            if(lPosition==-1L)
                {
                    TRACE(
            "CHexViewDoc::ReadLine returns False Seek (%8.8lX,%8.8lX)\n",lOffset,lPosition);
                    
            //這個宏是用來調試用的,在VC的窗口的輸入框中會看到輸出的內容!只有在debug版本中才會運行 
                    return FALSE;
                }

                BYTE
            * pszBuffer=new BYTE[nLength]; 
                
            int nReturned=m_pFile->Read(pszBuffer,nLength);

                
            if(nReturned<=0)
                {
                    TRACE2(
            "CHexViewDoc::ReadLine returns FALSE Read (%d,%d)",nLength,nReturned);
                    delete pszBuffer;
                    
            return FALSE;
                }

                CString strTemp;
                CString strCharsIn;

                strTemp.Format(_T(
            "%8.8lX - "),lPosition);
                strLine
            =strTemp;

                
            for(int nIndex=0;nIndex<nReturned;nIndex++)
                {
                    
            if(nIndex==0)
                        strTemp.Format(_T(
            "%2.2X"),pszBuffer[nIndex]);
                    
            //以2位的十六進制讀取
                    else if(nIndex%16==0)
                        strTemp.Format(_T(
            "=%2.2X"),pszBuffer[nIndex]);
                    
            else if(nIndex%8==0)
                        strTemp.Format(_T(
            "-%2.2X"),pszBuffer[nIndex]);
                    
            else
                        strTemp.Format(_T(
            " %2.2X"),pszBuffer[nIndex]);

                    
            if(_istprint(pszBuffer[nIndex]))
                        strCharsIn
            +=pszBuffer[nIndex];
                    
            else
                        strCharsIn
            +=_T('.');
                    strLine
            +=strTemp;
                }
                
            if(nReturned<nLength)
                {
                    CString strPadding(_T(
            ' '),3*(nLength-nReturned));
                    strLine
            +=strPadding;
                }
                strLine
            +=_T("  ");
                strLine
            +=strCharsIn;

                delete pszBuffer;
                
            return true;
            }
            3、視圖的編碼
                  首先,定義一些變量,并對數據的字體進行改變
            class CHexViewerView : public CScrollView
            {
            .
            protected:
                CFont
            * m_pFont;
                LOGFONT m_logfont;
                
            int m_nPointSize;
                
            int m_nPageHeight;
                
            int m_nPageWidth;
            .
            };

            CHexViewerView::CHexViewerView()
            {
                
            // TODO: add construction code here
                memset(&m_logfont,0,sizeof(m_logfont));
                m_nPointSize
            =120;
                _tcscpy(m_logfont.lfFaceName,_T(
            "Fixedsys"));
                
            //函數原型:char *strcpy( char *strDestination, const char *strSource );


                CWindowDC dc(NULL);
                m_logfont.lfHeight
            =::MulDiv(m_nPointSize,dc.GetDeviceCaps(LOGPIXELSY),720);
                
            //MulDiv字體高度值和磅值有如下的換算公式;GetDeviceCaps該函數檢索指定設備的設備指定信息。
                
            //LOGPIXELSY沿屏幕高度每邏輯英寸的像素數,在多顯示器系統中,該值對所顯示器相同;
                m_logfont.lfPitchAndFamily=FIXED_PITCH;//指定字體間距和字體族,低端二位指定字體的字符間距
                m_pFont=new CFont;
                m_pFont
            ->CreateFontIndirect(&m_logfont);
            }

            CHexViewerView::
            ~CHexViewerView()
            {
                
            if (m_pFont!=NULL)
                {
                    delete m_pFont;
                }
            }
                  設置滾動的大小,可以向其傳遞映射模式(MM_TEXT顯示文本)和文檔大小(m_lFileSize)
            void CHexViewerView::OnInitialUpdate()
            {
                CScrollView::OnInitialUpdate();

                CHexViewerDoc
            * pDoc=GetDocument();
                ASSERT_VALID(pDoc);

                CSize sizeTotal(
            0,pDoc->m_lFileSize);
                
            //sizeTotal.cx = sizeTotal.cy = 100;
                SetScrollSizes(MM_TEXT, sizeTotal);
            }
                  給視圖添加為當前設備上下文(DC)計算字體高度(以像素計)的輔助函數。這里的技巧是傳遞-1作為字符串長度。這意味著不進行任何實際繪制操作,但是DrawText函數仍返回指定文本的高度。
            int CHexViewerView::MeasureFontHeight(CFont* pFont,CDC* pDC)
            {
                CFont
            * pOldFont;
                pOldFont
            =pDC->SelectObject(pFont);

                CRect rectDummy;
                CString strRender
            =_T("1234567890ABCDEF- ");
                
            int nHeight=pDC->DrawText(strRender,-1,rectDummy,DT_TOP|DT_SINGLELINE|DT_CALCRECT);

                pDC
            ->SelectObject(pOldFont);

                
            return nHeight;
                  最后隊試圖進行修改,用GetScrollPosition返回用戶滾動的位置,用GetClientRect來返回工作區的大小,用MeasureFontHeight返回每行的高度,然后可以用一個for循環通過ReadLine函數來從文檔中檢索每行數據。
            void CHexViewerView::OnDraw(CDC* pDC)
            {
                CHexViewerDoc
            * pDoc = GetDocument();
                ASSERT_VALID(pDoc);

                CString strRender;
                CFont
            * pOldFont;
                CSize ScrolledSize;
                
            int nStartLine;
                
            int nHeight;
                CRect ScrollRect;
                CPoint ScrolledPos
            =GetScrollPosition();//獲取該屏滾動條的位置


                CRect rectClient;
                GetClientRect(
            &rectClient);

                pOldFont
            =pDC->SelectObject(m_pFont);
                nHeight
            =MeasureFontHeight(m_pFont,pDC);

                ScrolledSize
            =CSize(rectClient.Width(),rectClient.Height());
                ScrollRect
            =CRect(rectClient.left,ScrolledPos.y,rectClient.right,ScrolledSize.cy+ScrolledPos.y);
                nStartLine
            =ScrolledPos.y/16;

                ScrollRect.top
            =nStartLine*nHeight;

                
            if(pDoc->m_pFile!=NULL)
                {
                    
            int nLine;
                    
            for(nLine=nStartLine;ScrollRect.top<ScrollRect.bottom;nLine++)
                    {
                        
            if(!pDoc->ReadLine(strRender,16,nLine*16))
                            
            break;

                        nHeight
            =pDC->DrawText(strRender,-1,&ScrollRect,DT_TOP|DT_NOPREFIX|DT_SINGLELINE);
                        ScrollRect.top
            +=nHeight;
                    }
                }

                pDC
            ->SelectObject(pOldFont);
            }
            posted on 2009-07-02 11:15 The_Moment 閱讀(1074) 評論(1)  編輯 收藏 引用 所屬分類: VC實踐

            FeedBack:
            # re: HexViewer[未登錄]
            2011-09-19 13:10 | a
            eeeeeeee  回復  更多評論
              
            亚洲精品无码久久一线| 亚洲欧美成人久久综合中文网| 精品一二三区久久aaa片| 女人高潮久久久叫人喷水| 久久久亚洲AV波多野结衣| 久久久久久久久无码精品亚洲日韩 | 久久亚洲中文字幕精品有坂深雪| 久久精品中文字幕无码绿巨人| 国产精品九九久久免费视频 | 久久综合精品国产一区二区三区| 中文成人久久久久影院免费观看| 久久久久亚洲AV成人片| 国产免费久久精品丫丫| 亚洲精品乱码久久久久久蜜桃图片| 国产成人久久777777| 无码人妻久久一区二区三区免费| 久久青青国产| 色天使久久综合网天天| 欧美亚洲国产精品久久蜜芽| 久久久久亚洲AV成人网| 日韩精品久久久久久久电影蜜臀| 国产高潮国产高潮久久久91| 少妇久久久久久久久久| 伊人久久精品影院| 久久精品无码一区二区三区免费| 久久99精品国产99久久| 人妻丰满AV无码久久不卡| 国产精品久久久久免费a∨| 久久成人精品| 日韩va亚洲va欧美va久久| 久久综合久久自在自线精品自| 嫩草影院久久国产精品| 嫩草伊人久久精品少妇AV| 色诱久久av| 一本大道久久香蕉成人网| 久久久免费观成人影院| 久久精品成人免费观看97| 欧美色综合久久久久久| 欧美午夜A∨大片久久| 久久久久久无码国产精品中文字幕| 亚洲精品高清国产一线久久|