?? mciapi.cpp
字號:
////////////
//mciapi.cpp : v0020
//writen by : Liu Gang(0020), Xie Shen(v0010)
//complier : Microsoft Visual C++ 4.2
//Library : winmm.lib
//v0010 : Sep.12.1995
//v0020 : Feb.9.1997
//v0021 : Aug.14.1997, changed some convensions
////////////
// implementation file
// this file contains the functions to play avi, midi wav file
// need multimeida library when link
#include "stdafx.h" // standard includings
#include "assert.h" // debug functions, error message functions
#include "mciapi.h"
BOOL MCI_bEnableAVI = TRUE;
BOOL MCI_bEnableMIDI = TRUE;
BOOL MCI_bEnableWAV = TRUE;
BOOL MCI_bEnableCDAUDIO = TRUE;
////////////
//AVI Animation functions
////////////
// open avi file
// hwnd : window for avi file to play on and receive error message
// filename : avi file name, must with full path and extension
// lpRect : display rectangle on desination window
// if NULL, display it with its origional size
// and the topleft on destination window is (0,0)
// if right and bottom is 0, the functions will use
// left and top as topleft position on destinaiton window
// 如果為空,在目的窗口的左上角按原大小播放AVI
// 如果right和bottom分量都是0,則以left和top分量為左上角原大小播放AVI
// return : -1 if failed, others to be the handle of avi file
// 失敗則返回-1,否則返回avi文件的句柄
int MCI_AVI_Open(HWND hwnd,LPSTR filename,CONST RECT *lpRect/*=NULL*/)
{
if (!MCI_bEnableAVI) return -1;
DWORD retrn;
char b[MCI_STRINGSIZE];
int videoID;
//Open avi
MCI_OPEN_PARMS mciopen;
mciopen.lpstrDeviceType="avivideo";
mciopen.lpstrElementName=filename;
if((retrn=mciSendCommand(0,MCI_OPEN,MCI_WAIT|MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciopen))!=0L)
{
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+2, b, filename);
OutputDebugString( "MCI_OpenAvi Error(2): Cannot open file:" );
OutputDebugString( filename );
OutputDebugString( "\n" );
// MCI_bEnableAVI=FALSE;
return -1;
}
videoID=mciopen.wDeviceID;
// set avi time format
MCI_SET_PARMS mciset;
mciset.dwCallback=(DWORD)hwnd;
mciset.dwTimeFormat=MCI_FORMAT_FRAMES;
if((retrn=mciSendCommand(videoID,MCI_SET,MCI_WAIT|MCI_SET_TIME_FORMAT ,(DWORD)(LPVOID)&mciset))!=0L) {
mciSendCommand(videoID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,128);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+3, b, filename);
OutputDebugString( "MCI_OpenAvi Error(3): Set time format error:" );
OutputDebugString( filename );
OutputDebugString( "\n" );
MCI_bEnableAVI=FALSE;
return FALSE;
}
//Set position and size on destination window
if( lpRect == NULL ) return videoID;
// get display rectangle on destination window
MCI_DGV_RECT_PARMS dgvRect;
dgvRect.dwCallback=(DWORD)hwnd;
if( lpRect->right == 0 && lpRect->bottom == 0 )
{
if((retrn=mciSendCommand(videoID,MCI_WHERE,
MCI_WAIT|MCI_DGV_WHERE_DESTINATION,(DWORD)(LPVOID)&dgvRect))!=0L)
{
mciSendCommand(videoID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+9, b, filename);
OutputDebugString( "MCI_OpenAvi Error(9): GetWave information error!" );
OutputDebugString( filename );
OutputDebugString( "\n" );
MCI_bEnableAVI=FALSE;
return -1;
}
}
else
{
dgvRect.rc.right = lpRect->right - lpRect->left;
dgvRect.rc.bottom = lpRect->bottom - lpRect->top;
}
dgvRect.rc.left = lpRect->left;
dgvRect.rc.top = lpRect->top;
// set rectangle
if((retrn=mciSendCommand(videoID,MCI_PUT,
MCI_WAIT|MCI_DGV_RECT|MCI_DGV_PUT_DESTINATION,(DWORD)(LPVOID)&dgvRect))!=0L)
{
mciSendCommand(videoID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+5, b, filename);
OutputDebugString( "MCI_OpenAvi Error(5): Set position error:" );
OutputDebugString( filename );
OutputDebugString( "\n" );
MCI_bEnableAVI=FALSE;
return -1;
}
//Set window
MCI_DGV_WINDOW_PARMS mwindow;
mwindow.dwCallback=(DWORD)hwnd;
mwindow.hWnd=hwnd;
if((retrn=mciSendCommand(videoID,MCI_WINDOW,MCI_WAIT|MCI_ANIM_WINDOW_HWND,(DWORD)(LPVOID)&mwindow))!=0L)
{
mciSendCommand(videoID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit( hwnd, MCI_ERROR_ID+4, b, filename);
OutputDebugString( "MCI_OpenAvi Error(4): Set window error:" );
OutputDebugString( filename );
OutputDebugString( "\n" );
MCI_bEnableAVI=FALSE;
return -1;
}
return videoID;
}
// play avi file
// hwnd : window for avi file to play on and receive error message
// videoID : handle of avi object that previously opened, if it is -1, do nothing
// dwCommand: command for play, usually be MCI_WAIT, MCI_NOTIFY, and/or MCI_MCIAVI_PLAY_FULLSCREEN
// 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
//
// 當使用MCI_WAIT參數時,函數要等AVI播放完才能返回
// 使用MCI_NOTIFY時,函數馬上返回,當播放完時,會發出一個消息MM_MCINOTIFY,
// 在響應該消息時,可以關閉該文件
// dwFrom : frame number to be played from
// dwTo : frame number to be played to, if both zero, play it from beginning to end.
// return : TRUE if played
BOOL MCI_AVI_Play(HWND hwnd,int videoID,DWORD dwCommand, DWORD dwFrom/*=0*/, DWORD dwTo/*=0*/)
{
if (!MCI_bEnableAVI) return FALSE;
if (videoID==-1) return FALSE;
DWORD retrn;
char b[MCI_STRINGSIZE];
//Play avi
MCI_DGV_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(videoID,MCI_PLAY,
dwCommand,(DWORD)(LPMCI_DGV_PLAY_PARMS)&mciplay))!=0L)
{
mciSendCommand(videoID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+6, b);
OutputDebugString( "MCI_PlayAvi Error(6): Cannot play avi!\n" );
// MCI_bEnableAVI=FALSE;
return FALSE;
}
return TRUE;
}
// seek in avi file
// hwnd : window for avi file to play on and receive error message
// videoID : handle of avi 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
BOOL MCI_AVI_Seek(HWND hwnd,int videoID,DWORD dwCommand,DWORD dwTo/*=0*/)
{
if (!MCI_bEnableAVI) return FALSE;
if (videoID==-1) return FALSE;
DWORD retrn;
char b[MCI_STRINGSIZE];
//seek avi
MCI_SEEK_PARMS mciseek;
mciseek.dwCallback=(DWORD)hwnd;
mciseek.dwTo=dwTo;
if((retrn=mciSendCommand(videoID,MCI_SEEK,dwCommand,(DWORD)(LPVOID)&mciseek))!=0L)
{
mciSendCommand(videoID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+7, b);
OutputDebugString( "MCI_SeekAvi Error(7): Seek avi error!\n" );
MCI_bEnableAVI=FALSE;
return FALSE;
}
return TRUE;
}
// stop avi file
// hwnd : window for avi file to play on and receive error message
// videoID : handle of avi object that previously opened, if it is -1, do nothing
// return : TRUE if stoped the avi
BOOL MCI_AVI_Stop(HWND hwnd,int videoID)
{
if (!MCI_bEnableAVI) return FALSE;
if (videoID==-1) return FALSE;
DWORD retrn;
char b[MCI_STRINGSIZE];
//Stop avi
MCI_GENERIC_PARMS mcigen;
mcigen.dwCallback=(DWORD)hwnd;
if((retrn=mciSendCommand(videoID,MCI_STOP,MCI_WAIT,(DWORD)(LPVOID)&mcigen))!=0L)
{
mciSendCommand(videoID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+8, b);
OutputDebugString( "MCI_StopAvi Error(8): Stop avi error!\n" );
MCI_bEnableAVI=FALSE;
return FALSE;
}
return TRUE;
}
// pause avi file
// hwnd : window for avi file to play on and receive error message
// videoID : handle of avi object that previously opened, if it is -1, do nothing
// return : TRUE if stoped the avi
BOOL MCI_AVI_Pause(HWND hwnd,int videoID)
{
if (!MCI_bEnableAVI) return FALSE;
DWORD retrn;
char b[MCI_STRINGSIZE];
//Stop avi
MCI_GENERIC_PARMS mcigen;
mcigen.dwCallback=(DWORD)hwnd;
if((retrn=mciSendCommand(videoID,MCI_PAUSE,MCI_WAIT,(DWORD)(LPVOID)&mcigen))!=0L)
{
mciSendCommand(videoID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+8, b);
OutputDebugString( "MCI_PauseAvi Error(15): Pause avi error!\n" );
MCI_bEnableAVI=FALSE;
return FALSE;
}
return TRUE;
}
// close avi file
// videoID : handle of avi object that previously opened, if it is -1, do nothing
// return : TRUE if closed the file
BOOL MCI_AVI_Close(int videoID)
{
if (!MCI_bEnableAVI) return FALSE;
if (videoID==-1) return FALSE;
//Close avi
mciSendCommand(videoID,MCI_CLOSE,MCI_WAIT,NULL);
return TRUE;
}
////////////
////////////
// MIDI music functions
////////////
// open midi file
// hwnd : window for midi file to play on and receive error message
// filename : midi file name, must with full path and extension
// return : -1 if failed, others to be the handle of midi file
// 失敗則返回-1,否則返回midi文件的句柄
int MCI_MIDI_Open(HWND hwnd,LPSTR filename)
{
if( !MCI_bEnableMIDI ) return -1;
char b[MCI_STRINGSIZE];
DWORD retrn;
int midiID=-1;
// open midi
MCI_DGV_OPEN_PARMS mciOpen;
mciOpen.wDeviceID=NULL;
mciOpen.lpstrDeviceType="sequencer";
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+10, b, filename);
OutputDebugString( "MCI_OpenMIDI Error(10): Cannot open file:" );
OutputDebugString( filename );
OutputDebugString( "\n" );
MCI_bEnableMIDI = FALSE;
return -1;
}
midiID=mciOpen.wDeviceID;
return midiID;
}
// play 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
// 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參數時,函數要等MIDI播放完才能返回
// 使用MM_NOTIFY時,函數馬上返回,當播放完時,會發出一個消息MM_MCINOTIFY,
// 在響應該消息時,可以關閉該文件。
// 注意,當使用MCI_MIDI_Stop()函數終止播放時,也會發出該消息
// return : TRUE if played the file
BOOL MCI_MIDI_Play(HWND hwnd,int midiID,DWORD dwCommand)
{
if( !MCI_bEnableMIDI ) return FALSE;
if( midiID == -1 ) return FALSE;
DWORD retrn;
char b[MCI_STRINGSIZE];
//Play midi
MCI_PLAY_PARMS mciplay;
mciplay.dwCallback=(DWORD)hwnd;
if((retrn=mciSendCommand(midiID,MCI_PLAY,dwCommand,(DWORD)(LPVOID)&mciplay))!=0L)
{
mciSendCommand(midiID,MCI_CLOSE,0,NULL);
mciGetErrorString(retrn,(LPSTR)b,MCI_STRINGSIZE-1);
ErrorMessageNoQuit(hwnd, MCI_ERROR_ID+11, b);
OutputDebugString( "MCI_PlayMIDI Error(11): cannot play midi!\n" );
MCI_bEnableMIDI = FALSE;
return FALSE;
}
return TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -