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

📄 midiplayerdlg.cpp

📁 一个用evc开发的在windows mobile和wince上运行mid播放器
💻 CPP
字号:
// MidiPlayerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MidiPlayer.h"
#include "MidiPlayerDlg.h"

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

#include "MidiEngineApi.h"

#define PLAYER_DLG_WIDTH 100
#define PLAYER_DLG_HEIGHT 26

// Globle data and variabls

void CALLBACK Play_TimerProc(HWND Handle, UINT Msg, UINT idEvent, DWORD Time);
#define TimerEvent 2000
BOOL bStopRequested = FALSE;

/////////////////////////////////////////////////////////////////////////////
// CMidiPlayerDlg dialog

CMidiPlayerDlg::CMidiPlayerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMidiPlayerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMidiPlayerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMidiPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMidiPlayerDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
	// or to use CWnd::SubclassDlgItem()
//	DDX_Control(pDX, IDC_BTN_PLAY, m_btnPlay);
//	DDX_Control(pDX, IDC_BTN_CLOSE, m_btnClose);
//	DDX_Control(pDX, IDC_BTN_STOP, m_btnStop);
}

BEGIN_MESSAGE_MAP(CMidiPlayerDlg, CDialog)
	//{{AFX_MSG_MAP(CMidiPlayerDlg)
	ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
	ON_BN_CLICKED(IDC_BTN_PLAY, OnBtnPlay)
	ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_CTLCOLOR()
	ON_WM_ACTIVATE()
	//}}AFX_MSG_MAP
//	ON_MESSAGE(WM_PLAY_NEW_MIDI, PlayNewMidiFile)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMidiPlayerDlg message handlers

BOOL CMidiPlayerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen
	SetWindowPos(&CWnd::wndTopMost, 0, 0, PLAYER_DLG_WIDTH, PLAYER_DLG_HEIGHT, /*SWP_NOSIZE |*/ SWP_SHOWWINDOW | SWP_NOMOVE);
	SetWindowText(IDS_TITLE);

	// Or to use DDX_Control()
	m_btnPlay.SubclassDlgItem(IDC_BTN_PLAY, this);
	m_btnStop.SubclassDlgItem(IDC_BTN_STOP, this);
	m_btnClose.SubclassDlgItem(IDC_BTN_CLOSE, this);

	// Load bitmaps for the buttons
	m_btnPlay.LoadBitmaps(IDB_BMP_PLAY_UP, IDB_BMP_PLAY_DOWN, IDB_BMP_PLAY_DOWN);
	m_btnStop.LoadBitmaps(IDB_BMP_STOP_UP, IDB_BMP_STOP_DOWN, IDB_BMP_STOP_DOWN);
	m_btnClose.LoadBitmaps(IDB_BMP_CLOSE_UP, IDB_BMP_CLOSE_DOWN);
	// Size controls
	CWnd* pWnd = NULL;
	pWnd = GetDlgItem(IDC_DRAGBAR);
	pWnd->MoveWindow(2, 1, 14, 22);
	pWnd = GetDlgItem(IDC_BTN_STOP);
	pWnd->MoveWindow(18, 2, 22, 21);
	pWnd = GetDlgItem(IDC_BTN_PLAY);
	pWnd->MoveWindow(42, 2, 22, 21);
	pWnd = GetDlgItem(IDC_SEPARATOR);
	pWnd->MoveWindow(68, 4, 3, 18);
	pWnd = GetDlgItem(IDC_BTN_CLOSE);
	pWnd->MoveWindow(74, 2, 22, 21);

	// TODO: Add extra initialization here
	if(!szFileName.IsEmpty())
	{
	//	szFileName.TrimLeft('"');
	//	szFileName.TrimRight('"');
		pPlayThead = AfxBeginThread(Play_Thread, this, 0, 0, 0, NULL);
		SetPlayState(FALSE);
	}
	else
	{
	//	MessageBox(L"Filename is NULL", L"Failed", MB_OK);
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CMidiPlayerDlg::OnBtnClose() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
}

void CMidiPlayerDlg::OnBtnPlay() 
{
	if(midi_IsPlaying() == 0)
	{
		SetPlayState(FALSE);
		pPlayThead = AfxBeginThread(Play_Thread, this, 0, 0, 0, NULL);
	}
}

void CMidiPlayerDlg::OnBtnStop() 
{
	// TODO: Add your control notification handler code here
//	if(midi_IsPlaying() == 4)
	{
		SetPlayState(TRUE);
		bStopRequested = TRUE;
		ResetMidi();
	}
}

int CMidiPlayerDlg::InitMidi()
{
	int error = 0;
	//// SetOutputRate
	midi_SetOutputRate(22050);

	//// SetConfigurationLocation
	//TCHAR *configfilename = TEXT("Config.cfg");
	//TCHAR PathAndName[256];
	//char szPathName[256];
	//GetModuleFileName(NULL, PathAndName, 256);
	//TCHAR *p;
	//p = wcsrchr(PathAndName, '\\');
	//TCHAR szTemp[256];
	//wcsncpy(szTemp, PathAndName, p-PathAndName+1);
	//wcscat(szTemp, configfilename);
	//MessageBox(PathAndName);
	//wcstombs(szPathName, szTemp, 256);
	//CString configFilename(L"Config.cfg");
	//CString pathname(PathAndName);
	//CString path;
	//path = pathname.Left(pathname.ReverseFind('\\')+1);
	//path += configFilename;
	//MessageBox(path);
	//MessageBox((LPCTSTR)path);
	//char *str;
	//str = (char*)(LPTSTR)(LPCTSTR)path;

	TCHAR szTemp[256] = TEXT("\\Windows\\MidiConfig.cfg");
	char szPathName[256];
	wcstombs(szPathName, szTemp, 256);

	midi_SetConfigurationLocation(szPathName);

	// Open
	error = midi_open();
	if( error != 0)
	{
		CString s;
		s.Format(L"Open device return error: %d", error);
	//	MessageBox(s);
	}
	// volume from 1 to 800
	midi_SetVolume(500);
	return error;
}

void CMidiPlayerDlg::ResetMidi()
{
	// Closing properly...
	if (midi_IsPlaying() < 5)
	{
		midi_Stop();
	}
	midi_close();
	// Kill timer
	KillTimer (TimerEvent);
	pPlayThead = NULL;
}

int CMidiPlayerDlg::PlayMidiFile(CString szFileName)
{
	char *str;
	str = (char*)(LPTSTR)(LPCTSTR)szFileName;
	if(midi_StartFile(str) != 0)
	{
	//	MessageBox(L"PlayMidi Failed");
		return 1;
	}
	
	midi_ComputeBuffer(2);
	int timer = SetTimer(TimerEvent, 1000, Play_TimerProc);
	bStopRequested = FALSE;
	SetPlayState(FALSE);
	
	return 0;
}
// Midi play timer callback function
void CALLBACK Play_TimerProc(HWND Handle, UINT Msg, 
	UINT idEvent, DWORD Time)
{
    if (!bStopRequested || midi_IsPlaying() == 4)
    {
		if ((midi_GetCurrentTime(0) > midi_GetTotalTime(0)+3))
        {// End of Midi file
			HWND hWnd = ::FindWindow(NULL, IDS_TITLE);
			{
				if(hWnd != NULL)
					::SendMessage (hWnd, WM_COMMAND, MAKEWPARAM(IDC_BTN_STOP, BN_CLICKED), NULL);
			}
		//	KillTimer (NULL, TimerEvent);
		//	midi_Stop(); 
		//	midi_close();
            return;
        }
		midi_ComputeBuffer(1);	
    }
	else
	{
	//	MessageBox(NULL, TEXT("Midi stop"), NULL, MB_OK);
		KillTimer (NULL, TimerEvent);
		midi_Stop(); 
		midi_close();
        return;
	}
}

UINT CMidiPlayerDlg::Play_Thread(LPVOID pParam)
{
	// Wait until UI loads
	Sleep(300);
	CMidiPlayerDlg * midiPlayerDlg = (CMidiPlayerDlg*)pParam;
	midiPlayerDlg->ResetMidi();
	midiPlayerDlg->InitMidi();
	midiPlayerDlg->PlayMidiFile(midiPlayerDlg->szFileName);
	return 0;
}

// Currently this function is not used
//int CMidiPlayerDlg::PlayNewMidiFile(WPARAM wparam, LPARAM lparam)
//{
//	CString szFileName;
//	AfxMessageBox((LPCTSTR)lparam);
//	szFileName = CString(reinterpret_cast<LPCTSTR> (lparam));
//	this->szFileName = szFileName;
//	MessageBox(this->szFileName);
//	szFileName.TrimLeft('"');
//	szFileName.TrimRight('"');
//	return PlayMidiFile(szFileName);
//	pPlayThead = AfxBeginThread(Play_Thread, this, 0, 0, 0, NULL);
//	SetPlayState(FALSE);
//	return 0;
//}

void CMidiPlayerDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	HDC hdc = ::GetDC(NULL);

	// Get current point
	CPoint pt = point;
	ClientToScreen(&pt);

	if(dragging)
	{
		// The following call causes a litte mess during dragging
	//	SetWindowPos(&CWnd::wndTopMost, pt.x - startPiont.x, pt.y - startPiont.y, 0, 0, SWP_NOSIZE /*| SWP_SHOWWINDOW*/);
		pt -= startPiont;
		previousPoint -= startPiont;
		CRect rect(pt.x, pt.y, pt.x+PLAYER_DLG_WIDTH-1, pt.y+PLAYER_DLG_HEIGHT-1);
		CRect lastRect(previousPoint.x, previousPoint.y, previousPoint.x+PLAYER_DLG_WIDTH-1, previousPoint.y+PLAYER_DLG_HEIGHT-1);	
		SIZE sz;
		sz.cx = 1;
		sz.cy = 1;
		CDC::FromHandle(hdc)->DrawDragRect(&rect, sz, &lastRect, sz, NULL, NULL);
	}
	::ReleaseDC(NULL, hdc);
	// Store current point for later use
	previousPoint = point;
	ClientToScreen(&previousPoint);
	CDialog::OnMouseMove(nFlags, point);
}

void CMidiPlayerDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(point.x >= 2 && point.x <= 16 && point.y >= 2 && point.y <= 24)
	{
		startPiont = point;
		ClientToScreen(&startPiont);
		previousPoint = point;
		ClientToScreen(&previousPoint);
		CRect rect;
		GetWindowRect(&rect);
		startPiont.x -= rect.left;
		startPiont.y -= rect.top;
		dragging = TRUE;
		SetCapture();
	}
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CMidiPlayerDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CPoint pt;
	pt = point;
	ClientToScreen(&pt);
	if(dragging)
	{
		SetWindowPos(&CWnd::wndTopMost, pt.x - startPiont.x, pt.y - startPiont.y, PLAYER_DLG_WIDTH, PLAYER_DLG_HEIGHT, SWP_SHOWWINDOW);
		dragging = FALSE;
		ReleaseCapture();
	}
	CDialog::OnLButtonUp(nFlags, point);
}

BOOL CMidiPlayerDlg::SetPlayState(BOOL stopped)
{
	if(stopped)
	{
		m_btnPlay.SetState(FALSE);
		m_btnStop.SetState(TRUE);
	}
	else
	{
		m_btnPlay.SetState(TRUE);
		m_btnStop.SetState(FALSE);
	}
	// Close button always up
	m_btnClose.SetState(FALSE);
	return stopped;
}

HBRUSH CMidiPlayerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	m_brBckColor.CreateSolidBrush(RGB(206, 223, 239));
	// TODO: Return a different brush if the default is not desired
	hbr = (HBRUSH)m_brBckColor;
	return hbr;
}

void CMidiPlayerDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
{
	CDialog::OnActivate(nState, pWndOther, bMinimized);
	
	// TODO: Add your message handler code here
	if(midi_IsPlaying() == 4)
	{
		SetPlayState(FALSE);
	}
	else
	{
		SetPlayState(TRUE);
	}
}

⌨️ 快捷键说明

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