vc打开多个图片 然后点击按钮显示下一张 或上一张
发布网友
发布时间:2022-05-27 16:43
我来回答
共1个回答
热心网友
时间:2023-11-04 12:27
PictureControl就是类CStatic,下面是VC++ WTL的编程用法,你着重看下WM_PAINT及设置图片的函数就能知道,如何用在MFC里了。关于Load图片的函数AtlLoadGdiplusImage(nRes, pstrType),我没贴出来,主要是为了方便你了解。
当你切换图片时,就调用SetBitmap(HBITMAP hBitmap)函数,就可以切换图片了。SizeToFit()函数是根据你设定的图片,自动调整控件窗口大小。
#ifndef _THEMEDIMAGE_H_INCLUDE_
#define _THEMEDIMAGE_H_INCLUDE_
#pragma once
//////////////////////////////////////////////////////////////////////
// Themed Image control
//
class CThemedImageCtrl : public CWindowImpl<CThemedImageCtrl, CStatic>
{
public:
CWindowm_wndThemeParent;
CBrushm_brBack;
CBitmapm_bmpImage;
// Operations
BOOL SubclassWindow(HWND hWnd)
{
ATLASSERT(m_hWnd==NULL);
ATLASSERT(::IsWindow(hWnd));
BOOL bRet = CWindowImpl<CThemedImageCtrl, CStatic>::SubclassWindow(hWnd);
if( bRet ) Init();
return bRet;
}
void Init()
{
m_wndThemeParent = GetParent();
Invalidate();
}
void SetThemeParent(HWND hWnd)
{
ATLASSERT(::IsWindow(hWnd));
m_wndThemeParent = hWnd;
if( !m_brBack.IsNull() ) m_brBack.DeleteObject();
}
void SetBitmap(HBITMAP hBitmap)
{
m_bmpImage = (HBITMAP) ::CopyImage(hBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
if( IsWindow() ) Invalidate();
}
void SetBitmap(UINT nRes, LPCTSTR pstrType)
{
m_bmpImage = AtlLoadGdiplusImage(nRes, pstrType);
if( IsWindow() ) Invalidate();
}
void SizeToFit()
{
ATLASSERT(!m_bmpImage.IsNull());
BITMAP BmpInfo = { 0 };
m_bmpImage.GetBitmap(&BmpInfo);
ResizeClient(BmpInfo.bmWidth, BmpInfo.bmHeight);
if( IsWindow() ) Invalidate();
}
// Message map and handlers
BEGIN_MSG_MAP(CThemedImageCtrl)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
MESSAGE_HANDLER(WM_MOVE, OnSize)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
END_MSG_MAP()
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
Init();
bHandled = FALSE;
return 0;
}
LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
if( !m_brBack.IsNull() ) m_brBack.DeleteObject();
return TRUE;
}
LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
return 1;
}
LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
if( wParam != NULL )
{
DoPaint((HDC) wParam);
}
else
{
CPaintDC dc(m_hWnd);
DoPaint(dc.m_hDC);
}
return 0;
}
// Implementation
void DoPaint(CDCHandle dc)
{
CRect rcClient;
GetClientRect(&rcClient);
// Preserve background
if( m_brBack.IsNull() ) m_brBack = AtlGetBackgroundBrush(m_hWnd, m_wndThemeParent);
HBRUSH hOldBrush = dc.SelectBrush(m_brBack);
dc.PatBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), PATCOPY);
dc.SelectBrush(hOldBrush);
// Paint image (assume that it is a 32bit with alpha-pr-pixel)...
CDC dcCompat;
dcCompat.CreateCompatibleDC(dc);
BITMAP BmpInfo = { 0 };
m_bmpImage.GetBitmap(&BmpInfo);
BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
HBITMAP hOldBitmap = dcCompat.SelectBitmap(m_bmpImage);
dc.AlphaBlend(0, 0, rcClient.Width(), rcClient.Height(), dcCompat, 0, 0, BmpInfo.bmWidth, BmpInfo.bmHeight, bf);
dcCompat.SelectBitmap(hOldBitmap);
}
};
#endif //_THEMEDIMAGE_H_INCLUDE_