Click here to Skip to main content
14,033,835 members
Click here to Skip to main content

Stats

748.4K views
30.2K downloads
281 bookmarked
Posted 14 Sep 2010
Licenced CPOL

PVS.AVPlayer - MCI Audio and Video Library

, 7 Aug 2018
Windows Media Control Interface (MCI) library with many added features
PVS.AVPlayer
PVS.AVPlayer .NET 2.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 3.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 3.5
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.5
PVS.AVPlayer .NET 4.5.1
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.5.2
PVS.AVPlayer.XML
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6.1
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6.2
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.7
PVS.AVPlayer .NET 4.7.1
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.7.2
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer All Source Code
AVPlayerExample
AVPlayerExample
AVPlayerExample.csproj.user
bin
Debug
PVS.AVPlayer.XML
Release
Dialogs
Display Overlays
obj
Debug
Release
x86
Debug
Release
Properties
Resources
Crystal Italic1.ttf
WingDings3a.ttf
Voice Recorder
FolderView
FolderView
bin
Debug
PVS.AVPlayer.XML
Release
FolderView.csproj.user
obj
Release
x86
Debug
Release
Properties
Resources
Crystal Italic1.ttf
PVS.AVPlayer
AVPlayerExample.csproj.user
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Custom Items
Native Methods
Bob.png
Crystal Italic1.ttf
Dial Green 2.png
Dial Green 4.png
Dial Green.png
Dial Red 2.png
Dial Red.png
media7.ico
media7a.ico
Media8.ico
Media8a.ico
VU Meter.png
WingDings3a.ttf
Sound Recorder
Various
About Dialog
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Custom Items
FolderView.csproj.user
Debug
Bob.png
Crystal Italic1.ttf
media7a.ico
media7b.ico
Media8a.ico
Media8b.ico
Subtitles Overlay
Various
How To (C#)
PVSAVPlayerHowTo
bin
Debug
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Release
obj
Debug
Release
Properties
How To (VB.NET)
PVSAVPlayerHowToVB
bin
Debug
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Release
My Project
Application.myapp
obj
Debug
Release
PVSAVPlayerHowTo.vbproj.user
PVS.AVPlayer Examples
AVPlayerExample.ex_
FolderView.ex_
AVPlayerExample.exe
FolderView.exe
PVS.AVPlayer.dll
#region Usings

using PVS.AVPlayer;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

#endregion

namespace AVPlayerExample
{
    public partial class BouncingOverlay : Form, IOverlay
    {
        /*
        
        PVS.AVPlayer Display Overlay - Example 'Bouncing'
        Displays simple 'bouncing' transparent shapes.
        
        */

        // ******************************** Fields

        #region Fields

        // Structure to hold a shape's data
        private struct Shape
        {
            public int          XPos;
            public int          YPos;
            public int          XDelta;
            public int          YDelta;
            public Rectangle    Rect;
        }

        // Enumeration shapes form
        private enum ShapeForm
        {
            Round,
            Square,
            Diamond,
            Star
        }

        private const int   BASE_XDELTA = 8; // the x movement (speed) of the first shape (in pixels)
        private const int   BASE_YDELTA = 10; // the y movement (speed) of the first shape (in pixels)
        private const int   DIFF_XDELTA = 1; // the x speed added to every next shape
        private const int   DIFF_YDELTA = 1; // the y speed added to every next shape

        private const int   TIMER_INTERVAL = 25;

        private bool        _isDrawing; // indicates shapes are being drawn

        private Shape[]     _shapes; // the shapes array
        private int         _shapeCount = 3; // the number of shapes

        private ShapeForm   _shapeForm = ShapeForm.Round; // the shape form
        private Point[]     _diamondPoints = new Point[4]; // used with drawing Diamond shape
        private Point[]     _starPoints = new Point[8]; // used with drawing Star shape

        private int         _shapeSize = 3; // the size of the shapes (overlay width or height divided by this)
        private bool        _shapeSizeCount; // set to true if shapeSize is number of shapes

        private bool        _moveShapes; // used to move shapes only by timer tick event (not with resize)

        private SolidBrush  _brush1; // used to draw shapes
        private HatchBrush  _brush2; // used to draw shapes
        private HatchBrush  _brush3; // used to draw shapes
        private HatchBrush  _brush4; // used to draw shapes
        private bool        _backTransparent; // indicates what is transparent, shape or background

        private Timer       _timer1;

        // Interacting with the player
        private Player      _basePlayer;

        private bool        _disposed;

        #endregion

        // ******************************** Initialize & Form event handling

        #region Initialize & Form event handling

        // Initialize
        public BouncingOverlay(Player thePlayer)
        {
            InitializeComponent();

            // Reduce 'flickering' of the bubbles
            DoubleBuffered = true;

            // Set numericUpDown1 control
            numericUpDown1.Value = _shapeCount;
            numericUpDown1.Focus();
            numericUpDown1.Select(0, 5);

            // Create and initialize the bubbles
            ShapesInit();

            // Set brush to draw transparant bubbles
            _brush1 = new SolidBrush(TransparencyKey);
            _brush2 = new HatchBrush(HatchStyle.HorizontalBrick, Color.Red, TransparencyKey);
            _brush3 = new HatchBrush(HatchStyle.OutlinedDiamond, Color.White, TransparencyKey);
            _brush4 = new HatchBrush(HatchStyle.Percent50, Color.Gold, TransparencyKey);

            // Create a timer
            _timer1 = new Timer();
            _timer1.Tick += timer1_Tick;
            _timer1.Interval = TIMER_INTERVAL;

            // Interact with the (base) player
            _basePlayer = thePlayer;
            _basePlayer.MediaPaused += basePlayer_MediaPausedResumed;
            _basePlayer.MediaResumed += basePlayer_MediaPausedResumed;
            // If paused, insure proper redraw
            ResizeRedraw = true;
        }

        // Handles pause/resume events from the main (base) player
        void basePlayer_MediaPausedResumed(object sender, EventArgs e)
        {
            if (_basePlayer.Paused) _timer1.Stop();
            else if (Visible) _timer1.Start();
        }

        // Gets called when the overlay is shown or hidden
        private void Form1_VisibleChanged(object sender, EventArgs e)
        {
            // No need for a running timer when the overlay is not visible
            if (Visible)
            {
                if (!_basePlayer.Paused) _timer1.Start();
            }
            else _timer1.Stop();
        }

        // Create and initialize 'shapesCount' bubbles
        private void ShapesInit()
        {
            int xDelta = BASE_XDELTA;
            int yDelta = BASE_YDELTA;

            _shapes = new Shape[_shapeCount];

            for (int i = 0; i < _shapes.Length; i++)
            {
                _shapes[i].XPos = 0;
                _shapes[i].YPos = 0;
                _shapes[i].XDelta = xDelta;
                _shapes[i].YDelta = yDelta;

                // This makes the shapes move in (slightly) different ways:
                xDelta += DIFF_XDELTA;
                yDelta += DIFF_YDELTA;
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _timer1.Dispose(); _timer1 = null;

                    _brush1.Dispose(); _brush1 = null;
                    _brush2.Dispose(); _brush2 = null;
                    _brush3.Dispose(); _brush3 = null;
                    _brush4.Dispose(); _brush4 = null;

                    _basePlayer.MediaPaused -= basePlayer_MediaPausedResumed;
                    _basePlayer.MediaResumed -= basePlayer_MediaPausedResumed;

                    _basePlayer = null;

                    if (components != null) components.Dispose();
                }
                base.Dispose(disposing);
                _disposed = true;
            }
        }

        #endregion

        // ******************************** IOverlay Control

        #region IOverlay Control

        // The visibility of the menu is controlled by the user from the main application (in this example application)
        public bool MenuEnabled
        {
            get { return bouncingPanel.Visible; }
            set { bouncingPanel.Visible = value; }
        }

        public bool HasMenu
        {
            get { return true; }
        }

        public void MediaStopped()
        {
            // not used with this overlay
        }

        #endregion

        // ******************************** Timer tick event handling

        #region Timer tick event

        void timer1_Tick(object sender, EventArgs e)
        {
            _moveShapes = true;
            Refresh(); // Move and draw the shapes (Form1_Paint)
            _moveShapes = false;
        }

        #endregion

        // ******************************** Move and/or draw the shapes

        #region Paint Shapes

        // Paint the shapes
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (!_isDrawing) // don't draw shapes if already drawing
            {
                _isDrawing = true;

                int theWidth = ClientRectangle.Width;
                int theHeight = ClientRectangle.Height;
                int theSize = theWidth > theHeight ? theHeight : theWidth;
                theSize /= _shapeSize;

                for (int i = 0; i < _shapes.Length; i++)
                {
                    // Only move the shapes from the timer tick event (not when overlay is resized)...
                    if (_moveShapes)
                    {
                        _shapes[i].XPos += _shapes[i].XDelta;
                        if (_shapes[i].XPos <= 0)
                        {
                            _shapes[i].XPos = 0;
                            _shapes[i].XDelta = -_shapes[i].XDelta;
                        }
                        else if (_shapes[i].XPos + theSize > theWidth)
                        {
                            _shapes[i].XPos = theWidth - theSize;
                            _shapes[i].XDelta = -_shapes[i].XDelta;
                        }

                        _shapes[i].YPos += _shapes[i].YDelta;
                        if (_shapes[i].YPos <= 0)
                        {
                            _shapes[i].YPos = 0;
                            _shapes[i].YDelta = -_shapes[i].YDelta;
                        }
                        else if (_shapes[i].YPos + theSize >= theHeight)
                        {
                            _shapes[i].YPos = theHeight - theSize;
                            _shapes[i].YDelta = -_shapes[i].YDelta;
                        }
                    }

                    // ... but also redraw the shapes after resizing the overlay
                    _shapes[i].Rect = new Rectangle(_shapes[i].XPos, _shapes[i].YPos, theSize, theSize);

                    // Drawing shapes, but could also move around controls like panels and pictureboxes
                    // Draw the transparant shape
                    switch (_shapeForm)
                    {
                        case ShapeForm.Round:
                            e.Graphics.FillEllipse(_brush1, _shapes[i].Rect);
                            break;

                        case ShapeForm.Square:
                            e.Graphics.FillRectangle(_brush2, _shapes[i].Rect);
                            break;

                        case ShapeForm.Diamond:
                            // Should do the some calculations elsewhere for efficiency/speed
                            _diamondPoints[0] = new Point(_shapes[i].XPos + (_shapes[i].Rect.Width / 2), _shapes[i].YPos);
                            _diamondPoints[1] = new Point(_shapes[i].XPos + _shapes[i].Rect.Width, _shapes[i].YPos + (_shapes[i].Rect.Height / 2));
                            _diamondPoints[2] = new Point(_shapes[i].XPos + (_shapes[i].Rect.Width / 2), _shapes[i].YPos + _shapes[i].Rect.Height);
                            _diamondPoints[3] = new Point(_shapes[i].XPos, _shapes[i].YPos + (_shapes[i].Rect.Height / 2));
                            e.Graphics.FillPolygon(_brush3, _diamondPoints);
                            break;

                        case ShapeForm.Star:
                            // Should do the some calculations elsewhere for efficiency/speed
                            _starPoints[0] = new Point(_shapes[i].XPos + (_shapes[i].Rect.Width / 2), _shapes[i].YPos);
                            _starPoints[1] = new Point(_shapes[i].XPos + (int)(0.67 * _shapes[i].Rect.Width), _shapes[i].YPos + (int)(0.33 * _shapes[i].Rect.Height));
                            _starPoints[2] = new Point(_shapes[i].XPos + (_shapes[i].Rect.Width), _shapes[i].YPos + (_shapes[i].Rect.Height / 2));
                            _starPoints[3] = new Point(_shapes[i].XPos + (int)(0.67 * _shapes[i].Rect.Width), _shapes[i].YPos + (int)(0.67 * _shapes[i].Rect.Height));
                            _starPoints[4] = new Point(_shapes[i].XPos + (_shapes[i].Rect.Width / 2), _shapes[i].YPos + _shapes[i].Rect.Height);
                            _starPoints[5] = new Point(_shapes[i].XPos + (int)(0.33 * _shapes[i].Rect.Width), _shapes[i].YPos + (int)(0.67 * _shapes[i].Rect.Height));
                            _starPoints[6] = new Point(_shapes[i].XPos, _shapes[i].YPos + (_shapes[i].Rect.Height / 2));
                            _starPoints[7] = new Point(_shapes[i].XPos + (int)(0.33 * _shapes[i].Rect.Width), _shapes[i].YPos + (int)(0.33 * _shapes[i].Rect.Height));
                            e.Graphics.FillPolygon(_brush4, _starPoints);
                            break;
                    }
                }
                _isDrawing = false;
            }
        }

        #endregion

        // ******************************** Buttons 'Set' handling

        #region Button 'Set'

        // The button 'Options' is handled by the user control Menubutton

        // Set the number of shapes according to the value in 'numericUpDown1' control
        private void setButton_Click(object sender, EventArgs e)
        {
            bool timerEnabled = _timer1.Enabled; // Get the current running state of the timer

            if (timerEnabled) _timer1.Stop(); // Don't draw shapes while they're being created and initialized

            _shapeCount = (int)numericUpDown1.Value;
            ShapesInit();
            if (_shapeSizeCount) _shapeSize = _shapes.Length;

            if (timerEnabled) _timer1.Start(); // Restart the timer if it was previously running
            else Invalidate();

            numericUpDown1.Focus();
            numericUpDown1.Select(0, 5);
        }

        // If the shape is transparent we'll need a low level mouse hook for this (as shown with overlay example 'Scribble')
        // because transparant area's do not receive mouse events. It's not added here.
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            // If the mouse click is inside a shape reverse it's x direction movement
            for (int i = 0; i < _shapes.Length; i++)
            {
                if (_shapes[i].Rect.Contains(e.Location))
                {
                    _shapes[i].XDelta = -_shapes[i].XDelta;
                }
            }
        }

        #endregion

        // ******************************** Contextmenu handling

        #region Contextmenu handling

        // When closed set focus to shapecount input
        private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            numericUpDown1.Focus();
        }

        // ******************************** Contextmenu ShapeForm items handling

        // Round
        private void roundToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _shapeForm = ShapeForm.Round;
            SetToolStripShapeForm(sender);
        }

        // Square
        private void squareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _shapeForm = ShapeForm.Square;
            SetToolStripShapeForm(sender);
        }

        // Diamond
        private void diamondToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _shapeForm = ShapeForm.Diamond;
            SetToolStripShapeForm(sender);
        }

        // Star
        private void starToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _shapeForm = ShapeForm.Star;
            SetToolStripShapeForm(sender);
        }

        // Removes the check marks from the Shape contextmenu items and checks the selected item
        private void SetToolStripShapeForm(object sender)
        {
            foreach (ToolStripItem item in (((ToolStripMenuItem)sender).GetCurrentParent().Items))
            {
                if (item.GetType() == typeof(ToolStripMenuItem))
                {
                    ((ToolStripMenuItem)item).Checked = item == sender;
                }
            }

            if(!_timer1.Enabled) Invalidate();
        }

        // ******************************** Contextmenu ShapeSize items handling

        // OverlaySize/3
        private void overlaySize3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _shapeSize = 3;
            SetToolStripShapeSize(sender);
        }

        // OverlaySize/4
        private void overlaySize4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _shapeSize = 4;
            SetToolStripShapeSize(sender);
        }

        // OverlaySize/5
        private void overlaySize5ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _shapeSize = 5;
            SetToolStripShapeSize(sender);
        }

        // Handles contextmenu item 'OverlaySize/shapecount' to set shape size
        private void overlaySizeBubblesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _shapeSize = _shapes.Length;
            SetToolStripShapeSize(sender);
        }

        // Removes the check marks from the ShapeSize contextmenu items and checks the selected item
        private void SetToolStripShapeSize(object sender)
        {
            // Check selected item (sender) and uncheck other menu items 
            foreach (ToolStripItem item in (((ToolStripMenuItem)sender).GetCurrentParent().Items))
            {
                if (item.GetType() == typeof(ToolStripMenuItem))
                {
                    ((ToolStripMenuItem)item).Checked = item == sender;
                }
            }

            // Set 'shapeSizeCount' to true if the shape size is calculated from the number of bubbles
            _shapeSizeCount = (ToolStripMenuItem)sender == overlaySizeBubblesToolStripMenuItem;

            if (!_timer1.Enabled) Invalidate();
        }

        // ******************************** Contextmenu Color item handling

        // Handles contextmenu item 'Color' to set the background color of the overlay (or shape)
        private void backColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog {Color = BackColor};
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                _brush2.Dispose();
                _brush3.Dispose();
                _brush4.Dispose();

                if (_backTransparent)
                {
                    _brush1.Color = colorDialog1.Color;
                    _brush2 = new HatchBrush(HatchStyle.HorizontalBrick, _brush1.Color, TransparencyKey);
                    _brush3 = new HatchBrush(HatchStyle.OutlinedDiamond, _brush1.Color, TransparencyKey);
                    _brush4 = new HatchBrush(HatchStyle.Percent50, _brush1.Color, TransparencyKey);
                }
                else
                {
                    BackColor = colorDialog1.Color;
                    _brush2 = new HatchBrush(HatchStyle.HorizontalBrick, Color.Red, TransparencyKey);
                    _brush3 = new HatchBrush(HatchStyle.OutlinedDiamond, Color.White, TransparencyKey);
                    _brush4 = new HatchBrush(HatchStyle.Percent50, Color.Gold, TransparencyKey);
                }
            }
            colorDialog1.Dispose();
        }

        // ******************************** Contextmenu Opacity item handling

        // Opacity
        private void HandleToolStripOpacity(object sender, EventArgs e)
        {
            double value;

            // get the text of the menu item (e.g. 50%), remove the '%' and convert to double value
            if (double.TryParse(((ToolStripMenuItem)sender).Text.TrimEnd('%'), out value))
            {
                if (value >= 25 && value <= 100)
                {
                    Opacity = value / 100;

                    // Removes the check marks from the Opacity contextmenu items and checks the selected item
                    foreach (ToolStripItem item in (((ToolStripMenuItem)sender).GetCurrentParent().Items))
                    {
                        if (item.GetType() == typeof(ToolStripMenuItem))
                        {
                            ((ToolStripMenuItem)item).Checked = item == sender;
                        }
                    }
                }
            }
        }

        // ******************************** Contextmenu Invert Transparency handling

        // Invert Transparency
        private void invertTransparencyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_backTransparent)
            {
                BackColor = _brush1.Color;
                _brush1.Color = TransparencyKey;
            }
            else
            {
                _brush1.Color = BackColor;
                BackColor = TransparencyKey;
            }
            _backTransparent = !_backTransparent;
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Peter Vegter
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190423.1 | Last Updated 7 Aug 2018
Article Copyright 2010 by Peter Vegter
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid