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

📄 mciapi.cpp

📁 赤壁之战(游戏原码)
💻 CPP
📖 第 1 页 / 共 2 页
字号:

// seek midi file, not done yet
// hwnd		:	window for midi file to play on and receive error message
// midiID	:	handle of midi object that previously opened, if it is -1, do nothing
// dwCommand:	command for seek, usually be MCI_WAIT, MCI_SEEK_TO_START
// return	:	TRUE if seeked the file
BOOL MCI_MIDI_Seek(HWND hwnd,int midiID,DWORD dwCommand)
{
	if( !MCI_bEnableMIDI ) return FALSE;
	
	return FALSE;
}

// stop midi file
// hwnd		:	window for midi file to play on and receive error message
// midiID	:	handle of midi object that previously opened, if it is -1, do nothing
// return	:	TRUE if stoped the file
BOOL MCI_MIDI_Stop(HWND hwnd,int midiID)
{
	if( !MCI_bEnableMIDI ) return FALSE;
	if( midiID == -1 )	return FALSE;

	DWORD retrn;
	char b[MCI_STRINGSIZE];

	//Stop midi
	MCI_GENERIC_PARMS mcigen;
	mcigen.dwCallback=(DWORD)hwnd;
	if((retrn=mciSendCommand(midiID,MCI_STOP,MCI_WAIT,(DWORD)(LPVOID)&mcigen))!=0L)
	{
		mciSendCommand(midiID,MCI_CLOSE,0,NULL);
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
		ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+12, b);
		OutputDebugString( "MCI_StopMIDI Error(12): Cannot stop midi or cannot find midi!\n" );
		MCI_bEnableMIDI = FALSE;
		return FALSE;
	}
	return TRUE;
}

// close midi file
// midiID	:	handle of midi object that previously opened, if it is -1, do nothing
// return	:	TRUE if closed the file
BOOL MCI_MIDI_Close(int midiID)
{
	if( !MCI_bEnableMIDI ) return FALSE;
	if( midiID == -1 )	return FALSE;

	// close midi
	mciSendCommand(midiID,MCI_CLOSE,MCI_WAIT,NULL);
	return TRUE;
}
////////////

////////////
// WAV music functions
////////////
// open wave file
// hwnd		:	window for wave file to play on and receive error message
// filename	:	wave file name, must with full path and extension
// return	:	-1 if failed, others to be the handle of wav file
//				失败则返回-1,否则返回wave文件的句柄
int MCI_WAV_Open(HWND hwnd,LPSTR filename)
{
	if (!MCI_bEnableWAV) return -1;

	char b[MCI_STRINGSIZE];
	DWORD retrn;	
	int waveID;

	// open wave
	MCI_OPEN_PARMS mciOpen;
	mciOpen.wDeviceID=NULL;
	mciOpen.lpstrDeviceType="waveaudio";
	mciOpen.lpstrElementName=filename;
	if((retrn=mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpen))!=0L)
	{
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
		ErrorMessageNoQuit( hwnd, MCI_ERROR_ID+20, b, filename);
		OutputDebugString( "MCI_OpenWav Error(20): Cannot open wave file: " );
		OutputDebugString( filename );
		OutputDebugString( "\n" );
		MCI_bEnableWAV=FALSE;
		return -1;
	}
	waveID=mciOpen.wDeviceID;
	return waveID;
}

// play wave file
// hwnd		:	window for wave file to play on and receive error message
// waveID	:	handle of wave object that previously opened, if it is -1, do nothing
// dwCommand:	command for play, usually be MCI_WAIT, MCI_NOTIFY
//				when using MCI_WAIT, the function will not returned until end playing.
//				when using MCI_NOTIFY, the function returned at once, when finished playing the 
//              object will send a MM_MCINOTIFY message to the window,
//				you can close it on receiving this message
//				当使用MM_WAIT参数时,函数要等WAV播放完才能返回
//				使用MM_NOTIFY时,函数马上返回,当播放完时,会发出一个消息MM_MCINOTIFY,
//				在响应该消息时,可以关闭该文件。
//				注意,当使用MCI_WAV_Stop()函数终止播放时,也会发出该消息
// return	:	TRUE if played the file
BOOL MCI_WAV_Play(HWND hwnd,int waveID,DWORD dwCommand)
{
	if (!MCI_bEnableWAV) return FALSE;
	if (waveID==-1) return FALSE;

	DWORD retrn;
	char b[MCI_STRINGSIZE];

	//Play wave
	MCI_PLAY_PARMS mciplay;
 	mciplay.dwCallback=(DWORD)hwnd;
	if((retrn=mciSendCommand(waveID,MCI_PLAY,dwCommand,(DWORD)(LPVOID)&mciplay))!=0L)	{
		mciSendCommand(waveID,MCI_CLOSE,0,NULL);
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
		ErrorMessageNoQuit( hwnd, MCI_ERROR_ID+21, b);
		OutputDebugString( "MCI_PlayWav Error(21): cannot play wave!\n" );
		MCI_bEnableWAV=FALSE;
		return FALSE;
	}
	return TRUE;
}

