这个是在闲暇的时候做的小程序,实现滚动显示图片。这里使用的图片是加载到资源中的,有需要从文件夹中加载的,可以稍微修改以下即可使用。
在这里我是把图片画在一个对话框上面,这是我创建对话框的IDD_DIALOG_PICTURE
#pragma once
// CPicturePage dialog
class CPicturePage : public CDialog
{
DECLARE_DYNAMIC(CPicturePage)
public:
BOOL m_bTrackMouse;
int m_iSwitchType;//1上一张 2下一张
BOOL m_bStop;
int m_iWndWidth;
int m_iIdx;
Image* GetPngStream(UINT uiID);
void ShowPic(CDC *pDC,UINT nID,int sx,int sy,int nScrWidth, int nScrHeight);
CPicturePage(CWnd* pParent = NULL); // standard constructor
virtual ~CPicturePage();
// Dialog Data
enum { IDD = IDD_DIALOG_PICTURE };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
virtual BOOL PreTranslateMessage(MSG* pMsg);
DECLARE_MESSAGE_MAP()
afx_msg void OnPaint();
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};
实现,涉及一些字体的创建,使用者自己百度
// PicturePage.cpp : implementation file
//
#include "stdafx.h"
#include "NtWonder.h"
#include "PicturePage.h"
#include "NtWonderDlg.h"
// CPicturePage dialog
CFont* g_DisplayFont16;
CFont* g_DisplayFont24;
CFont* g_DisplayFont32;
CString g_strProduct[13] = {_T("NUM1"),_T("NUM2"),_T("NUM3"),_T("NUM4"),_T("NUM5"),_T("NUM6"),_T("NUM7"),_T("NUM8"),_T("NUM9"),_T("NUM10"),_T("NUM11"),_T("NUM12"),_T("NUM13")};
IMPLEMENT_DYNAMIC(CPicturePage, CDialog)
CPicturePage::CPicturePage(CWnd* pParent /*=NULL*/)
: CDialog(CPicturePage::IDD, pParent)
{
m_iIdx = 0;
m_iWndWidth = 0;
m_iSwitchType = 0;
m_bStop = FALSE;
m_bTrackMouse = FALSE;
}
CPicturePage::~CPicturePage()
{
}
void CPicturePage::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CPicturePage, CDialog)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_MESSAGE(WM_MOUSELEAVE, &CPicturePage::OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, &CPicturePage::OnMouseHover)
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CPicturePage message handlers
BOOL CPicturePage::OnInitDialog()
{
CDialog::OnInitDialog();
SetTimer(1,2000,NULL);
return TRUE;
}
BOOL CPicturePage::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
return TRUE;
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
void CPicturePage::OnPaint()
{
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
m_iWndWidth = rc.Width();
CDC dcMem;
CBitmap bmpWnd;
dcMem.CreateCompatibleDC(&dc);
bmpWnd.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
dcMem.SelectObject(&bmpWnd);
dcMem.FillSolidRect(rc,((CNtWonderDlg*)GetParent())->m_clrSkin);
/* COLORREF lss = ((CNtWonderDlg*)GetParent())->m_clrSkin;
CString str;
str.Format(_T("%d %d %d"),GetRValue(lss),GetGValue(lss),GetBValue(lss));
AfxMessageBox(str);*/
Graphics graphics( dcMem.m_hDC);
Image* pImg = GetPngStream(IDR_JPG1+m_iIdx);
graphics.DrawImage(pImg,0,0,200,260);
CRect rcProduct = rc;
rcProduct.top = 260;
dcMem.SelectObject(g_DisplayFont24);
dcMem.SetBkMode(TRANSPARENT);
dcMem.DrawText(g_strProduct[m_iIdx],&rcProduct,DT_CENTER|DT_VCENTER);
dc.BitBlt(0,0,rc.Width(),rc.Height(),&dcMem,0,0,SRCCOPY);
}
Image* CPicturePage::GetPngStream(UINT uiID)
{
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(uiID),_T("PNG")); // type
if (!hRsrc)
return NULL;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return NULL;
// Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
// load from stream这是关键一句,通过FromStream返回以各Image*,然后在Graphic的DrawImage地方调用就行了!
Image* pImg = Gdiplus::Image::FromStream(pstm);
// free/release stuff
GlobalUnlock(m_hMem);
pstm->Release();
FreeResource(lpRsrc);
return pImg;
}
void CPicturePage::ShowPic(CDC *pDC,UINT nID,int sx,int sy,int nScrWidth, int nScrHeight)
{
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),_T("JPG")); // type
if (!hRsrc)
return;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return;
IPicture *pPic = NULL;
IStream *pStm = NULL;
//分配全局存储空间
HGLOBAL hGlobal=GlobalAlloc(GMEM_FIXED,len);
if(hGlobal==NULL)
return;
BYTE* pvData = (BYTE*)GlobalLock(hGlobal);
if(pvData==NULL)//锁定分配内存块
{
GlobalFree(hGlobal);
return;
}
memcpy(pvData,lpRsrc,len);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal,TRUE,&pStm);
//装入图形文件
OleLoadPicture(pStm,len,TRUE,IID_IPicture,(LPVOID*)&pPic);
pStm->Release();
pStm = NULL;
// GlobalFree(hGlobal);//First-chance exception at 0x7c990441 in NtWonder.exe: 0xC0000005: Access violation reading location 0x02990018.
if(NULL == pPic)return;
long hmWidth;//图片的真实宽度
long hmHeight;//图片的真实高度
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//将图形输出到屏幕上(有点像BitBlt)
pPic->Render(pDC->m_hDC,sx,sy,nScrWidth,nScrHeight,0,hmHeight,hmWidth,-hmHeight,NULL);
pPic->Release();
}
void CPicturePage::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(!m_bStop)
{
m_iIdx ++;
if(m_iIdx == 13)
m_iIdx = 0;
Invalidate();
}
CDialog::OnTimer(nIDEvent);
}
void CPicturePage::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// if(!m_bTrackMouse)
{
m_bTrackMouse = TRUE;
TRACKMOUSEEVENT tm;
tm.cbSize = sizeof(TRACKMOUSEEVENT);
//要注册什么消息
//TME_HOVER 对应 WM_MOUSEHOVER
//TME_LEAVE 对应 WM_MOUSELEAVE
tm.dwFlags = TME_HOVER|TME_LEAVE;
tm.dwHoverTime = HOVER_DEFAULT;
tm.hwndTrack = this->m_hWnd;
_TrackMouseEvent(&tm);
}
m_bStop = TRUE;
if(point.x <= (m_iWndWidth/2))
{
m_iSwitchType = 1;
SetCursor(::LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR_LEFT)));
}
else
{
m_iSwitchType = 2;
SetCursor(::LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR_RIGHT)));
}
CDialog::OnMouseMove(nFlags, point);
}
void CPicturePage::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_iSwitchType == 1)
{
m_iIdx --;
if(m_iIdx == -1)
m_iIdx = 12;
}
else if(m_iSwitchType == 2)
{
m_iIdx ++;
if(m_iIdx == 13)
m_iIdx = 0;
}
Invalidate();
CDialog::OnLButtonDown(nFlags, point);
}
LRESULT CPicturePage::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bStop = FALSE;
return 0;
}
LRESULT CPicturePage::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
return 0;
}
BOOL CPicturePage::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
// return CDialog::OnEraseBkgnd(pDC);
return TRUE;
}