一共3个按钮,“打开”上一张“ ”“下一张”,点击打开按钮选择要打开的图片显示在picture控件中,点击上一张显示上一张,点击下一张 显示下一张。求代码~~~~~~~ 点击打开按钮时有多张图片,选择一张,其余的都选中了,然后点击上一张实现其功能。求代码
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:
CWindow m_wndThemeParent;
CBrush m_brBack;
CBitmap m_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_
当你切换图片时,就调用SetBitmap(HBITMAP hBitmap)函数,就可以切换图片了。SizeToFit()函数是根据你设定的图片,自动调整控件窗口大小。
#ifndef _THEMEDIMAGE_H_INCLUDE_
#define _THEMEDIMAGE_H_INCLUDE_
#pragma once
//////////////////////////////////////////////////////////////////////
// Themed Image control
//
class CThemedImageCtrl : public CWindowImpl<CThemedImageCtrl, CStatic>
{
public:
CWindow m_wndThemeParent;
CBrush m_brBack;
CBitmap m_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_
温馨提示:答案为网友推荐,仅供参考