首先配置GDI+
在stdafx.h中添加
#include <gdiplus.h>
using namespace Gdiplus;
为App类添加成员变量
private:
GdiplusStartupInput m_GdiPlusStartupInput;
ULONG_PTR m_GdiplusToken;
在App::InitInstance添加GDI+初始化代码
GdiplusStartup(&m_GdiplusToken, &m_GdiPlusStartupInput, NULL);
在App::ExitInstance添加GDI+关闭代码
GdiplusShutdown(m_GdiplusToken);
至此,GDI+配置完毕
其中图像显示的关键是图像偏移量,在图像大小超过视图的时候显示区域只能显示部分图像,并通过鼠标拖动来改变偏移量。
用PicWidth和PicHeight来表示显示图像的宽度和高度
用ViewWidth和ViewHeight来表示视图的宽度和高度
左上角坐标为(x0,y0)则
ViewWidth - PicWidth <= x0 <= 0
ViewHeight - PicHeight <= y0 <= 0
通过上述两式可以判断偏移量的最大值
在View类中添加成员变量和成员函数,用于控制图像显示。
public:
bool m_bWidthOut;
bool m_bHeightOut;
int m_nShowType;
//以指定模式显示图像
void ShowPicture(CDC* pDC, Image& image, int nShowType);
//设置显示模式
void SetShowType(int nType);
//获得指定模式下图像大小
CSize GetShowPicSize(Image& image, int nShowType);
//从指定路径打开图像
void OpenFile(const CString& strPath);
/************************************************************************/
/* 清除图像的偏移坐标 */
/************************************************************************/
void ClearOffset(void)
{
m_nXX = m_nYY = m_nXXMax = m_nYYMax = 0;
}
void FixOffset(void); //修正图像的偏移量
/************************************************************************/
/* 获得指定索引文件的全路径 */
/************************************************************************/
CString GetFilePath(int nIndex)
{
ASSERT(nIndex >= 0 && nIndex < m_nPicNum);
return m_strPath + L"\\" + m_FilesNameAry[nIndex];
}
private:
int m_nPos; //当前文件在容器中的索引
int m_nEffectDisplayType; //当前特效类型
bool m_bEffectDraw; //是否使用特效显示
Bitmap* m_pBitmap; //图片信息
CString m_strPath; //当前打开目录
int m_nPicNum; //当前目录的文件个数
CStringArray m_FilesNameAry; //保存当前目录下所有文件
int m_nShowPicHeight; //显示图片的高度
int m_nShowPicWidth; //显示图片的宽度
CPoint m_psMove; //记录移动前鼠标的位置
int m_nXX; //图片X坐标的偏移量
int m_nYY; //图片Y坐标的偏移量
int m_nXXMax; //X坐标最大偏移量
int m_nYYMax; //Y坐标最大偏移量
//图像显示类型枚举
enum ShowType
{
ST_NORMAL = 0, //原始大小
ST_FIXWIDTH = 1, //适合宽度
ST_FIXHEIGHT = 2, //适合高度
ST_FIXSCREEN = 3 //适合屏幕
};
在View.cpp里添加函数定义
首先是构造函数和析构函数
CGraphShowView::CGraphShowView()
: m_strPath(_T(""))
{
// TODO: 在此处添加构造代码
m_nShowType = ST_NORMAL;
m_bHeightOut = false;
m_bWidthOut = false;
m_nXX = 0;
m_nYY = 0;
m_nXXMax = 0;
m_nYYMax = 0;
m_pBitmap = NULL;
m_bEffectDraw = false;
m_nEffectDisplayType = EDT_RAND;
m_nPos = -1;
}
CGraphShowView::~CGraphShowView()
{
delete m_pBitmap;
}