• <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項目,在創(chuàng)建單文檔時把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框架的因公程序中打開文檔后,還會調(diào)用CHexViewerDoc::OnOpenDocument虛函數(shù)。
            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;
            }
                  最后添加一個輔助成員函數(shù),視圖調(diào)用該函數(shù)從打開的文件中讀取一行數(shù)據(jù)。
            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);
                    
            //這個宏是用來調(diào)試用的,在VC的窗口的輸入框中會看到輸出的內(nèi)容!只有在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、視圖的編碼
                  首先,定義一些變量,并對數(shù)據(jù)的字體進行改變
            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"));
                
            //函數(shù)原型:char *strcpy( char *strDestination, const char *strSource );


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

            CHexViewerView::
            ~CHexViewerView()
            {
                
            if (m_pFont!=NULL)
                {
                    delete m_pFont;
                }
            }
                  設(shè)置滾動的大小,可以向其傳遞映射模式(MM_TEXT顯示文本)和文檔大?。╩_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);
            }
                  給視圖添加為當(dāng)前設(shè)備上下文(DC)計算字體高度(以像素計)的輔助函數(shù)。這里的技巧是傳遞-1作為字符串長度。這意味著不進行任何實際繪制操作,但是DrawText函數(shù)仍返回指定文本的高度。
            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來返回工作區(qū)的大小,用MeasureFontHeight返回每行的高度,然后可以用一個for循環(huán)通過ReadLine函數(shù)來從文檔中檢索每行數(shù)據(jù)。
            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 閱讀(1076) 評論(1)  編輯 收藏 引用 所屬分類: VC實踐

            FeedBack:
            # re: HexViewer[未登錄]
            2011-09-19 13:10 | a
            eeeeeeee  回復(fù)  更多評論
              
            久久99久久99精品免视看动漫| 久久久久久A亚洲欧洲AV冫| 久久精品国产亚洲AV影院| 亚洲欧美日韩中文久久| 久久国产精品-久久精品| 国产99久久九九精品无码| 久久受www免费人成_看片中文| 精品人妻伦九区久久AAA片69| www性久久久com| 欧美国产成人久久精品| 久久精品国内一区二区三区| 色偷偷91久久综合噜噜噜噜| avtt天堂网久久精品| 免费无码国产欧美久久18| 国产精品VIDEOSSEX久久发布| 亚洲AV无码久久精品狠狠爱浪潮| 91久久香蕉国产熟女线看| 无码人妻久久久一区二区三区| 国内精品欧美久久精品| 国产精品福利一区二区久久| 久久夜色精品国产亚洲| 思思久久99热免费精品6| 伊人久久综在合线亚洲2019| 色综合久久无码中文字幕| 午夜精品久久久久久| 久久精品三级视频| 国产AV影片久久久久久| 97久久精品午夜一区二区| 久久久无码精品亚洲日韩蜜臀浪潮| 久久综合色之久久综合| 久久久久国产视频电影| 久久精品国产99久久丝袜| 色综合久久88色综合天天| 久久久久免费精品国产| 99久久国语露脸精品国产| 色婷婷久久综合中文久久蜜桃av| 久久久久国产精品麻豆AR影院 | 伊人久久成人成综合网222| 久久www免费人成看国产片| 99久久亚洲综合精品成人| 国产精品热久久毛片|