📄 photoviewerdlg.cpp
字号:
// PhotoViewerDlg.cpp: implementation of the CPhotoViewerDlg class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PhotoViewerDlg.h"
#include "NListBox.h"
#include "ximage.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CPhotoViewerDlg, CNDialog)
ON_BN_CLICKED(IDC_PREVIOUS,OnPrevious)
ON_BN_CLICKED(IDC_NEXT,OnNext)
ON_BN_CLICKED(IDC_ZOOMIN,OnZoomIn)
ON_BN_CLICKED(IDC_ZOOMOUT,OnZoomOut)
ON_BN_CLICKED(IDC_BESTFIT,OnBestfit)
ON_BN_CLICKED(IDC_ACTUAL_SIZE,OnActualSize)
END_MESSAGE_MAP()
CPhotoViewerDlg::CPhotoViewerDlg(CNWnd* pParent, UINT uID)
:CNDialog(pParent, uID)
{
m_bBestFit = TRUE;
m_bActualSize = FALSE;
m_pImage = NULL;
m_dZoomRatio = 1.0;
m_nCurOffsetX = m_nOffsetX = 0;
m_nCurOffsetY = m_nOffsetY = 0;
m_pFileList = NULL;
memset(m_szFileName, 0, sizeof(m_szFileName));
}
CPhotoViewerDlg::~CPhotoViewerDlg()
{
}
BOOL CPhotoViewerDlg::OnInitDialog()
{
CNDialog::OnInitDialog();
m_btnBestFit.SubclassDlgItem(IDC_BESTFIT,this);
m_btnBestFit.SetIcon(IDI_PHOTO_BESTFIT);
m_btnActualSize.SubclassDlgItem(IDC_ACTUAL_SIZE,this);
m_btnActualSize.SetIcon(IDI_PHOTO_ACTUAL_SIZE);
m_btnPrevious.SubclassDlgItem(IDC_PREVIOUS,this);
m_btnPrevious.SetIcon(IDI_MP_PREVIOUS);
m_btnNext.SubclassDlgItem(IDC_NEXT,this);
m_btnNext.SetIcon(IDI_MP_NEXT);
m_btnZoomIn.SubclassDlgItem(IDC_ZOOMIN,this);
m_btnZoomIn.SetIcon(IDI_PHOTO_ZOOMIN);
m_btnZoomOut.SubclassDlgItem(IDC_ZOOMOUT, this);
m_btnZoomOut.SetIcon(IDI_PHOTO_ZOOMOUT);
if ( m_pFileList )
{
int nItem = m_pFileList->GetCurSel();
m_pFileList->GetItemText(nItem, m_szFileName, MAX_PATH);
CreateCxImage();
}
return TRUE;
}
BOOL CPhotoViewerDlg::OnEraseBkgnd(HDC hdc)
{
RECT rc;
SetRect(&rc, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
FillSolidRect(hdc, &rc, RGB(0,0,0));
DrawImage(hdc);
SetRect(&rc, 0,SCREEN_HEIGHT - PV_BAR_HEIGHT,
SCREEN_WIDTH, SCREEN_HEIGHT);
FillSolidRect(hdc, &rc, MP_BAR_COLOR);
return TRUE;
}
HBRUSH CPhotoViewerDlg::OnCtlColor(HDC hdc,HWND hwnd, UINT nCtlColor)
{
// if neccessary, call base class to set DC attributes
//CNDialog::OnCtlColor(hdc,hwnd, nCtlColor);
static BOOL s_bInit = FALSE;
static HBRUSH s_hbrMenuBar;
if( !s_bInit )
{
s_hbrMenuBar = CreateSolidBrush(MP_BAR_COLOR);
s_bInit = TRUE;
}
// all buttons and route info static control.
if(nCtlColor == WM_CTLCOLORBTN )
{
return s_hbrMenuBar;
}
return CNDialog::OnCtlColor(hdc,hwnd, nCtlColor);
}
void CPhotoViewerDlg::OnPrevious()
{
if ( m_pFileList )
{
int nItem = m_pFileList->GetCurSel() - 1;
if( nItem >=0 ) // it's still a illegal value.
{
m_pFileList->SetCurSel(nItem);
m_pFileList->GetItemText(nItem, m_szFileName, MAX_PATH);
CreateCxImage();
m_btnBestFit.PostMessage(BM_CLICK, 0, 0);
}
}
}
void CPhotoViewerDlg::OnNext()
{
if ( m_pFileList )
{
int nItem = m_pFileList->GetCurSel() + 1;
if( nItem < m_pFileList->GetCount() ) // it's still a illegal value.
{
m_pFileList->SetCurSel(nItem);
m_pFileList->GetItemText(nItem, m_szFileName, MAX_PATH);
CreateCxImage();
m_btnBestFit.PostMessage(BM_CLICK, 0, 0);
}
}
}
void CPhotoViewerDlg::OnZoomIn()
{
m_bBestFit = FALSE;
m_dZoomRatio *= 1.25;
InvalidateRect(NULL);
}
void CPhotoViewerDlg::OnZoomOut()
{
m_bBestFit = FALSE;
m_dZoomRatio *= 0.75;
InvalidateRect(NULL);
}
void CPhotoViewerDlg::OnBestfit()
{
m_bBestFit = TRUE;
// reset zoom ratio on this case.
m_dZoomRatio = 1.0;
m_nCurOffsetX = m_nOffsetX = 0;
m_nCurOffsetY = m_nOffsetY = 0;
InvalidateRect(NULL);
}
void CPhotoViewerDlg::OnActualSize()
{
m_bBestFit = FALSE;
// reset zoom ratio on this case.
m_dZoomRatio = 1.0;
m_nCurOffsetX = m_nOffsetX = 0;
m_nCurOffsetY = m_nOffsetY = 0;
InvalidateRect(NULL);
}
void CPhotoViewerDlg::OnMouseMove(UINT nFlags, POINT point)
{
// if mouse done, move the picture.
if ( GetCapture() == GetSafeHwnd() )
{
m_nCurOffsetX = m_nOffsetX + point.x - m_ptMouseDown.x;
m_nCurOffsetY = m_nOffsetY + point.y - m_ptMouseDown.y;
InvalidateRect(NULL);
UpdateWindow();
}
}
void CPhotoViewerDlg::OnLButtonDown(UINT nFlags, POINT point)
{
// it take affects only hit the output area.
if(point.y < SCREEN_HEIGHT - PV_BAR_HEIGHT)
{
SetCapture(GetSafeHwnd());
m_ptMouseDown = point;
}
}
void CPhotoViewerDlg::OnLButtonUp(UINT nFlags, POINT point)
{
if ( GetCapture() == GetSafeHwnd() )
{
m_nOffsetX = m_nCurOffsetX;
m_nOffsetY = m_nCurOffsetY;
ReleaseCapture();
}
}
void CPhotoViewerDlg::DrawImage(HDC hdc)
{
if( !m_pImage ) return;
int cx = m_pImage->GetWidth();
int cy = m_pImage->GetHeight();
// rect for draw.
RECT rect;
SetRect(&rect, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - PV_BAR_HEIGHT);
int rect_cx = SCREEN_WIDTH;
int rect_cy = SCREEN_HEIGHT - PV_BAR_HEIGHT;
// adjust rect if trying to display it in Bestfit mode.
if ( m_bBestFit && (cx > rect_cx || cy > rect_cy) ) // picture is larger than output window.
{
double ratioX = (double)cx / (double)rect_cx;
double ratioY = (double)cy / (double)rect_cy;
double ratio = (ratioX > ratioY) ? ratioX : ratioY;
m_dZoomRatio = 1.0/ratio;
}
cx = (int)(cx * m_dZoomRatio);
cy = (int)(cy * m_dZoomRatio);
InflateRect(&rect, (cx-rect_cx)/2, (cy-rect_cy)/2);
OffsetRect(&rect, m_nCurOffsetX, m_nCurOffsetY);
// draw the picture.
m_pImage->Draw(hdc,rect);
}
void CPhotoViewerDlg::CreateCxImage()
{
// delete image if exists one.
delete m_pImage;
m_pImage = NULL;
if( m_szFileName[0] == 0 ) return;
TCHAR ext[MAX_PATH];
FindExtension(m_szFileName, ext);
int type = FindType(ext);
m_pImage = new CxImage(m_szFileName, type);
InvalidateRect(NULL);
}
void CPhotoViewerDlg::FindExtension(LPCTSTR name, LPTSTR ext/*output*/)
{
ext[0] = (TCHAR)0;
int len = _tcslen(name);
int i;
for (i = len-1; i >= 0; i--){
if (name[i] == L'.'){
_tcscpy(ext, (LPTSTR)&name[i+1]);
}
}
}
//////////////////////////////////////////////////////////////////////////////
int CPhotoViewerDlg::FindType(LPCTSTR exts)
{
TCHAR ext[MAX_PATH+1];
_tcscpy(ext, exts);
_tcslwr(ext);
int type = 0;
if ( _tcscmp(ext, L"bmp") == 0 ) type = CXIMAGE_FORMAT_BMP;
#if CXIMAGE_SUPPORT_JPG
else if (_tcscmp(ext, L"jpg") == 0 ||_tcscmp(ext, L"jpeg") == 0 ) type = CXIMAGE_FORMAT_JPG;
#endif
#if CXIMAGE_SUPPORT_GIF
else if (_tcscmp(ext, L"gif") == 0 ) type = CXIMAGE_FORMAT_GIF;
#endif
#if CXIMAGE_SUPPORT_PNG
else if (_tcscmp(ext, L"png") == 0 ) type = CXIMAGE_FORMAT_PNG;
#endif
#if CXIMAGE_SUPPORT_MNG
// else if (ext==L"mng"||ext==L"jng") type = CXIMAGE_FORMAT_MNG;
#endif
#if CXIMAGE_SUPPORT_ICO
else if (_tcscmp(ext, L"ico") == 0 ) type = CXIMAGE_FORMAT_ICO;
#endif
#if CXIMAGE_SUPPORT_TIF
else if (_tcscmp(ext, L"tiff") == 0 ||_tcscmp(ext, L"tif") == 0 ) type = CXIMAGE_FORMAT_TIF;
#endif
#if CXIMAGE_SUPPORT_TGA
// else if (ext==L"tga") type = CXIMAGE_FORMAT_TGA;
#endif
#if CXIMAGE_SUPPORT_PCX
// else if (ext==L"pcx") type = CXIMAGE_FORMAT_PCX;
#endif
#if CXIMAGE_SUPPORT_WBMP
// else if (ext==L"wbmp") type = CXIMAGE_FORMAT_WBMP;
#endif
#if CXIMAGE_SUPPORT_WMF
// else if (ext==L"wmf"||ext==L"emf") type = CXIMAGE_FORMAT_WMF;
#endif
#if CXIMAGE_SUPPORT_J2K
// else if (ext==L"j2k"||ext==L"jp2") type = CXIMAGE_FORMAT_J2K;
#endif
#if CXIMAGE_SUPPORT_JBG
// else if (ext==L"jbg") type = CXIMAGE_FORMAT_JBG;
#endif
#if CXIMAGE_SUPPORT_JP2
// else if (ext==L"jp2"||ext==L"j2k") type = CXIMAGE_FORMAT_JP2;
#endif
#if CXIMAGE_SUPPORT_JPC
// else if (ext==L"jpc"||ext==L"j2c") type = CXIMAGE_FORMAT_JPC;
#endif
#if CXIMAGE_SUPPORT_PGX
// else if (ext==L"pgx") type = CXIMAGE_FORMAT_PGX;
#endif
#if CXIMAGE_SUPPORT_RAS
// else if (ext==L"ras") type = CXIMAGE_FORMAT_RAS;
#endif
#if CXIMAGE_SUPPORT_PNM
// else if (ext==L"pnm"||ext==L"pgm"||ext==L"ppm") type = CXIMAGE_FORMAT_PNM;
#endif
else type = CXIMAGE_FORMAT_UNKNOWN;
return type;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -