在VISUALBAISC5.0中可以使用播放視頻動畫文件(*.AVI)的方法在我們的軟件中加入動畫效果,但在軟件中如何實現視頻畫中畫的效果呢?經筆者摸索,發現利用WIDOWS95的API函數mciExecute、mciSendString和SetWindowPos可實現這一效果。
下面筆者以最常用的大畫面中疊加小畫面為例來說明視頻畫中畫的實現方法。
一、建立窗體。進入VISUALBAISC5.0系統環境,建立一個新的窗體FORM1。
二、建立視頻窗口。在這裡筆者使用了一大一小的兩個圖畫框(PictureBox)作為視頻窗口。選擇控件工具箱中的PictureBox控件,在窗體中拉出兩個方框(Picture1和Picture2)。其中Picture1為大畫面,播放AVI01.AVI文件;Picture2為小畫面,播放AVI02.AVI文件。
三、編寫相應控件的腳本。
通用模塊:
DeclareFunctionmciExecuteLib"winmm.dll"(ByVallpstrCommandAsString)AsLong
DeclareFunctionmciSendStringLib"winmm.dll"Alias"mciSendStringA"(ByVallpstrCommandAsString,ByVallpstrReturnStringAsString,ByValuReturnLengthAsLong,ByValhwndCallbackAsLong)AsLong
DeclareFunctionSetWindowPosLib"user32"(ByValhwndAsLong,ByValhWndInsertAfterAsLong,ByValxAsLong,ByValyAsLong,ByValcxAsLong,ByValcyAsLong,ByValwFlagsAsLong)AsLong
FORM1的ACTIVATE過程:
DimAAAsLong
DimPlayAviAsLong
DimtmpValAsString
DimKeyValSizeAsLong
OnErrorResumeNext
tmpVal=String$(1024,0)
KeyValSize=1024
PlayAvi=mciSendString("STATUSAVI01READY" Str(Picture2.hwnd),tmpVal,KeyValSize,0)
IfPlayAvi<>263Then
PlayAvi=mciExecute("closeAVI01")
EndIf
tmpVal=String$(1024,0)
KeyValSize=1024
PlayAvi=mciSendString("OPENAVI01.AVIALIASAVI01TYPEAVIVIDEOSTYLECHILDPARENT" Str(Picture2.hwnd) "WAIT",tmpVal,KeyValSize,0)
x1Pos=Picture1.Width
y1Pos=Picture1.Height
tmpVal=String$(1024,0)
KeyValSize=1024
AA=mciSendString("STATUSAVI01WINDOWHANDLEWAIT",tmpVal,KeyValSize,0)
If(Asc(Mid(tmpVal,KeyValSize,1))=0)Then
tmpVal=Left(tmpVal,KeyValSize-1)
Else
tmpVal=Left(tmpVal,KeyValSize)
EndIf
AA=SetWindowPos(tmpVal,0,0,0,x1Pos,y1Pos,0)
AA=mciExecute("WINDOWAVI01STATESHOW")
tmpVal=String$(1024,0)
KeyValSize=1024
PlayAvi=mciSendString("STATUSAVI02READY" Str(Picture1.hwnd),tmpVal,KeyValSize,0)
IfPlayAvi<>263Then
PlayAvi=mciExecute("CLOSEAVI02")
EndIf
tmpVal=String$(1024,0)
KeyValSize=1024
PlayAvi=mciSendString("OPENAVI02.AVIALIASAVI02TYPEAVIVIDEOSTYLECHILDPARENT" Str(Picture1.hwnd) "WAIT",tmpVal,KeyValSize,0)
x1Pos=Picture2.Width
y1Pos=Picture2.Height
tmpVal=String$(1024,0)
KeyValSize=1024
AA=mciSendString("STATUSAVI02WINDOWHANDLEWAIT",tmpVal,KeyValSize,0)
If(Asc(Mid(tmpVal,KeyValSize,1))=0)Then
tmpVal=Left(tmpVal,KeyValSize-1)
Else
tmpVal=Left(tmpVal,KeyValSize)
EndIf
AA=SetWindowPos(tmpVal,0,0,0,x1Pos,y1Pos,0)
MciExecute"WINDOWAVI02STATESHOW"
MciExecute"PLAYAVI01REPEAT"
MciExecute"PLAYAVI02REPEAT"
OnErrorGoTo0
FORM1的QueryUnload過程:
mciExecute"STOPAVI01"
mciExecute"STOPAVI02"
mciExecute"CLOSEAVI01"
mciExecute"CLOSEAVI02"
運行此程序可見兩個動畫同時播放,實現了視頻畫中畫的效果。以上程序在VISUALBAISC5.0及WINDOWS95中運行通過。->