// seek wave file
// hwnd		:	window for wave file to play on and receive error message
// waveID	:	handle of wave object that previously opened, if it is -1, do nothing
// dwCommand:	command for seek, usually be MCI_WAIT, MCI_SEEK_TO_START
// return	:	TRUE if seeked the file
BOOL MCI_WAV_Seek(HWND hwnd,int waveID,DWORD dwCommand)
{
	if (!MCI_bEnableWAV) return FALSE;
	if (waveID==-1) return FALSE;

	DWORD retrn;
	char b[MCI_STRINGSIZE];

	//seek wav
	MCI_SEEK_PARMS mciseek;
 	mciseek.dwCallback=(DWORD)hwnd;
	if((retrn=mciSendCommand(waveID,MCI_SEEK,dwCommand,(DWORD)(LPVOID)&mciseek))!=0L)	{
		mciSendCommand(waveID,MCI_CLOSE,0,NULL);
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
		ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+22, b);
		OutputDebugString( "MCI_SeekWav Error(22):	Cannot send seek command!\n" );
		MCI_bEnableWAV=FALSE;
		return FALSE;
	}
	return TRUE;
}

// stop wave file
// hwnd		:	window for wave file to play on and receive error message
// waveID	:	handle of wave object that previously opened, if it is -1, do nothing
// return	:	TRUE if stoped the file
BOOL MCI_WAV_Stop(HWND hwnd,int waveID)
{
	if (!MCI_bEnableWAV) return FALSE;
	if (waveID==-1) return FALSE;

	DWORD retrn;
	char b[MCI_STRINGSIZE];

	//Stop wave
	MCI_GENERIC_PARMS mcigen;
	mcigen.dwCallback=(DWORD)hwnd;
	if((retrn=mciSendCommand(waveID,MCI_STOP,MCI_WAIT,(DWORD)(LPVOID)&mcigen))!=0L)	{
		mciSendCommand(waveID,MCI_CLOSE,0,NULL);
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
		ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+23, b);
		OutputDebugString( "MCI_StopWav Error(23): cannot stop wave!\n" );
		MCI_bEnableWAV=FALSE;
		return FALSE;
	}
	return TRUE;
}

// close wave file
// waveID	:	handle of wave object that previously opened, if it is -1, do nothing
// return	:	TRUE if closed the file
BOOL MCI_WAV_Close(int waveID)
{
	if (!MCI_bEnableWAV) return FALSE;
	if (waveID==-1) return FALSE;

	// close wave
	mciSendCommand(waveID,MCI_CLOSE,MCI_WAIT,NULL);
	return TRUE;
}
////////////

////////////
// open CDAudio by track
// hwnd		:	window for cdaudio file to play on and receive error message
// nTrack	:	Sound Track to play
// return	:	-1 if failed, others to be the handle of cdaudio file
//				失败则返回-1,否则返回cdaudio文件的句柄
int MCI_CDAUDIO_Open(HWND hwnd)
{
	if (!MCI_bEnableCDAUDIO) return -1;

	char b[MCI_STRINGSIZE];
	DWORD retrn;	
	int cdaudioID;

	// open cdaudio
	MCI_OPEN_PARMS mciOpen;
	mciOpen.wDeviceID=NULL;
	mciOpen.lpstrDeviceType="cdaudio";
	//mciOpen.lpstrElementName=filename;
	if((retrn=mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE,(DWORD)(LPVOID)&mciOpen))!=0L)
	{
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
//		ErrorMessageNoQuit( hwnd, MCI_ERROR_ID+30, b);
		OutputDebugString( "MCI_OpenCD Error(30): Cannot read CD Audio data!\n" );
//		MCI_bEnableCDAUDIO=FALSE;
		return -1;
	}
	cdaudioID=mciOpen.wDeviceID;

	// set cd audio time format
	MCI_SET_PARMS mciSet;
 	mciSet.dwCallback=(DWORD)hwnd;
	mciSet.dwTimeFormat = MCI_FORMAT_TMSF;
	if((retrn=mciSendCommand(cdaudioID,MCI_SET,MCI_SET_TIME_FORMAT,(DWORD)(LPVOID)&mciSet))!=0L)
	{
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
//		ErrorMessageNoQuit( hwnd, MCI_ERROR_ID+34, b);
		OutputDebugString( "MCI_OpenCD Error(34): Set time format error!\n" );
		MCI_bEnableCDAUDIO=FALSE;
		return -1;
	}

	return cdaudioID;
}

// play cdaudio file
// hwnd		:	window for cdaudio file to play on and receive error message
// cdaudioID	:	handle of cdaudio object that previously opened, if it is -1, do nothing
// dwCommand:	command for play, usually be MCI_WAIT, MCI_NOTIFY
//				when using MCI_WAIT, the function will not returned until end playing.
//				when using MCI_NOTIFY, the function returned at once, when finished playing the 
//              object will send a MM_MCINOTIFY message to the window,
//				you can close it on receiving this message
//				当使用MM_WAIT参数时,函数要等CDAudio播放完才能返回
//				使用MM_NOTIFY时,函数马上返回,当播放完时,会发出一个消息MM_MCINOTIFY,
//				在响应该消息时,可以关闭该文件。
//				注意,当使用MCI_CDAUDIO_Stop()函数终止播放时,也会发出该消息
// dwFrom	:	track number to be played from, 1 if the first track
// dwTo		:	track number to be played to, if both zero, play it from beginning to end.
// return	:	TRUE if played the file
BOOL MCI_CDAUDIO_Play(HWND hwnd,int cdaudioID,DWORD dwCommand/*=MCI_NOTIFY*/, DWORD dwFrom/* = 0*/, DWORD dwTo/* = 0*/ )
{
	if (!MCI_bEnableCDAUDIO) return FALSE;
	if (cdaudioID==-1) return FALSE;

	DWORD retrn;
	char b[MCI_STRINGSIZE];

	//Play cdaudio
	MCI_PLAY_PARMS mciplay;
 	mciplay.dwCallback=(DWORD)hwnd;
	if(dwFrom!=0)	{mciplay.dwFrom=dwFrom;dwCommand|=MCI_FROM;}
	if(dwTo!=0)		{mciplay.dwTo=dwTo;dwCommand|=MCI_TO;}
	if((retrn=mciSendCommand(cdaudioID,MCI_PLAY,dwCommand,(DWORD)(LPVOID)&mciplay))!=0L)	{
		mciSendCommand(cdaudioID,MCI_CLOSE,0,NULL);
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
//		ErrorMessageNoQuit( hwnd, MCI_ERROR_ID+31, b);
		OutputDebugString( "MCI_PlayCD Error(31): cannot play cd audio!\n" );
//		MCI_bEnableCDAUDIO=FALSE;
		return FALSE;
	}
	return TRUE;
}

// seek cdaudio file
// hwnd		:	window for cdaudio file to play on and receive error message
// cdaudioID	:	handle of cdaudio object that previously opened, if it is -1, do nothing
// dwCommand:	command for seek, usually be MCI_WAIT, MCI_SEEK_TO_START
// nTrack	:	Track number to seek
// return	:	TRUE if seeked the file
BOOL MCI_CDAUDIO_Seek(HWND hwnd,int cdaudioID, int nTrack, DWORD dwCommand/*=MCI_WAIT|MCI_SEEK_TO_START*/)
{
	if (!MCI_bEnableCDAUDIO) return FALSE;
	if (cdaudioID==-1) return FALSE;

	DWORD retrn;
	char b[MCI_STRINGSIZE];

	//seek wav
	MCI_SEEK_PARMS mciseek;
 	mciseek.dwCallback=(DWORD)hwnd;
	mciseek.dwTo = nTrack;
	if((retrn=mciSendCommand(cdaudioID,MCI_SEEK,dwCommand,(DWORD)(LPVOID)&mciseek))!=0L)	{
		mciSendCommand(cdaudioID,MCI_CLOSE,0,NULL);
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
//		ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+32, b);
		OutputDebugString( "MCI_SeekCD Error(32):	Cannot send seek command!\n" );
		MCI_bEnableCDAUDIO=FALSE;
		return FALSE;
	}
	return TRUE;
}

// stop cdaudio file
// hwnd		:	window for cdaudio file to play on and receive error message
// cdaudioID	:	handle of cdaudio object that previously opened, if it is -1, do nothing
// return	:	TRUE if stoped the file
BOOL MCI_CDAUDIO_Stop(HWND hwnd,int cdaudioID)
{
	if (!MCI_bEnableCDAUDIO) return FALSE;
	if (cdaudioID==-1) return FALSE;

	DWORD retrn;
	char b[MCI_STRINGSIZE];

	//Stop cdaudio
	MCI_GENERIC_PARMS mcigen;
	mcigen.dwCallback=(DWORD)hwnd;
	if((retrn=mciSendCommand(cdaudioID,MCI_STOP,MCI_WAIT,(DWORD)(LPVOID)&mcigen))!=0L)	{
		mciSendCommand(cdaudioID,MCI_CLOSE,0,NULL);
		mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
//		ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+33, b);
		OutputDebugString( "MCI_StopCD Error(33): cannot stop cd audio!\n" );
		MCI_bEnableCDAUDIO=FALSE;
		return FALSE;
	}
	return TRUE;
}

// close cdaudio file
// cdaudioID	:	handle of cdaudio object that previously opened, if it is -1, do nothing
// return	:	TRUE if closed the file
BOOL MCI_CDAUDIO_Close(int cdaudioID)
{
	if (!MCI_bEnableCDAUDIO) return FALSE;
	if (cdaudioID==-1) return FALSE;

	// close cdaudio
	mciSendCommand(cdaudioID,MCI_CLOSE,MCI_WAIT,NULL);
	return TRUE;
}
////////////

⌨️ 快捷键说明

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