' This program is copyright 1993 by Ron Wodaski.  All rights reserved.
' Please do not distribute this program.  I have uploaded it to Compuserve
'   as a teaching example, to show how to work with MCI and video in
'   Visual Basic.  A final version will be included in a new edition of
'   my book, Multimedia Madness, in the fall of 1993.
'
' This application can be used to play a video file in a child window of the
'   currently active window.

' This application can be used in two ways.
'
' Method one: drag and drop
'   Simply drag an avi file onto this program in File Manager, and the avi file
'   will play in the file manager window.
'
' Method two: using command line arguments
'   You can use command line arguments in several ways:
'      - use the File/Run menu selection in File Manager, and supply the arguments yourself
'      - Create an icon, and add the arguments after the program name
'      - Run the program from another application, and pass command line arguments
'   There are two command line formats you can use:
'      - Format 1: vidtop2 d:\path\filename
'      - Format 2: vidtop2 d:\path\filename w x y z
'          where w is the left coordinate of the playback window
'                x is the top coordinate of the playback window
'                y is the width of the playback window
'                z is the height of the playback window

' All coordinates are relative to the currently active window, since the playback
' window is created as a child of the currently active window.

' Declare the various functions we can use for mci stuff.
' Playing sounds.
Declare Function sndPlaySound Lib "mmsystem" (ByVal SoundName As String, ByVal Flags As Integer) As Integer
' Sending strings to mci.
Declare Function mciSendString Lib "mmsystem" (ByVal MCI_Command As String, ByVal ReturnString As String, ByVal ReturnLength As Integer, ByVal Handle As Integer) As Long
' Getting error messages.
Declare Function mciGetErrorString Lib "mmsystem" (ByVal MCI_Error As Long, ByVal ErrorString As String, ByVal ReturnLength As Integer) As Integer
' Sending strings to mci, no return messages.
Declare Function mciExecute Lib "mmsystem" (ByVal MCI_Command As String) As Integer
' Get handle of current window.
Declare Function GetActiveWindow Lib "User" () As Integer

Sub Command1_Click ()
    End
End Sub

Sub Form_Load ()
    Dim ReturnString As String * 1024
    Dim ErrorString As String * 1024
    Dim AVIFile As String
    Dim AVIPosition As String
    Dim Cmd As String

    wFlags% = SWP_NOMOVE Or SWP_NOSIZE
    
    ' Parse command line.
    Cmd = Command$
    ' If no command line argument, exit.
    If Len(Cmd) < 2 Then
        MsgBox "This program only works if you supply a command line argument. You must supply a filename and optional playback coordinates: vidtop d:\path\filename [100 100 160 120]     Optional argument shown in brackets."
        End
    End If
    
    Separator% = InStr(Cmd$, " ")
    ' Get filename from command line argument.
    If Separator% = 0 Then
        AVIFile = Cmd$
        ' Set default position string.
        AVIPosition = "Default"
    Else
        AVIFile = Left$(Cmd$, Separator% - 1)
        ' Get size/position information next.
        AVIPosition = Mid$(Cmd$, Separator%, 99)
    End If
    
    ' Open the window as a child of the current window.  Normally, this is the application
    '   running when this program is executed.  To refine this, you would need to pass the
    '   handle of your application window on the command line, and extract it to use it.
    Wnd% = GetActiveWindow()
    Cmd = "open " + AVIFile + " style child parent " + Str$(Wnd%) + " alias AVIFile"
    
    result% = mciSendString(Cmd, ReturnString, 1024, 0)
    ' If not opened, show error message and end program.
    If result% <> 0 Then
        result2@ = mciGetErrorString(result%, ErrorString, 1024)
        MsgBox ErrorString
        End
    End If
    
    ' Position the playback window.
    If AVIPosition = "Default" Then
        ' Play at upper left corner of parent window.
        result% = mciSendString("where AVIFile window", ReturnString, 1024, 0)
        result% = mciExecute("put AVIFile window at " + ReturnString)
    Else
        result% = mciExecute("put AVIFile window at " + AVIPosition)
    End If
    ' Uncomment following line to change default video placement within playback window.
    'result% = mciExecute("put aviFile destination at 0 0 160 120")
    ' Allow user to stop playback by pressing escape key.
    result% = mciExecute("break aviFile on 27")
    ' Realize palette (only does something when 8-bit video file involved)
    result% = mciExecute("realize aviFile normal")
    ' Play the file without interruption.
    result% = mciExecute("play AVIFile wait")
    ' Close file when done.
    result% = mciExecute("close AVIFile")
    ' End program
    End
End Sub

