www.pudn.com > ImageVidwer.rar > ImageViewerDlg.cpp
// ImageViewerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ImageViewer.h"
#include "ImageViewerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CImageViewerDlg dialog
BYTE* bmpBuffer;
BOOL bmpLoaded;
JSAMPARRAY pBuffer;
CImageViewerDlg::CImageViewerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CImageViewerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CImageViewerDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
WCHAR strRes[MAX_PATH];
CString(LoadString(GetResourceLib(),IDS_PICTURE_TITLE ,strRes,MAX_PATH));
m_TitleText = strRes;
m_hBitmap=NULL;
m_InitDC = FALSE;
m_bAutoPlay = false;
m_bFullScreen = false;
m_bReDraw = TRUE;
m_DefaultRect = CRect(59,37,262,209); //203*172
m_DelayTime = 1000 * 3;
}
CImageViewerDlg::~CImageViewerDlg()
{
if (!m_SourceFile.IsEmpty()) //·ÀÖ¹ÎÞЧ·¾¶¸²¸ÇÒÔǰ
{
m_InitInf.FileListSize=g_Playlist.m_FileList.GetSize();
m_InitInf.CurrentFileNO=g_Playlist.m_CurrentFile;
CFile myFile(L"\\residentflash\\ImageViewer.ini", CFile::modeCreate | CFile::modeReadWrite);
CArchive arStore(&myFile, CArchive::store);
arStore.Write(&m_InitInf,sizeof(int)*3);
arStore.WriteString( m_SourceFile );
arStore.WriteString( L"\n" );
for (int i=0;i 1)
{
m_butNext.EnableWindow(TRUE);
m_butBack.EnableWindow(TRUE);
m_butAutoPlay.EnableWindow(TRUE);
m_FileNumber = g_Playlist.m_TotalFiles;
}
else
{
m_butNext.EnableWindow(FALSE);
m_butBack.EnableWindow(FALSE);
m_butAutoPlay.EnableWindow(FALSE);
m_FileNumber = 1;
}
//
if (!m_SourceFile.IsEmpty())
{
m_butFullScreen.EnableWindow(TRUE);
if (m_FileNumber > 1)
{
m_butBack.EnableWindow(TRUE);
m_butNext.EnableWindow(TRUE);
}
else
{
m_butBack.EnableWindow(FALSE);
m_butNext.EnableWindow(FALSE);
}
}
else
{
m_butAutoPlay.EnableWindow(FALSE);
m_butFullScreen.EnableWindow(FALSE);
m_butBack.EnableWindow(FALSE);
m_butNext.EnableWindow(FALSE);
}
//LoadImage(m_SourceFile);
return TRUE;
}
m_hBitmap = 0;
m_FileNumber = 0;
m_Zoom = 0;
m_SourceFile = L"";//L"\\SDMEM\\Picturs\\jpg2.jpg";
return TRUE; // return TRUE unless you set the focus to a control
}
HBITMAP CImageViewerDlg::LoadImageJPG(const CString &strFileName)
{
FILE *pFile;
JSAMPARRAY pBuffer;
int nRowSize;
ByteArray imageData(320 * 240 * 3);
struct jpeg_error_mgr jerr;
struct jpeg_decompress_struct cinfo;
if((pFile = _tfopen(strFileName,_T("rb"))) == NULL)
{
CString strError;
strError.Format(_T("Can't Open File '%s'"),strFileName);
MessageBox(strError);
return 0;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,pFile);
jpeg_read_header(&cinfo,TRUE);
jpeg_start_decompress(&cinfo);
nRowSize = cinfo.output_width * cinfo.output_components;
pBuffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, nRowSize, 1);
while(cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo,pBuffer,TRUE);
imageData.Append(pBuffer[0],nRowSize);
}
m_ImageSize = CSize(cinfo.output_width,cinfo.output_height);
if (m_hBitmap)
{
DeleteObject(m_hBitmap);
}
m_hBitmap = CSTScreenBuffer::CreateBitmapByRGBArray
(imageData.GetData(),cinfo.output_width,cinfo.output_height);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(pFile);
return m_hBitmap;
}
void CImageViewerDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
//return;
// TODO: Add your message handler code here
if (!m_SourceFile.IsEmpty())
{
CDC buffDC;
CBitmap bgBmp;
bgBmp.CreateCompatibleBitmap(&dc,320,240);
buffDC.CreateCompatibleDC(&dc);
buffDC.SelectObject(&bgBmp);
if (m_bFullScreen) //È«ÆÁ ÏÔʾȫͼ,ûÓзŴóËõС¹¦ÄÜ
{
Zoom2FullScreen();
}
else //´°¿Ú
{
ZoomImage(m_Zoom);
buffDC.BitBlt(0,0,320,240,&m_BgDC,0,0,SRCCOPY);
}
if (m_hBitmap)
{
CDC m_MemDC;
m_MemDC.CreateCompatibleDC(&dc);
m_MemDC.SelectObject(m_hBitmap);
if (!m_bFullScreen)
{
buffDC.FillSolidRect(&m_DefaultRect,RGB(0,0,0));
}
else
{
buffDC.FillSolidRect(0,0,320,240,RGB(0,0,0));
}
buffDC.StretchBlt(m_DstStartX,m_DstStartY,m_DstWidth,m_DstHeight,&m_MemDC,m_SrcStartX,m_SrcStartY,m_SrcWidth,m_SrcHeight,SRCCOPY);
buffDC.SetBkMode(TRANSPARENT);
buffDC.SetTextColor(RGB(255,0,0));
buffDC.DrawText(GetFileName(m_SourceFile),CRect(86,213,237,231),DT_CENTER|DT_VCENTER);
m_MemDC.DeleteDC();
}
dc.BitBlt(0,0,320,240,&buffDC,0,0,SRCCOPY);
buffDC.DeleteDC(); //ɾ³ý˳ÐòÇÐÎðµßµ¹!!!
bgBmp.DeleteObject();
}
else
{
dc.BitBlt(0,0,320,240,&m_BgDC,0,0,SRCCOPY);
}
// Do not call CDialog::OnPaint() for painting messages
}
BOOL CImageViewerDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}
void CImageViewerDlg::OnButtonAutoplay()
{
// TODO: Add your control notification handler code here
m_bAutoPlay = !m_bAutoPlay;
if (m_bAutoPlay)
{
m_butAutoPlay.LoadBitmaps(IDB_IMAGE_PLAYD,IDB_IMAGE_PLAYD,IDB_IMAGE_PLAYDIS);
}
else
{
m_butAutoPlay.LoadBitmaps(IDB_IMAGE_PLAY,IDB_IMAGE_PLAY,IDB_IMAGE_PLAYDIS);
}
m_butAutoPlay.Invalidate();
//
if (m_bAutoPlay)
{
SetTimer(1,m_DelayTime,NULL);
}
else
{
KillTimer(1);
}
}
void CImageViewerDlg::OnButtonClose()
{
// TODO: Add your control notification handler code here
EndDialog(IDOK);
}
void CImageViewerDlg::OnButtonFullscreen()
{
// TODO: Add your control notification handler code here
if (!m_bFullScreen)
{
m_bFullScreen = TRUE;
ShowControl(FALSE);
m_Zoom = 0;
m_butZoomIn.EnableWindow(FALSE);
m_butZoomOut.EnableWindow(TRUE);
Invalidate();
}
}
void CImageViewerDlg::OnButtonBack()
{
// TODO: Add your control notification handler code here
if (g_Playlist.m_CurrentFile >0)
{
m_butNext.EnableWindow(TRUE);
if (g_Playlist.m_CurrentFile == 1)
{ m_butBack.EnableWindow(FALSE);
}
m_SourceFile = g_Playlist.GetNextWorkFileName(TRUE,FALSE);
LoadImage(m_SourceFile);
InvalidateRect(CRect(86,213,237,231));
}
}
void CImageViewerDlg::OnButtonNext()
{
// TODO: Add your control notification handler code here
if (g_Playlist.m_CurrentFile < g_Playlist.m_TotalFiles-1)
{
m_butBack.EnableWindow(TRUE);
if (g_Playlist.m_CurrentFile == g_Playlist.m_TotalFiles-2)
{ m_butNext.EnableWindow(FALSE);
}
m_SourceFile = g_Playlist.GetNextWorkFileName(FALSE,FALSE);
LoadImage(m_SourceFile);
InvalidateRect(CRect(86,213,237,231));
}
}
void CImageViewerDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
COpenFileDlg fileDlg;
fileDlg.LoadBitmap(IDB_FILEVIEW_WND,IDB_ICON_IMAGEDIR,IDB_ICON_IMAGEDIRD,
IDB_ICON_IMAGE,IDB_ICON_IMAGED);
fileDlg.SetFileType( L".bmp.jpg.");
fileDlg.SetPosition(m_InitInf.FileviewPosition);
fileDlg.SetSourceFile(m_InitInf.SourceFile);
if (fileDlg.DoModal() == IDOK)
{
m_InitInf.FileviewPosition=fileDlg.GetPosition();
m_InitInf.SourceFile=fileDlg.GetSourceFile();
m_SourceFile = fileDlg.GetSourceFile();
g_Playlist.m_FileList.RemoveAll();
g_Playlist.m_TotalFiles = fileDlg.m_FileList.GetSize();
g_Playlist.m_CurrentFile = fileDlg.m_FileNO;
g_Playlist.m_FileList.Append(fileDlg.m_FileList);
g_Playlist.m_FilePath = m_SourceFile.Left(m_SourceFile.ReverseFind(L'\\')+1);
g_Playlist.InitSeed(g_Playlist.m_TotalFiles);
if (g_Playlist.m_TotalFiles > 1)
{
m_butNext.EnableWindow(TRUE);
m_butBack.EnableWindow(TRUE);
m_butAutoPlay.EnableWindow(TRUE);
m_FileNumber = g_Playlist.m_TotalFiles;
}
else
{
m_butNext.EnableWindow(FALSE);
m_butBack.EnableWindow(FALSE);
m_butAutoPlay.EnableWindow(FALSE);
m_FileNumber = 1;
}
//
if (!m_SourceFile.IsEmpty())
{
m_butFullScreen.EnableWindow(TRUE);
if (m_FileNumber > 1)
{
m_butBack.EnableWindow(TRUE);
m_butNext.EnableWindow(TRUE);
}
else
{
m_butBack.EnableWindow(FALSE);
m_butNext.EnableWindow(FALSE);
}
}
else
{
m_butAutoPlay.EnableWindow(FALSE);
m_butFullScreen.EnableWindow(FALSE);
m_butBack.EnableWindow(FALSE);
m_butNext.EnableWindow(FALSE);
}
LoadImage(m_SourceFile);
}
}
void CImageViewerDlg::OnButtonZoomin()
{
// TODO: Add your control notification handler code here
if (m_hBitmap)
{
m_butZoomOut.EnableWindow(TRUE);
if (m_Zoom > 0)
{
if (--m_Zoom==0)
{ m_butZoomIn.EnableWindow(FALSE);
}
InvalidateRect(m_DefaultRect);
}
}
}
void CImageViewerDlg::OnButtonZoomout()
{
// TODO: Add your control notification handler code here
if (m_hBitmap)
{
m_butZoomIn.EnableWindow(TRUE);
if (m_Zoom < MAX_ZOOM)
{
if (++m_Zoom==MAX_ZOOM)
{ m_butZoomOut.EnableWindow(FALSE);
}
InvalidateRect(m_DefaultRect);
}
}
}
bool CImageViewerDlg::LoadImage(CString strFileName)
{
m_Zoom=0;
m_butZoomIn.EnableWindow(FALSE);
m_butZoomOut.EnableWindow(FALSE);
CClientDC dc(this);
switch(GetImageType(strFileName))
{
case IMG_BMP:
// ×°ÔØBitmap¸ñʽµÄͼƬ
//m_hBitmap = SHLoadDIBitmap(strFileName);
if (m_hBitmap)
{
DeleteObject(m_hBitmap);
m_hBitmap = NULL;
m_DIBSection.DeleteObject();
}
m_DIBSection.Load(strFileName);
m_hBitmap = m_DIBSection.GetSafeHandle();
if (!m_hBitmap)
{
return FALSE;
}else
{
// »ñȡͼƬ¿íºÍ¸ß
m_ImageSize = CSize(m_DIBSection.GetWidth(),m_DIBSection.GetHeight());
}
break;
case IMG_JPG:
if (m_hBitmap)
{
DeleteObject(m_hBitmap);
m_hBitmap = NULL;
}
LoadImageJPG(strFileName);
if(!m_hBitmap)
{
return false;
}
break;
case IMG_UNKNOWN:
m_hBitmap = NULL;
//MessageBox(L"¸ÃͼƬ¸ñʽÔݲ»Ö§³Ö....");
return FALSE;
break;
}
if (m_bFullScreen)
{
Invalidate();
}
else
{
InvalidateRect(m_DefaultRect);
}
m_butZoomOut.EnableWindow(TRUE);
return true;
}
int CImageViewerDlg::GetImageType(CString strFileName)
{
// ÅжÏÎļþÊÇ·ñÊÇBitmapλͼ
CFile file;
CFileException e;
// ´ò¿ªÎļþ
if (! file.Open(strFileName,CFile::modeRead,&e) )
{ return IMG_UNKNOWN;
}
// ¶ÁÈ¡ÎļþǰÁ½¸ö×Ö½Ú
BYTE bBuffer[2];
file.Read(bBuffer,2);
file.Close();
// ÅжÏǰÁ½¸ö×Ö½ÚÊDz»ÊÇ'BM'
if (bBuffer[0] == 0x42 && bBuffer[1] == 0x4D)
{
return IMG_BMP;
}
else
{
if (bBuffer[0] == 0xFF && bBuffer[1] == 0xD8)
{
return IMG_JPG;
}
}
return IMG_UNKNOWN;
}
//***********************************************
void CImageViewerDlg::ZoomImage(int iZoom)
{
CSize m_InitViewSize; //³õʼ̬´°¿Ú¶ÔÓ¦µÄԴͼÏñ´óС
CSize m_CurrentViewSize; //Öмä±äÁ¿,ÓÃÓÚ¼ÆËãSrc & Dst
int m_ZoomQuotiety; // =2^m_zoom
float k; // Ä¿±ê/Ô´ ±ÈÀýϵÊ
float n; //
int m;
//¼ÆËã³õʼÏÔʾλÖÃ
if ( (m_ImageSize.cx*m_DefaultRect.Height()) > (m_ImageSize.cy*m_DefaultRect.Width()) )
{n = ((float)m_ImageSize.cx)/m_DefaultRect.Width();
}
else
{n = ((float)m_ImageSize.cy)/m_DefaultRect.Height();
}
if (n < 1) //Сͼ 1:1 ÏÔʾ
{n=1;
}
float cx = n * m_DefaultRect.Width()+0.5;
float cy = n * m_DefaultRect.Height()+0.5;
m_InitViewSize = CSize(cx, cy);
m_ZoomQuotiety=1< m_CurrentViewSize.cx) //´óͼ
{
//½«ÏÔÊ¾ÇøÓòµ÷Õûµ½Í¼Æ¬·¶Î§ÄÚ
m=m_CurrentViewSize.cx/2;
if (m_CenterPt.x (m_ImageSize.cx-m) )
{m_CenterPt.x = m_ImageSize.cx-m;
}
m_SrcWidth=m_CurrentViewSize.cx;
m_SrcStartX=m_CenterPt.x - m_CurrentViewSize.cx/2;
m_DstWidth=m_DefaultRect.Width();
m_DstStartX=m_DefaultRect.left;
}
else // (m_ImageSize.cx < =m_CurrentViewSize.cx) Сͼ
{
m_CenterPt.x = m_ImageSize.cx/2;
m_SrcWidth = m_ImageSize.cx;
m_SrcStartX=0;
m_DstWidth = k*m_SrcWidth+0.5;
m_DstStartX=m_DefaultRect.CenterPoint().x - m_DstWidth/2;
}
//Y ×ø±êµ÷Õû
if (m_ImageSize.cy > m_CurrentViewSize.cy) //´óͼ
{
//½«ÏÔÊ¾ÇøÓòµ÷Õûµ½Í¼Æ¬·¶Î§ÄÚ
m=m_CurrentViewSize.cy/2;
if (m_CenterPt.y (m_ImageSize.cy-m) )
{m_CenterPt.y = m_ImageSize.cy-m;
}
m_SrcHeight=m_CurrentViewSize.cy;
m_SrcStartY=m_CenterPt.y-(m_CurrentViewSize.cy/2);
m_DstHeight=m_DefaultRect.Height();
m_DstStartY=m_DefaultRect.top;
}
else // (m_ImageSize.cy <= m_CurrentViewSize.cy) //Сͼ
{
m_CenterPt.y = m_ImageSize.cy/2;
m_SrcHeight = m_ImageSize.cy;
m_SrcStartY=0;
m_DstHeight = k*m_SrcHeight+0.5;
m_DstStartY=m_DefaultRect.CenterPoint().y - m_DstHeight/2;
}
}
//***********************************************
void CImageViewerDlg::Zoom2FullScreen(void)
{
float k; //ÏÔʾ±ÈÀý
m_CenterPt.x = m_ImageSize.cx/2;
m_CenterPt.y = m_ImageSize.cy/2;
m_SrcWidth = m_ImageSize.cx;
m_SrcHeight = m_ImageSize.cy;
m_SrcStartX=0;
m_SrcStartY=0;
if (m_ImageSize.cx*240 > m_ImageSize.cy*320)
{ k = ((float)320)/m_ImageSize.cx; //ÒÔ¸¡µãÊýÔËËã
}
else
{k=((float)240)/m_ImageSize.cy;
}
if (k > 1) //Сͼ 1:1 ÏÔʾ
{k=1;
}
m_DstWidth =k*m_SrcWidth;
m_DstHeight =k*m_SrcHeight;
m_DstStartX=(320 - m_DstWidth)/2;
m_DstStartY=(240 - m_DstHeight)/2;
}
//***********************************************
void CImageViewerDlg::ShowControl(BOOL bShow)
{
int sw;
if (bShow)
{sw=SW_SHOW;
}
else
{sw=SW_HIDE;
}
m_butZoomIn.ShowWindow(sw);
m_butZoomOut.ShowWindow(sw);
m_butAutoPlay.ShowWindow(sw);
m_butFullScreen.ShowWindow(sw);
m_butBack.ShowWindow(sw);
m_butNext.ShowWindow(sw);
m_butClose.ShowWindow(sw);
m_butOpen.ShowWindow(sw);
}
CString CImageViewerDlg::GetFileName(CString strFileName)
{
return strFileName.Right(strFileName.GetLength() -strFileName.ReverseFind(L'\\')-1);
}
void CImageViewerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_SourceFile = g_Playlist.GetNextWorkFileName(FALSE,FALSE);
LoadImage(m_SourceFile);
InvalidateRect(CRect(86,213,237,231));
CDialog::OnTimer(nIDEvent);
}
CRect CImageViewerDlg::GetZoomRect(CRect *rcScreen, CSize sizePicture)
{
CRect rect(rcScreen);
double dWidth = rcScreen->Width();
double dHeight = rcScreen->Height();
double dAspectRatio = dWidth/dHeight;
double dPictureWidth = sizePicture.cx;
double dPictureHeight = sizePicture.cy;
double dPictureAspectRatio = dPictureWidth/dPictureHeight;
//If the aspect ratios are the same then the screen rectangle
// will do, otherwise we need to calculate the new rectangle
if (dPictureAspectRatio > dAspectRatio)
{
int nNewHeight = (int)(dWidth/dPictureWidth*dPictureHeight);
int nCenteringFactor = (rcScreen->Height() - nNewHeight) / 2;
rect.SetRect( 0,
nCenteringFactor,
(int)dWidth,
nNewHeight + nCenteringFactor);
}
else if (dPictureAspectRatio < dAspectRatio)
{
int nNewWidth = (int)(dHeight/dPictureHeight*dPictureWidth);
int nCenteringFactor = (rcScreen->Width() - nNewWidth) / 2;
rect.SetRect( nCenteringFactor,
0,
nNewWidth + nCenteringFactor,
(int)(dHeight));
}
return rect;
}
void CImageViewerDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (!m_bFullScreen) //´°¿Úģʽ
{
m_DownPt=point;
}
else
{
m_bFullScreen = FALSE;
ShowControl(TRUE);
Invalidate();
}
//CDialog::OnLButtonDown(nFlags, point);
}
//´°¿ÚģʽͼƬÍ϶¯
void CImageViewerDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (!m_bFullScreen)
{
if ( m_DefaultRect.PtInRect(point) || m_DefaultRect.PtInRect(m_DownPt) )
{
m_CenterPt.x -=(point.x-m_DownPt.x)/m_Scale;
m_CenterPt.y -=(point.y-m_DownPt.y)/m_Scale;
InvalidateRect(m_DefaultRect);
}
}
else //È«ÆÁģʽ,Çл»Í¼Æ¬,
{
if ( (point.x-m_DownPt.x) >60)
{
OnButtonBack();
}
else if ( (point.x-m_DownPt.x) < -60)
{
OnButtonNext();
}
}
//CDialog::OnLButtonUp(nFlags, point);
}
//È«ÆÁģʽÇл»
void CImageViewerDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MessageBox(L"killjapan");
if (m_bFullScreen)
{
m_bFullScreen = FALSE;
ShowControl(TRUE);
Invalidate();
}
else if (m_DefaultRect.PtInRect(point) )//´°¿Úģʽ Ë«»÷ͼƬÇл»µ½È«ÆÁ
{
OnButtonFullscreen() ;
}
CDialog::OnLButtonDblClk(nFlags, point);
}