' 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 popup window.
' The popup window will play on top of all other windows, since it is
' set as a Topmost window using the SetWindowPos API function.

' The 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 screen, since the playback
' window is created as a popup 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
' Put a window on top.
Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)

Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2

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 file as a popup window.
    Cmd = "open " + AVIFile + " style popup alias AVIFile"
    result% = mciSendString(Cmd, ReturnString, 1024, 0)
    
    ' To open the window as a child of your application, comment the two preceding lines
    '    and uncomment the following two lines.
    'Wnd% = GetActiveWindow()
    'result% = mciExecute("open " + AVIFile + " style child parent " + Str$(Wnd%) + " alias AVIFile")
    
    ' If not opened, show error message and end program.
    If result% <> 0 Then
        MsgBox "File not found: " + AVIFile
        End
    End If
    
    ' Get handle for playback window.
    result% = mciSendString("status AVIFile window handle", ReturnString, 1024, 0)
    ' Convert handle from string to integer.
    Wnd% = Val(ReturnString)
    ' Use handle to make playback window a topmost window.
    Call SetWindowPos(Wnd%, -1, 0, 0, 0, 0, wFlags%)
    ' Position the playback window.
    If AVIPosition = "Default" Then
        ' Do nothing; let MCI determine defaults.
    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

