⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myshowcontrol.cpp

📁 "Visual C++.net实践与提高-深入Windows编程"的源代码
💻 CPP
字号:
// MyBitmapShow.cpp : implementation file
//

#include "stdafx.h"
#include "MyShowControl.h"
#define MYBUTTON_CLASSNAME    _T("MyBitmapShow")  

// CMyBitmapShow

IMPLEMENT_DYNAMIC(CMyBitmapShow, CWnd)

CMyBitmapShow::CMyBitmapShow()
: b_MouseOver(false)
{
	m_iBitmapSum = -1 ;
	m_iCurrentBitmapIndex = 0 ;
	m_nTimer = 0 ;
	m_nBreakSeconds = 1000 ;
	m_bShowing = false ;

	RegisterWindowClass();
}

CMyBitmapShow::~CMyBitmapShow()
{
}


BEGIN_MESSAGE_MAP(CMyBitmapShow, CWnd)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
	ON_WM_TIMER()
END_MESSAGE_MAP()



// CMyBitmapShow message handlers

bool CMyBitmapShow::RegisterWindowClass()
{
	WNDCLASS wndcls;
    HINSTANCE hInst = AfxGetInstanceHandle();

    if (!(::GetClassInfo(hInst, MYBUTTON_CLASSNAME, &wndcls)))
    {
        // otherwise we need to register a new class
        wndcls.style            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
        wndcls.lpfnWndProc      = ::DefWindowProc;
        wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
        wndcls.hInstance        = hInst;
        wndcls.hIcon            = NULL;
        wndcls.hCursor          = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        wndcls.hbrBackground    = (HBRUSH) (COLOR_3DFACE + 1);
        wndcls.lpszMenuName     = NULL;
        wndcls.lpszClassName    = MYBUTTON_CLASSNAME;

        if (!AfxRegisterClass(&wndcls))
        {
            AfxThrowResourceException();
            return FALSE;
        }
    }

    return true;	
}

void CMyBitmapShow::OnPaint()
{
	if (m_bShowing)
    {
	    CPaintDC dc(this); // device context for painting

        // Create memory DC
//        CDC MemDC;
//        if (!MemDC.CreateCompatibleDC(&dc))
//            return;

        // Get Size of Display area
        CRect rect;
        GetClientRect(rect);

        // Get size of bitmap
		POINT pt ;
		pt.x = 0 ; pt.y = 0 ;
		m_PictureList.Draw( &dc , m_iCurrentBitmapIndex , pt , ILD_NORMAL ) ;
/*
		m_PictureList.GetBitmap(&bm);
        
        // Draw the bitmap
        CBitmap* pOldBitmap = (CBitmap*) MemDC.SelectObject(&m_Bitmap);
        dc.StretchBlt(0, 0, rect.Width(), rect.Height(), 
                      &MemDC, 
                      0, 0, bm.bmWidth, bm.bmHeight, 
                      SRCCOPY);
        MemDC.SelectObject(pOldBitmap);      
*/
    }

}

BOOL CMyBitmapShow::OnEraseBkgnd(CDC* pDC)
{
	if (m_bShowing)
        return TRUE;
	return CWnd::OnEraseBkgnd(pDC);
}

BOOL CMyBitmapShow::AddBitmap(UINT nIDResource)
{
	BOOL retn = false ;
	CBitmap * pbmp = new CBitmap ;
	if( pbmp->LoadBitmap(nIDResource))
	{
		if( Add( pbmp ) )
		{
			retn = true ;
		}
		else
		{
			pbmp->DeleteObject() ;
			delete pbmp ;
		}
 	}
	return retn ;
}

BOOL CMyBitmapShow::Add( CBitmap * pbmp)
{
	// 如果图片列表中没有图片,则需要Create一下,
	// 如果图片列表中位图与所添加位图大小不一致,则取消添加操作
	BOOL retn = false ;
	BITMAP bm ;
	pbmp->GetBitmap( &bm ) ;
	if( m_iBitmapSum < 0 )
	{
		m_sizeBmp.cx = bm.bmWidth ;
		m_sizeBmp.cy = bm.bmHeight ;
		m_PictureList.Create(m_sizeBmp.cx , m_sizeBmp.cy , ILC_COLOR , 0 , 4 ) ;
		m_iBitmapSum = 0 ;
	}
	if( m_sizeBmp.cx == bm.bmWidth && m_sizeBmp.cy == bm.bmHeight ) 
	{
		if( m_PictureList.Add( pbmp , RGB(0,0,0)) != -1 )
		{
			m_iBitmapSum ++ ;
			retn = true ;
		}
	}
	return retn ;
}

BOOL CMyBitmapShow::AddBitmap(LPCTSTR pstrBmpFile)
{
	BOOL retn = false ;
	CBitmap * pbmp = new CBitmap ;
	if( pbmp->LoadBitmap(pstrBmpFile))
	{
		if( Add( pbmp ) )
		{
			retn = true ;
		}
		else
		{
			pbmp->DeleteObject() ;
			delete pbmp ;
		}
	}
	return retn ;
}

BOOL CMyBitmapShow::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
	
	BOOL returnval = CWnd::Create(MYBUTTON_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID);
	//Track
	return returnval ;	

}

void CMyBitmapShow::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if( !b_MouseOver){
		b_MouseOver = true;

		TRACKMOUSEEVENT tr; 
		tr.cbSize=sizeof(tr);
		tr.dwFlags=TME_LEAVE; 
		tr.hwndTrack=this->m_hWnd ; 
		TrackMouseEvent(&tr); // 鼠标离开时发送:WM_MOUSELEAVE
		// 如果存在计时器,则停止计时,(鼠标在控件之上时,图片不再循环放映,而是静态显示当前图片)
		if( m_nTimer )
			KillTimer(m_nTimer) ;
		m_nTimer = 0 ;
	}

	CWnd::OnMouseMove(nFlags, point);
}

LRESULT CMyBitmapShow::OnMouseLeave(WPARAM wParam , LPARAM lParam)
{
	b_MouseOver = false ;
	// 鼠标移走,则打开新的计时器,重新计时循环
	if( m_bShowing )
	{
		m_nTimer = (UINT) SetTimer(1,m_nBreakSeconds,0) ;
	}
	return 0 ;
}

void CMyBitmapShow::OnTimer(UINT nIDEvent)
{
	// 时间间隔到,循环显示下一幅画
    if( m_iCurrentBitmapIndex < m_iBitmapSum - 1) // 0 - (m_iBitmapSum-1)
	{
		m_iCurrentBitmapIndex ++ ;
	}
	else
	{
		m_iCurrentBitmapIndex = 0 ;
	}
	if( m_bShowing)	// 如果在图片列表中存在图片,则需要在控件窗口重绘
		Invalidate() ;		// 强制重画控件
	CWnd::OnTimer(nIDEvent);
}

void CMyBitmapShow::SetBreakTime(UINT nseconds) 
{
	ASSERT( nseconds > 10 ) ;
	m_nBreakSeconds = nseconds ;
	if( m_nTimer ){
		KillTimer(m_nTimer) ;
		m_nTimer = (UINT) SetTimer(1,m_nBreakSeconds, 0) ;
	}
}

void CMyBitmapShow::BeginShow()
{
	// 开始显示位图,如果位图列表中有位图存在,则开始显示位图,并开始计时
	// 否则什么都不作。
	if( m_iBitmapSum > 0 ){
		m_bShowing = true ;
		m_nTimer = (UINT) SetTimer(1,m_nBreakSeconds,0) ;
		Invalidate() ;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -