Click here to Skip to main content
14,034,739 members
Click here to Skip to main content

Stats

748.5K 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
/****************************************************************
    
    PVS.AVPlayer - Version 0.91
    August 2018, The Netherlands
    © Copyright 2018 PVS The Netherlands - Free to Use

    ****************

    For use with Microsoft .NET Framework version 2.0 or higher and any CPU.
    Created with Microsoft Visual Studio.

    Articles on CodeProject with information on the use of the PVS.AVPlayer library:
    About the Player: http://www.codeproject.com/Articles/109714/PVS-AVPlayer-MCI-Audio-and-Video-Library
    About the Sound Recorder: http://www.codeproject.com/Articles/1116698/PVS-AVPlayer-MCI-Sound-Recorder

    ****************

    The PVS.AVPlayer library source code is divided into 10 files:

     1. Player.cs            - Player source code
     2. MouseEvents.cs       - extension source code of Player.cs, provides player display mouse events
     3. PeakMeter.cs         - extension source code of Player.cs, provides player audio peak level values
     4. DisplayClones.cs     - extension source code of Player.cs, provides player multiple video displays 
     5. Subtitles.cs         - extension source code of Player.cs, provides player SubRip (.srt) subtitles
     6. Signals.cs           - extension source code of Player.cs, provides player media position signaling
     7. CursorHide.cs        - extension source code of Player.cs, hides the mouse cursor during inactivity
     8. Recorder.cs          - Sound Recorder source code
     9. PlayerRecorder.cs    - code used by both Player.cs (and its extension files) and Recorder.cs
    10. Infolabel.cs         - Info Label (custom ToolTip) source code
    
    Required references:
    System
    System.Drawing
    System.Windows.Forms

    ****************
 
    This file: CursorHide.cs

    Player Class
    Extension to file 'Player.cs'.

    ****************

    About Media Control Interface (MCI)
    - for information about MCI, please see https://msdn.microsoft.com/en-us/library/vs/alm/dd757151(v=vs.85).aspx
    - you can find many articles about mci on the internet, search examples: 'c# mci', 'vb mci', 'mcisendstring'
      or the subject of your question.
    - the PVS.AVPlayer library also provides 'direct access' to MCI (e.g. 'Mci.MciSendString').

    ****************

    Thanks!

    Many thanks to Microsoft (Windows, .NET Framework, Visual Studio Express, etc.), all the people
    writing about programming on the internet (a great source for ideas and solving problems),
    the websites publishing those or other writings about programming, the people responding
    to the PVS.AVPlayer articles with comments and suggestions and, of course, CodeProject:
    thank you Deeksha, Smitha and Sean Ewington for the beautiful articles and all!

    Special thanks to Sean Ewington of CodeProject who also took care of publishing the many code
    updates and changes in the PVS.AVPlayer articles in a friendly, fast, and highly competent manner.
    Thank you very much, Sean!

    Peter Vegter
    August 2018, The Netherlands

    ****************************************************************/

#region Usings

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace PVS.AVPlayer
{
    public partial class Player
    {
        /*
            Automatically hides (after not having moved for a few seconds) and unhides (when moved or clicked) the mouse cursor when playing media.
            A MessageFilter is used to detect mouse events.
        */

        // ******************************** Cursor Hide - Item Class / MessageFilter

        #region Cursor Hide - Cursor Hide - Item Class / MessageFilter

        private sealed class CH_Item
        {
            internal Form   _form;
            internal Player _player;

            internal CH_Item(Form form, Player player)
            {
                _form = form;
                _player = player;
            }
        }

        private sealed class CH_MessageFilter : IMessageFilter
        {
            private const int WM_MOUSEMOVE  = 0x0200;
            private const int WM_MOUSEWHEEL = 0x020A;

            public bool PreFilterMessage(ref Message m)
            {
                if (ch_Disabled || ch_Busy) return false;

                if (m.Msg == WM_MOUSEMOVE)
                {
                    ch_Moved = DateTime.Now;
                    if (ch_Hidden)
                    {
                        Point _cursorHideNewPosition = Cursor.Position;
                        if (Math.Abs(_cursorHideNewPosition.X - ch_OldPosition.X) >= 2 || Math.Abs(_cursorHideNewPosition.Y - ch_OldPosition.Y) >= 2)
                        {
                            Cursor.Show();
                            ch_Hidden = false;
                            ch_Timer.Start();

                            if (MediaCursorHideChanged != null)
                            {
                                ch_EventArgs._reason = CursorChangedReason.MouseMoved;
                                CH_RaiseEvent(Form.ActiveForm);
                            }
                        }
                        else ch_OldPosition = _cursorHideNewPosition;
                    }
                }
                else if (m.Msg > WM_MOUSEMOVE && m.Msg <= WM_MOUSEWHEEL)
                {
                    ch_Moved = DateTime.Now;
                    if (ch_Hidden)
                    {
                        Cursor.Show();
                        ch_Hidden = false;
                        ch_Timer.Start();

                        if (MediaCursorHideChanged != null)
                        {
                            ch_EventArgs._reason = CursorChangedReason.MouseAction;
                            CH_RaiseEvent(Form.ActiveForm);
                        }
                    }
                }
                return false;
            }
        }

        #endregion


        // ******************************** Cursor Hide - Fields

        #region Cursor Hide - Fields

        internal static Timer               ch_Timer;
        private static bool                 ch_Busy;
        internal static int                 ch_Delay = 3; // default delay 3 seconds
        private static DateTime             ch_Moved;
        private static Point                ch_OldPosition;
        private static bool                 ch_Active;
        internal static bool                ch_Disabled;
        private static int                  ch_DisabledCount;
        internal static bool                ch_Hidden;
        private static CH_MessageFilter     ch_MessageFilter;
        private static List<CH_Item>        ch_Items;
        internal static CursorHideEventArgs ch_EventArgs;
        private static bool                 ch_ModalThread;

        #endregion


        // ******************************** Cursor Hide - Main

        #region Cursor Hide - Main

        /// <summary>
        /// Provides access to the cursor hide settings of the player (e.g. Player.CursorHide.Add).
        /// </summary>
        public CursorHide CursorHide
        {
            get
            {
                if (_cursorHideClass == null)
                {
                    _cursorHideClass = new CursorHide(this);
                    ch_EventArgs = new CursorHideEventArgs();
                }
                return _cursorHideClass;
            }
        }

        #endregion


        // ******************************** Cursor Hide - Add / Remove / Has Items / Form Closed Handler

        #region Cursor Hide - Add / Remove / Has Items / Form Closed Handler

        internal static int CH_AddItem(Form form, Player player, Control display)
        {
            int result = Global.MCIERR_NO_WINDOW;

            if (form != null)
            {
                int index = -1;
                result = Global.MCIERR_NO_ERROR;

                if (ch_Items == null)
                {
                    ch_Items = new List<CH_Item>(8);
                }
                else
                {
                    int count = ch_Items.Count;
                    for (int i = 0; i < count; i++)
                    {
                        if (ch_Items[i]._form == form && ch_Items[i]._player == player)
                        {
                            index = i;
                            break;
                        }
                    }
                }

                if (index == -1)
                {
                    CH_Item item = new CH_Item(form, player);
                    ch_Items.Add(item);
                    form.FormClosed += CH_FormClosedHandler;

                    if (!ch_Active)
                    {
                        CH_Start();
                    }
                }
            }
            return result;
        }

        internal static int CH_RemoveItem(Form form, Player player, Control display)
        {
            int result = Global.MCIERR_NO_WINDOW;

            if (ch_Items != null)
            {
                int count = ch_Items.Count;
                result = Global.MCIERR_NO_ERROR;

                for (int i = 0; i < count; i++)
                {
                    if (ch_Items[i]._form == form && ch_Items[i]._player == player)
                    {
                        ch_Items[i]._form.FormClosed -= CH_FormClosedHandler;
                        ch_Items.RemoveAt(i);
                        if (ch_Active && ch_Items.Count <= 0)
                        {
                            CH_Stop();
                        }
                        break;
                    }
                }
            }
            return result;
        }

        internal static int CH_RemovePlayerItems(Player player)
        {
            if (player != null)
            {
                if (ch_Items != null)
                {
                    for (int i = ch_Items.Count - 1; i >= 0; --i)
                    {
                        if (ch_Items[i]._player == player)
                        {
                            ch_Items[i]._form.FormClosed -= CH_FormClosedHandler;
                            ch_Items.RemoveAt(i);
                        }
                    }

                    if (ch_Active && ch_Items.Count <= 0)
                    {
                        CH_Stop();
                    }
                }
            }
            return Global.MCIERR_NO_ERROR;
        }

        internal static bool CH_HasItems(Player player)
        {
            bool result = false;

            if (player != null)
            {
                if (ch_Items != null)
                {
                    for (int index = ch_Items.Count - 1; index >= 0; --index)
                    {
                        if (ch_Items[index]._player == player)
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            return result;
        }

        private static void CH_FormClosedHandler(object sender, FormClosedEventArgs e)
        {
            if (ch_Items != null)
            {
                Form form = (Form)sender;

                for (int i = ch_Items.Count - 1; i >= 0; --i)
                {
                    if (ch_Items[i]._form == form)
                    {
                        form.FormClosed -= CH_FormClosedHandler;
                        ch_Items.RemoveAt(i);
                    }
                }

                if (ch_Active && ch_Items.Count <= 0)
                {
                    CH_Stop();
                }
            }
        }

        #endregion


        // ******************************** Cursor Hide - Show Cursor / Hide Cursor (Forced)

        #region Cursor Hide - Show Cursor / Hide Cursor (Forced)

        internal static void CH_ShowCursor()
        {
            if (ch_Active && ch_Hidden)
            {
                Cursor.Show();
                ch_Hidden = false;
                ch_Moved = DateTime.Now;
                ch_Timer.Start();

                if (MediaCursorHideChanged != null)
                {
                    ch_EventArgs._reason = CursorChangedReason.UserCommand;
                    CH_RaiseEvent(Form.ActiveForm);
                }
            }
        }

        internal static void CH_HideCursor()
        {
            if (ch_Active && !ch_Hidden)
            {
                ch_Timer.Stop();
                ch_OldPosition = Cursor.Position;
                Cursor.Hide();
                ch_Hidden = true;

                if (MediaCursorHideChanged != null)
                {
                    ch_EventArgs._reason = CursorChangedReason.UserCommand;
                    CH_RaiseEvent(Form.ActiveForm);
                }
            }
        }

        #endregion


        // ******************************** Cursor Hide - Start / Stop / Disable / Modal Thread

        #region Cursor Hide - Start / Stop / Disable / Modal Thread

        private static void CH_Start()
        {
            if (!ch_Active)
            {
                if (ch_Timer == null)
                {
                    ch_Timer = new Timer();
                    ch_Timer.Interval = ch_Delay == 1 ? 500 : 100; // _cursorHideDelay * 500;
                    ch_Timer.Tick += CH_TimerTick;

                    ch_MessageFilter = new CH_MessageFilter();
                }

                ch_Moved = DateTime.Now;
                ch_Timer.Start();
                Application.AddMessageFilter(ch_MessageFilter);

                Application.EnterThreadModal += CH_EnterThreadModal;
                Application.LeaveThreadModal += CH_LeaveThreadModal;

                MF_StartMenuMonitor();

                ch_Active = true;
            }
        }

        private static void CH_Stop()
        {
            if (ch_Active)
            {
                ch_Timer.Dispose();
                ch_Timer = null;

                Application.RemoveMessageFilter(ch_MessageFilter);
                ch_MessageFilter = null;

                ch_Items.Clear();
                ch_Items = null;

                ch_Disabled = false;
                ch_DisabledCount = 0;

                Application.EnterThreadModal -= CH_EnterThreadModal;
                Application.LeaveThreadModal -= CH_LeaveThreadModal;
                ch_ModalThread = false;

                if (!mf_Active) MF_StopMenuMonitor();

                ch_Active = false;

                if (ch_Hidden)
                {
                    Cursor.Show();
                    ch_Hidden = false;
                }
            }
        }

        internal static bool CH_Disabled
        {
            set
            {
                if (ch_Active)
                {
                    if (value)
                    {
                        ch_DisabledCount++;
                        ch_Disabled = true;

                        if (ch_Hidden)
                        {
                            Cursor.Show();
                            ch_Hidden = false;
                            ch_Timer.Start();

                            if (MediaCursorHideChanged != null) CH_RaiseEvent(Form.ActiveForm);
                        }
                    }
                    else
                    {
                        ch_DisabledCount--;
                        if (ch_DisabledCount <= 0)
                        {
                            ch_DisabledCount = 0;
                            ch_Moved = DateTime.Now;
                            ch_Disabled = false;
                        }
                    }
                }
            }
        }

        private static void CH_EnterThreadModal(object sender, EventArgs e)
        {
            if (!ch_ModalThread && !ch_Disabled)
            {
                ch_EventArgs._reason = CursorChangedReason.ModalDialog; // alt. way to set _reason
                CH_Disabled = true;
                ch_ModalThread = true;
            }
        }

        private static void CH_LeaveThreadModal(object sender, EventArgs e)
        {
            if (ch_ModalThread)
            {
                if (ch_Disabled) CH_Disabled = false;
                ch_ModalThread = false;
            }
        }

        #endregion


        // ******************************** Cursor Hide - Timer Tick / Raise Event

        #region Cursor Hide - Timer Tick / Raise Event

        private static void CH_TimerTick(object sender, EventArgs e)
        {
            if (ch_Disabled || ch_Busy || mf_Ignore) return;
            ch_Busy = true;

            if (!ch_Hidden && Form.ActiveForm != null)
            {
                if ((DateTime.Now - ch_Moved).TotalSeconds >= ch_Delay)
                {
                    int count = ch_Items.Count;
                    for (int i = 0; i < count; i++)
                    {
                        if (ch_Items[i]._form == Form.ActiveForm)
                        {
                            if (ch_Items[i]._player.Playing)
                            {
                                ch_Timer.Stop();
                                ch_OldPosition = Cursor.Position;
                                Cursor.Hide();
                                ch_Hidden = true;

                                if (MediaCursorHideChanged != null)
                                {
                                    ch_EventArgs._reason = CursorChangedReason.MouseIdle;
                                    CH_RaiseEvent(ch_Items[i]._form);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            ch_Busy = false;
        }

        private static void CH_RaiseEvent(Form form)
        {
            //if (MediaCursorHideChanged != null)
            {
                ch_EventArgs._visible = !ch_Hidden;
                ch_EventArgs._form = form;
                MediaCursorHideChanged(null, ch_EventArgs);
            }
        }

        #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
Web05 | 2.8.190423.1 | Last Updated 7 Aug 2018
Article Copyright 2010 by Peter Vegter
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid