• <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>

            幽魂國度

             

            簡述如何實(shí)現(xiàn)簡單預(yù)覽

            步驟1:添加新類CDIBStatic,類型為MFC Class,基類為CStatic。
            步驟2:在工程中加入CPictureObj.h和CPictureObj.cpp,及CIstream.h和CIstream.cpp。
            步驟3:在類CDIBStatic加入頭文件#include "PictureObj.h",添加變量CPictureObj* m_pPicObj;,用于讀取和顯示。CPictureObj中封裝了IPicture接口。
            步驟4:新建一Dialog,加入控件picture,類型為MFC Class,基類為CfileDlg。
            部分代碼:
            CDIBStatic源代碼:
            OOL CDIBStatic::LoadDib(LPCTSTR lpszFileName)//讀取
            {
             try//利用try語句當(dāng)文件第一次打開時lpszFileName會出錯,在catch中捕獲將其設(shè)置為NULL
            lpszFileName = lpszFileName;
              // 確保文件存在并能打開
              CFile file(lpszFileName, CFile::modeRead);
              file.Close();

            // 創(chuàng)建圖像顯示的對象并讀入圖像文件
              m_pPicObj = new CPictureObj;
              if(!m_pPicObj->Load(lpszFileName))
              {
               // 讀入文件失敗,清除對象
               m_pPicObj = NULL;
               delete m_pPicObj;
               // 清除顯示的圖像并顯示錯誤提示
               PaintDib(IsValidDib());
               return FALSE;
              }
              PaintDib(IsValidDib());
              return TRUE;
             }
             catch (CFileException* e)
             {
              m_lpszFileName = NULL;
              PaintDib(IsValidDib());
              e->Delete();
              return FALSE;
             }
            }

            void CDIBStatic::PaintDib(BOOL bDibValid)//顯示
            {
             ASSERT_VALID(this);
             ClearDib(); // 清除以前的圖像
              
             CRect PaintRect;
             // 獲得顯示區(qū)域
             GetClientRect(&PaintRect);   
             PaintRect.InflateRect(-1, -1);
             CClientDC dc(this);

             if (bDibValid && m_bPreview)
             {
              CSize size = m_pPicObj->GetSize(&dc);
              int nDestX, nDestY, nDestWidth, nDestHeight;
              if ((DWORD)size.cx < (DWORD)PaintRect.Width() && (DWORD)size.cy < (DWORD)PaintRect.Height())
              { // 圖像尺寸小于顯示區(qū)域?qū)D像顯示在中間
               nDestX = PaintRect.left + (PaintRect.Width() - size.cx)/2;
               nDestY = PaintRect.top + (PaintRect.Height() - size.cy)/2;
               nDestWidth = size.cx;
               nDestHeight = size.cy;
              }
              else
              { // 圖像尺寸大于顯示區(qū)域,進(jìn)行比例縮放
               if ((PaintRect.Width()/(float)size.cx) <= (PaintRect.Height()/(float)size.cy))
               { // 寬度限制
                nDestWidth = PaintRect.Width();
                nDestHeight = (nDestWidth*size.cy) / size.cx;
                nDestX = PaintRect.left;
                nDestY = PaintRect.top + (PaintRect.Height() - nDestHeight) /2;
               }
               else
               { // 高度限制  
                nDestHeight = PaintRect.Height();
                nDestWidth = (nDestHeight*size.cx) / size.cy;
                nDestX = PaintRect.left + (PaintRect.Width() - nDestWidth) /2;
                nDestY = PaintRect.top;
               }
              }

              // 獲得圖像的顯示位置和大小
              CRect RectDest(nDestX, nDestY, nDestX+nDestWidth, nDestY+nDestHeight);
              // 顯示圖像
              m_pPicObj->Draw(&dc,&RectDest,&RectDest);
              // 給圖像加一外框
              CBrush*  pOldBrush  = (CBrush*)dc.SelectStockObject(NULL_BRUSH);  
              dc.Rectangle(RectDest);
              if(NULL != pOldBrush)  { dc.SelectObject(pOldBrush);  }
             }
             else
             {
              // 顯示錯誤提示信息
              CString strText = "不能識別的文件格式!";
              if( m_lpszFileName == NULL || strlen(m_lpszFileName) <= 0 )
              {
               strText = "沒有選擇文件!";
              }
              if( !m_bPreview )
              {
               strText = "";
              }
              dc.DrawText(strText, strText.GetLength(), &PaintRect, DT_SINGLELINE|DT_CENTER|DT_VCENTER|DT_END_ELLIPSIS);
             }
              
             return;
            }

            HBRUSH CDIBSatic::CtlColor(CDC* pDC, UINT nCtlColor)//用于重繪
            {
             // TODO: Change any attributes of the DC here
             PaintDib(IsValidDib());
             return (HBRUSH)GetStockObject(NULL_BRUSH);
            }
            BOOL IsValidDib() const { return (m_pPicObj && m_pPicObj->m_pPict); }
            void CDIBSatic::ClearDib()//清除圖片
            {
             ASSERT_VALID(this);
             
             CClientDC dc(this);
             CRect rectPaint;
               
             GetClientRect(&rectPaint);   
             rectPaint.InflateRect(-1,-1);
                
             CBrush* pBrushWhite; //白畫刷
             pBrushWhite = CBrush::FromHandle((HBRUSH)::GetStockObject(WHITE_BRUSH));
               
             dc.FillRect(&rectPaint, pBrushWhite);
            }
            void RemoveDib() { m_lpszFileName = NULL; delete m_pPicObj; m_pPicObj = NULL; PaintDib(IsValidDib()); }
             void SetPreview(BOOL bPreview) { m_bPreview = bPreview; PaintDib(IsValidDib()); }
            CPreviewFileDlg源代碼:
            BOOL CPreviewFileDlg::OnInitDialog()
            {
             CFileDialog::OnInitDialog();
             m_DIBStaticCtrl.SubclassDlgItem(IDC_IMAGE, this);
             CWnd* pParent = GetParent();
             CRect rcParent;
             pParent->GetClientRect(&rcParent);
             CRect rcPrev;
             GetDlgItem(IDC_PREVIEW)->GetClientRect(&rcPrev); //復(fù)選框
             CRect rc;
             m_DIBStaticCtrl.GetClientRect(&rc);
             int height = rc.Height();
             rc.top = rcPrev.bottom - 10;//圖像框設(shè)置
             rc.bottom = rc.top + height ;
             rc.left = 50;
             rc.right = rcParent.Width() - rc.left;
             m_DIBStaticCtrl.MoveWindow(&rc, true);

             GetDlgItem(IDC_PREVIEW)->SendMessage(BM_SETCHECK, (m_bPreview) ? 1 : 0);
             
             return TRUE;  // return TRUE unless you set the focus to a control
                           // EXCEPTION: OCX Property Pages should return FALSE
            }
            void CPreviewFileDlg::OnFileNameChange()
            {
             CFileDialog::OnFileNameChange();
             if (m_bPreview)
             {
              m_DIBStaticCtrl.SetPreview(m_bPreview);//顯示圖片
              m_DIBStaticCtrl.LoadDib(GetPathName()); //加載
             }

            }//當(dāng)點(diǎn)擊文件時調(diào)用
            void CPreviewFileDlg::OnFolderChange()
            {
             CFileDialog::OnFolderChange();
             m_DIBStaticCtrl.RemoveDib();//清除
            }
            菜單欄源代碼:
            void CPreviewDlg::OnPreview()
            {
             // TODO: Add extra validation here
                static char BASED_CODE szFilter[] = "Bitmap(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg|GIF(*.gif)|*.gif|WMF(*.wmf)|*.wmf|ICON(*.ico)|*.ico||";
             CString strDefName;

             char szPath[MAX_PATH];//最大目錄大小
             
                CPreviewFileDlg FileDlg(TRUE,"*.*",NULL,
                                    OFN_FILEMUSTEXIST|OFN_NONETWORKBUTTON|
                                    OFN_PATHMUSTEXIST,szFilter);
             FileDlg.m_ofn.lpstrInitialDir = szPath;
                if( FileDlg.DoModal() != IDOK )
              return;

                // To get the selected file's path and name
                CString strFileName;
                strFileName = FileDlg.GetPathName();

                if(strFileName.IsEmpty())
                {
              return;
             }
            }

            posted on 2009-12-30 21:03 閱讀(615) 評論(0)  編輯 收藏 引用

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久综合久久综合久久| 久久伊人五月天论坛| 亚洲欧洲日产国码无码久久99| 亚洲伊人久久精品影院| 国产三级久久久精品麻豆三级| 婷婷久久综合九色综合98| 午夜精品久久久久成人| 亚洲AV日韩精品久久久久久久| 日本精品久久久中文字幕| 亚洲人AV永久一区二区三区久久| 久久亚洲精品成人av无码网站| 成人午夜精品久久久久久久小说| 久久久久久久久66精品片| 91精品国产乱码久久久久久| 一本久久a久久精品综合香蕉| .精品久久久麻豆国产精品| 少妇久久久久久被弄到高潮| 久久免费小视频| 99久久久国产精品免费无卡顿| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 香蕉aa三级久久毛片 | 9999国产精品欧美久久久久久| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 品成人欧美大片久久国产欧美| 亚洲中文字幕久久精品无码喷水| 久久精品国产亚洲精品| 91精品日韩人妻无码久久不卡| 精品久久久久中文字幕日本 | 久久精品国产亚洲网站| 波多野结衣久久| 久久精品中文字幕大胸| 亚洲精品综合久久| 亚洲国产婷婷香蕉久久久久久| 久久久久亚洲精品中文字幕| 久久亚洲AV无码西西人体| 狠狠色伊人久久精品综合网| 久久国产精品偷99| 亚洲伊人久久综合影院| 久久精品人人做人人爽电影| 伊人久久大香线蕉综合Av| 色欲av伊人久久大香线蕉影院|