📄 picimgdlg.cpp
字号:
// PicImgDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PicImg.h"
#include "PicImgDlg.h"
#include "FullPic.h"
#include <imaging.h>
#include <initguid.h>
#include <imgguids.h>
#include "Help.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Global Variable
/////////////////////////////////////////////////////////////////////////////
// CPicImgDlg dialog
CPicImgDlg::CPicImgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPicImgDlg::IDD, pParent)
{
m_curbmpptr = NULL;
//{{AFX_DATA_INIT(CPicImgDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPicImgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPicImgDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPicImgDlg, CDialog)
//{{AFX_MSG_MAP(CPicImgDlg)
ON_BN_CLICKED(IDC_LIST, OnList)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_FULLSCREEN, OnFullscreen)
ON_BN_CLICKED(IDC_NEXT, OnNext)
ON_BN_CLICKED(IDC_PREVIOUS, OnPrevious)
ON_BN_CLICKED(IDC_ZOOMIN, OnZoomin)
ON_BN_CLICKED(IDC_ZOOMOUT, OnZoomout)
ON_BN_CLICKED(ID_BACK, OnBack)
ON_WM_LBUTTONUP()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_HELP, OnHelp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPicImgDlg message handlers
BOOL CPicImgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
this->SetWindowPos(NULL,0,0,640,480,SWP_NOZORDER);
this->SetWindowText(_T("ImageViewer"));
//打开按键光
m_light = NULL; //灯灭的句柄无效
m_signal = TRUE;
//定时器的设置
SetTimer(1128,10000,NULL); //修改按键灯灭
m_light = CreateFile(L"DSK2:",GENERIC_READ|GENERIC_WRITE,0,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
m_bg.LoadBitmap(IDB_BG);
m_rate = 1000;
// 设置显示区大小
m_rtimage = CRect(16,44,548,443); // 图象显示区大小
m_rtopen = CRect(561,426,631,476); // 图象打开
m_rtfullscreen = CRect(561,126,631,176); // 图象全屏
m_rtprv = CRect(561,306,631,356); // 前一张
m_rtnext = CRect(561,366,631,416); // 下一张
m_rtzoomin = CRect(561,246,631,296); // 放大
m_rtzoomout = CRect(561,186,631,236); // 缩小
m_rtclose = CRect(590,0,630,30); // 关闭
m_rtname = CRect(100,0,540,30);
m_rthelp = CRect(540,0,580,30);
m_fullflag = FALSE;
m_curbmpptr = NULL;
if(m_curfile!="")
{
SetTimer(88,0,0);
}
else
{
OnOK();
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CPicImgDlg::OnList()
{
PictureSkim mypicturescan;
int n_Response=mypicturescan.DoModal();
if(n_Response==IDOK)
{
// 文件正常打开
if(mypicturescan.filename!="")
{
m_curfile =mypicturescan.filename;
m_rate = 1000;
}
else
return;
}
else
return;
ShowImage();
}
// 将文件读到内存
HBITMAP CPicImgDlg::ImgConvert(CString strFileName)
{
IImage* m_pImage = NULL;
IImagingFactory* m_pImagingFactory = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
HRESULT hr = CoCreateInstance(CLSID_ImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IImagingFactory,
(void **)&m_pImagingFactory);
HBITMAP hBitmap = 0;
ImageInfo imageInfo;
if (SUCCEEDED(hr))
{
hr = m_pImagingFactory->CreateImageFromFile(strFileName, &m_pImage);//从内存中读取图片
if (FAILED(hr))
{
return 0;
}
m_pImage->GetImageInfo(&imageInfo);
CWindowDC dc(0);
CDC dcBitmap;
dcBitmap.CreateCompatibleDC(&dc);
hBitmap = CreateCompatibleBitmap(dc.GetSafeHdc(), imageInfo.Width, imageInfo.Height);
if (hBitmap)
{
HGDIOBJ hOldBitmap = dcBitmap.SelectObject(hBitmap);
m_pImage->Draw(dcBitmap.GetSafeHdc(), CRect(0, 0, imageInfo.Width, imageInfo.Height), NULL);
dcBitmap.SelectObject(hOldBitmap);
}
m_pImage->Release();
}
m_pImagingFactory->Release();
CoUninitialize();
return hBitmap;
}
void CPicImgDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC memdc;
memdc.CreateCompatibleDC(&dc);
memdc.SelectObject(m_bg);
dc.BitBlt(0,0,640,480,&memdc,0,0,SRCCOPY);
HGDIOBJ hOldBitmap = memdc.SelectObject(m_curbmpptr); //内存目标
CBrush br(RGB(0,0,0));
int show_x,show_y,show_w,show_h;
int src_x,src_y,src_w,src_h;
BITMAP bm;
if (m_curbmpptr!=NULL)
m_curbmpptr->GetObject(sizeof(bm), &bm);
else
{
memdc.DeleteDC();
return;
}
//全屏的时候是没有放大和缩小的,在这里应该隐藏按钮的。
if (m_fullflag)
{
// 区域
if (ifullwidth * m_rate > 640 * 1000)
{
src_w = (640*bm.bmWidth*1000)/(m_rate*ifullwidth);
src_x = (bm.bmWidth - src_w)/2;
}
else
{
src_x = 0;
src_w = bm.bmWidth;
}
if (ifullheight * m_rate > 480 * 1000)
{
src_h = (480*bm.bmHeight*1000)/(m_rate*ifullheight);
src_y = (bm.bmHeight - src_h)/2;
}
else
{
src_y = 0;
src_h = bm.bmHeight;
}
if ((640-ifullwidth*m_rate/1000)/2 > 0)
{
show_x = (640-ifullwidth*m_rate/1000)/2;
show_w = ifullwidth*m_rate/1000;
}
else
{
show_x = 0;
show_w = 640;
}
if ((480-ifullheight*m_rate/1000)/2 > 0)
{
show_y = (480-ifullheight*m_rate/1000)/2;
show_h = ifullheight*m_rate/1000;
}
else
{
show_y = 0;
show_h = 480;
}
//画图
dc.FillRect(CRect(0,0,640,480),&br);
dc.StretchBlt(show_x, show_y, show_w, show_h, &memdc, src_x,src_y,src_w,src_h, SRCCOPY);
}
else
{
// 区域
if (iareawidth * m_rate > m_rtimage.Width() * 1000)
{
src_w = (m_rtimage.Width()*bm.bmWidth*1000)/(m_rate*iareawidth);
src_x = (bm.bmWidth - src_w)/2;
}
else
{
src_x = 0;
src_w = bm.bmWidth;
}
if (iareaheight * m_rate > m_rtimage.Height() * 1000)
{
src_h = (m_rtimage.Height()*bm.bmHeight*1000)/(m_rate*iareaheight);
src_y = (bm.bmHeight - src_h)/2;
}
else
{
src_y = 0;
src_h = bm.bmHeight;
}
if ((m_rtimage.Width()-iareawidth*m_rate/1000)/2 > 0)
{
show_x = (m_rtimage.Width()-iareawidth*m_rate/1000)/2;
show_w = iareawidth*m_rate/1000;
}
else
{
show_x = 0;
show_w = m_rtimage.Width();
}
if ((m_rtimage.Height()-iareaheight*m_rate/1000)/2 > 0)
{
show_y = (m_rtimage.Height()-iareaheight*m_rate/1000)/2;
show_h = iareaheight*m_rate/1000;
}
else
{
show_y = 0;
show_h = m_rtimage.Height();
}
//画图
dc.FillRect(&m_rtimage,&br);
dc.StretchBlt(m_rtimage.left+show_x, m_rtimage.top+show_y, show_w, show_h, &memdc, src_x,src_y,src_w,src_h, SRCCOPY);
}
//把字的区域显示出来
CFont ft;
ft.CreateFont(
25, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial"));
dc.SelectObject(&ft);
dc.SetBkMode(TRANSPARENT);
dc.DrawText(m_filename,m_rtname,DT_CENTER|DT_VCENTER);
ft.DeleteObject();
memdc.SelectObject(hOldBitmap);
memdc.DeleteDC();
}
void CPicImgDlg::OnFullscreen()
{
m_fullflag = !m_fullflag;
RedrawWindow();
}
BOOL CPicImgDlg::OnNext()
{
// TODO: Add your control notification handler code here
if(m_curfile=="")
return FALSE;
else
{
CString strname = m_curfile;
CString strpath;
CString fullformat;
CString fixname;
int i;
i = strname.ReverseFind('\\');
strpath = strname.Left( i );
fullformat = strpath + "\\*.*";
WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile(fullformat,&fd);
int a = TRUE;
while((hFind != INVALID_HANDLE_VALUE) &&a)
{
if( wcscmp( m_curfile,strpath + "\\" + fd.cFileName ) )
{
a = ::FindNextFile(hFind,&fd); //a是判断是否还有文件.
}
else
{
a = ::FindNextFile(hFind,&fd);
//在这里判断是不是最后一幅.
if( a != FALSE )
{
splitpath ( fd.cFileName, fixname );
fixname.MakeLower();
if ( !wcscmp ( fixname, _T(".jpg") ) || !wcscmp ( fixname, _T(".bmp") ) )
{
m_curfile = strpath + "\\" + fd.cFileName;
a = FALSE;
ShowImage(); //防止不是需要的文件时候出错。
}
else //防止是文件夹的情况.
m_curfile = strpath + "\\" + fd.cFileName;
}
else
{
//找到第一个文件,判断它的类型,hFind要重新指定第一个句柄
hFind = FindFirstFile(fullformat,&fd);
splitpath ( fd.cFileName, fixname );
fixname.MakeLower();
if ( !wcscmp ( fixname, _T(".jpg") ) || !wcscmp ( fixname, _T(".bmp") ) )
{
m_curfile = strpath + "\\" + fd.cFileName;
ShowImage(); //防止不是需要的文件时候出错。
}
else //防止是文件夹的情况.
{
//第一个文件不一定是图片文件,需要继续往下找,要把a置TRUE.
a = TRUE;
m_curfile = strpath + "\\" + fd.cFileName;
}
}
}
}
FindClose(hFind);
}
return TRUE;
}
//如果要实现到第一个后能够上翻到最后一个,就要加一个函数能够找到第一个和最后一个图片的文件名
BOOL CPicImgDlg::OnPrevious()
{
if(m_curfile=="")
return FALSE;
else if( m_curfile == GetFirstName() )
{
m_curfile = GetLastName();
ShowImage();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -