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

            幽魂國度

             

            簡述如何實現(xiàn)簡單預覽

            步驟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語句當文件第一次打開時lpszFileName會出錯,在catch中捕獲將其設置為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ū)域,進行比例縮放
               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); //復選框
             CRect rc;
             m_DIBStaticCtrl.GetClientRect(&rc);
             int height = rc.Height();
             rc.top = rcPrev.bottom - 10;//圖像框設置
             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()); //加載
             }

            }//當點擊文件時調(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)  編輯 收藏 引用

            導航

            統(tǒng)計

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久av免费天堂小草播放| 久久综合给合久久狠狠狠97色| 国内精品久久久久久麻豆| 久久综合伊人77777麻豆| 男女久久久国产一区二区三区| 久久久久久久99精品免费观看| 精品久久久久久无码人妻热| 亚洲精品无码久久千人斩| 伊人久久综在合线亚洲2019 | 欧美久久久久久| 热re99久久6国产精品免费| 日本精品久久久中文字幕| 久久久亚洲裙底偷窥综合| 亚洲狠狠久久综合一区77777| 亚洲v国产v天堂a无码久久| 亚洲国产精品久久久久婷婷软件 | 人人狠狠综合久久亚洲| 久久精品国产影库免费看 | 青青久久精品国产免费看| 国产69精品久久久久777| 国内精品久久久久影院亚洲| 99热精品久久只有精品| 精品久久久久久国产| 亚洲狠狠婷婷综合久久久久| 性做久久久久久免费观看| 久久狠狠色狠狠色综合| 久久99精品久久久久婷婷| 久久综合亚洲欧美成人| 亚洲中文字幕久久精品无码APP| 日韩欧美亚洲国产精品字幕久久久| 国产精品久久国产精麻豆99网站 | 久久久久久av无码免费看大片| 99久久精品费精品国产一区二区| 人妻无码中文久久久久专区| 久久综合狠狠综合久久综合88| 无遮挡粉嫩小泬久久久久久久| 久久综合香蕉国产蜜臀AV| 2020久久精品国产免费| AA级片免费看视频久久| 亚洲欧洲久久久精品| 午夜精品久久久久久毛片|