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

📄 mididemoview.cpp

📁 此程序是在vc++环境下
💻 CPP
字号:
// MIDIDemoView.cpp : implementation of
// the CMIDIDemoView class
//

#include "stdafx.h"
#include "MIDIDemo.h"

#include "MIDIDemoDoc.h"
#include "MIDIDemoView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMIDIDemoView

IMPLEMENT_DYNCREATE(CMIDIDemoView, CView)

BEGIN_MESSAGE_MAP(CMIDIDemoView, CView)
	//{{AFX_MSG_MAP(CMIDIDemoView)
	ON_COMMAND(ID_MUSIC_SELECTSONG_JUNGLE, OnMusicSelectsong)
	ON_UPDATE_COMMAND_UI(ID_MUSIC_SELECTSONG_CANDYLAND, OnUpdateMusicSelectsong)
	ON_COMMAND(ID_MUSIC_STOP, OnMusicStop)
	ON_UPDATE_COMMAND_UI(ID_MUSIC_STOP, OnUpdateMusicStop)
	ON_COMMAND(ID_MUSIC_PLAY, OnMusicPlay)
	ON_UPDATE_COMMAND_UI(ID_MUSIC_PLAY, OnUpdateMusicPlay)
	ON_COMMAND(ID_MUSIC_SELECTSONG_SPACE, OnMusicSelectsong)
	ON_COMMAND(ID_MUSIC_SELECTSONG_WINTER, OnMusicSelectsong)
	ON_COMMAND(ID_MUSIC_SELECTSONG_CANDYLAND, OnMusicSelectsong)
	ON_UPDATE_COMMAND_UI(ID_MUSIC_SELECTSONG_JUNGLE, OnUpdateMusicSelectsong)
	ON_UPDATE_COMMAND_UI(ID_MUSIC_SELECTSONG_SPACE, OnUpdateMusicSelectsong)
	ON_UPDATE_COMMAND_UI(ID_MUSIC_SELECTSONG_WINTER, OnUpdateMusicSelectsong)
	ON_WM_CREATE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMIDIDemoView construction/destruction

static char *szFilename[] = {
	"sample1.mid", "sample2.mid",
	"sample3.mid", "sample4.mid" };

CMIDIDemoView::CMIDIDemoView()
{

	// Set the initial song to 0
	// and the initial file to sample1.mid.
	m_nSong = 0;
	m_Midi.Open( szFilename[0] );

}

CMIDIDemoView::~CMIDIDemoView()
{
}

BOOL CMIDIDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMIDIDemoView drawing

void CMIDIDemoView::OnDraw(CDC* pDC)
{
	CMIDIDemoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
}

/////////////////////////////////////////////////////////////////////////////
// CMIDIDemoView diagnostics

#ifdef _DEBUG
void CMIDIDemoView::AssertValid() const
{
	CView::AssertValid();
}

void CMIDIDemoView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMIDIDemoDoc* CMIDIDemoView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMIDIDemoDoc)));
	return (CMIDIDemoDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMIDIDemoView message handlers

void CMIDIDemoView::OnMusicSelectsong() 
{
	// Remember if the file is playing.
	BOOL bIsPlaying = m_Midi.IsPlaying();

	// Close the midi file.
	m_Midi.Close();

	// Get the menu message ID.
	WORD wID = GetCurrentMessage()->wParam;

	// Set the song number to 0, 1, 2, or 3
	// depending on the menu ID.
	if( wID == ID_MUSIC_SELECTSONG_JUNGLE )
		m_nSong = 0;
	else if( wID == ID_MUSIC_SELECTSONG_WINTER )
		m_nSong = 1;
	else if( wID == ID_MUSIC_SELECTSONG_CANDYLAND )
		m_nSong = 2;
	else
		m_nSong = 3;

	// Open the CMidi object with the selected
	// song's filename.
	m_Midi.Open( szFilename[m_nSong] );

	// If we were playing when we came into
	// this function, start the audio.
	if( bIsPlaying )
		m_Midi.Play();

}

void CMIDIDemoView::OnUpdateMusicSelectsong(
	CCmdUI* pCmdUI) 
{

	// Set the check for the currently
	// selected song.
	pCmdUI->SetCheck( m_nSong ==
		(int) pCmdUI->m_nIndex );

}

void CMIDIDemoView::OnMusicStop() 
{

	// Stop the playing.
	m_Midi.Stop();

}

void CMIDIDemoView::OnUpdateMusicStop(CCmdUI* pCmdUI) 
{

	// Enable the 'stop' menu selection
	// if the file is currently playing.
	pCmdUI->Enable( m_Midi.IsPlaying() );

}

void CMIDIDemoView::OnMusicPlay() 
{

	// Play the midi file.
	m_Midi.Play();

}

void CMIDIDemoView::OnUpdateMusicPlay(CCmdUI* pCmdUI) 
{

	// Enable the 'play' menu selection
	// if the file is not currently playing.
	pCmdUI->Enable( !m_Midi.IsPlaying() );

}

int CMIDIDemoView::OnCreate(
	LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// Kick off the timer.
	SetTimer( 1, 500, NULL );
	
	return 0;
}

void CMIDIDemoView::OnTimer(UINT nIDEvent) 
{

	// Get a DC to the window.
	CClientDC ClientDC( this );

	// We'll need a CString object
	// to form the information text.
	CString strInfo;

	// Use the GetMinutes() and GetSeconds()
	// functions to obtain the current minutes
	// and seconds. We'll get negative values
	// if the midi file isn't opened, so check
	// for this.
	int nMinutes = m_Midi.GetMinutes();
	if( nMinutes < 0 )
		nMinutes = 0;
	int nSeconds = m_Midi.GetSeconds();
	if( nSeconds < 0 )
		nSeconds = 0;

	// Use the GetLength() function to obtain
	// the total midi file length. We'll get
	// negative values if the midi file isn't
	// opened, so check for this.
	int nTotalMinutes, nTotalSeconds;
	m_Midi.GetLength( &nTotalMinutes, &nTotalSeconds );
	if( nTotalMinutes < 0 )
		nTotalMinutes = 0;
	if( nTotalSeconds < 0 )
		nTotalSeconds = 0;

	// Format the information string so that we can display
	// it to the window.
	strInfo.Format( "File:%s, %s, %02d:%02d %02d:%02d    ",
		szFilename[m_nSong],
		m_Midi.IsPlaying() ? "PLAYING" : "!PLAYING",
		nMinutes, nSeconds,
		nTotalMinutes, nTotalSeconds );

	// Write the information string to the
	// window.
	ClientDC.TextOut( 10, 10, strInfo );
	
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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