Option Explicit On
Option Strict Off



Friend Class frmVideo

    Inherits System.Windows.Forms.Form

    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
    Private Declare Ansi Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
    Private Const SND_ASYNC As Short = &H1S

    Private Sub cmdRec_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRec.Click
        'Dim Index As Short = cmdRec.GetIndex(eventsender)
        cmdStop.Visible = True
        cmdStop.Enabled = True
        cmdRec.Visible = False
        ezVidCap1.CaptureFile = My.Application.Info.DirectoryPath & "\capture.avi"
        ezVidCap1.CaptureVideo()
        cmdRec.Visible = True
        cmdStop.Visible = False
    End Sub

    Private Sub cmdStop_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdStop.Click
        'Dim Index As Short = cmdStop.GetIndex(eventSender)
        ezVidCap1.CaptureEnd()
    End Sub

    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim FileName As String
        Dim rp As Integer = ezVidCap1.TimeLimit * 1000
        picCap.Visible = True
        ezVidCap1.Visible = False
        FileName = Dir(My.Application.Info.DirectoryPath & "\capture.avi")
        SendString("open " & FileName)
        SendString("window " & FileName & " handle " & picCap.Handle.ToInt32)
        SendString("play " & FileName)
        System.Threading.Thread.Sleep(rp + 1000)
        SendString("close " & FileName)
        picCap.Visible = False
        ezVidCap1.Visible = True
    End Sub

    Private Sub btnSaveDIB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveDIB.Click
        ezVidCap1.SaveDIB(My.Application.Info.DirectoryPath & "\capture.bmp")
        Call PlaySound(My.Application.Info.DirectoryPath & "\camera_click.wav", 0, SND_ASYNC)
        If Err.Number Then
            MsgBox(Err.Description, MsgBoxStyle.Information, My.Application.Info.Title)
        End If
        picCap.Visible = True
        picCap.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\capture.bmp")
        System.Threading.Thread.Sleep(1000)
        picCap.Visible = False
        With picCap
            If Not (.Image Is Nothing) Then
                .Image.Dispose()
                .Image = Nothing
            End If
        End With
    End Sub

    Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
        Dim MyFile As String
        MyFile = Dir(My.Application.Info.DirectoryPath & "\capture.bmp")
        If Len(MyFile) > 1 Then
            btnView.Visible = False
            btnBack.Visible = True
            picCap.Visible = True
            ezVidCap1.Visible = False
            cmdRec.Enabled = False
            btnPlay.Enabled = False
            btnSaveDIB.Enabled = False
            picCap.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\capture.bmp")
            btnBack.Focus()
        Else
            MsgBox("File not found")
            Exit Sub
        End If
    End Sub

    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        picCap.Visible = False
        With picCap
            If Not (.Image Is Nothing) Then
                .Image.Dispose()
                .Image = Nothing
            End If
        End With
        ezVidCap1.Visible = True
        btnView.Visible = True
        btnBack.Visible = False
        cmdRec.Enabled = True
        btnPlay.Enabled = True
        btnSaveDIB.Enabled = True
        btnSaveDIB.Focus()
    End Sub

    Public Function SendString(ByVal cmdString As String) As Long

        SendString = mciSendString(cmdString, CStr(VariantType.Null), 0, 0)

    End Function

    Private Sub frmVideo_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.chkVideo.Checked = False
    End Sub

    Private Sub frmVideo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Left = Form1.Left + 234
        Me.Top = Form1.Top
    End Sub
End Class