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

            幽魂國度

             

            簡述如何實現簡單預覽

            步驟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();

            // 創建圖像顯示的對象并讀入圖像文件
              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;
             // 獲得顯示區域
             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())
              { // 圖像尺寸小于顯示區域將圖像顯示在中間
               nDestX = PaintRect.left + (PaintRect.Width() - size.cx)/2;
               nDestY = PaintRect.top + (PaintRect.Height() - size.cy)/2;
               nDestWidth = size.cx;
               nDestHeight = size.cy;
              }
              else
              { // 圖像尺寸大于顯示區域,進行比例縮放
               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()); //加載
             }

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

            導航

            統計

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久亚洲国产精品五月天婷| 国产精品对白刺激久久久| 久久国产精品久久久| 国内精品久久国产大陆| 欧美亚洲另类久久综合| 亚洲а∨天堂久久精品9966| 国产美女亚洲精品久久久综合| 性做久久久久久久| 国产精品久久一区二区三区| 国产精品久久久久久久午夜片 | 亚洲午夜无码久久久久小说| 久久久久久久97| 一本久久久久久久| 亚洲AV无码久久精品成人 | 久久国产福利免费| 亚洲精品美女久久777777| 94久久国产乱子伦精品免费| 久久婷婷五月综合色奶水99啪| 国产亚洲欧美成人久久片| 超级97碰碰碰碰久久久久最新| 51久久夜色精品国产| 久久九九久精品国产免费直播| 亚洲午夜精品久久久久久人妖| 久久久久久久97| 色妞色综合久久夜夜| 久久人人爽人人爽人人爽| 中文字幕亚洲综合久久2| 日韩精品久久无码人妻中文字幕 | 国产精品9999久久久久| 欧美一区二区久久精品| 亚洲国产精品久久久久婷婷软件| 久久国产热精品波多野结衣AV| 99久久香蕉国产线看观香| 久久综合九色综合久99| 久久久人妻精品无码一区 | 久久国产一区二区| 国产精品一区二区久久不卡| 一本色综合网久久| 一本色道久久HEZYO无码| 中文精品久久久久人妻不卡| 亚洲欧美成人综合久久久|