none
c# windows form project disabling mouse clicks and enabling RRS feed

  • Question

  • I am playing some videos on windows form project, displaying a full screen videos, and i do not want the user to click on the

    mouse while video is paying since it would interrupt the video and menu would pop up, so i place the code to disable the mouse

    clicks while form loads with media player and then ones the video is finished i want to enable back.

    The disabling works, however enabling fails, my mouse still disable after the video is finished.

    Here is code:

                                                                                                                                               

     private void Media_Player_Form_Load(object sender, EventArgs e)
            {   
                this.Width = 233;
                this.Height = 215;
                axWindowsMediaPlayer1.Width = 233;
                axWindowsMediaPlayer1.Height = 215;
                Point pt = new Point((Screen.GetWorkingArea(this).Width - this.Width) / 2, (Screen.GetWorkingArea(this).Height - this.Height) / 2);
                this.Location = pt;

               MouseClickMessageFilter B = new MouseClickMessageFilter();
                B.DisableMouseClicks();

            }

            private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
            {    
                if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
                {

                    this.Width = 233;
                    this.Height = 215;
                    axWindowsMediaPlayer1.Width = 233;
                    axWindowsMediaPlayer1.Height = 215;

                    MouseClickMessageFilter B = new MouseClickMessageFilter();
                    B.EnableMouseClicks();

    this.Close();


                } 

            }



        }
    }


            public class MouseClickMessageFilter : IMessageFilter
            {
                private MouseClickMessageFilter Filter;

                private const int LButtonDown = 0x201;
                private const int LButtonUp = 0x202;
                private const int LButtonDoubleClick = 0x203;
                private const int RButtonUp = 0x205;

                public bool PreFilterMessage(ref System.Windows.Forms.Message m)
                {
                    switch (m.Msg)
                    {
                        case LButtonDown:
                        case LButtonUp:
                        case LButtonDoubleClick:
                       // case RButtonDown;
                        case RButtonUp:
                      //  case RButtonDoubleClick:

                        return true;
                    }
                    return false;
                }
                public void DisableMouseClicks()
                {
                    if (this.Filter == null)
                    {
                        this.Filter = new MouseClickMessageFilter();
                        Application.AddMessageFilter(this.Filter);
                    }
                }

                public void EnableMouseClicks()
                {
                    if ((this.Filter != null))
                    {
                        Application.RemoveMessageFilter(this.Filter);
                        this.Filter = null;
                    }
                }



            } 

    Write back, thanks in advance



    • Edited by Gennady46 Tuesday, February 26, 2019 3:56 AM
    Tuesday, February 26, 2019 3:54 AM

Answers

  • Hi Jack, thank you so much, it worked :)

    However i have one more question,

    after i have created a new object like : 

    MouseClickMessageFilter B = new MouseClickMessageFilter();

    I was trying to delete it to free the memory,

    so every time user plays a video it would create a new object.

    I could not call B.Dispose(); Is there another way to do it?

    And of course i will mark as answer shortly.

    Replay back...




    • Edited by Gennady46 Tuesday, February 26, 2019 9:43 AM
    • Marked as answer by Gennady46 Wednesday, February 27, 2019 5:41 AM
    Tuesday, February 26, 2019 9:43 AM

All replies

  • Hi Gennady46,

    Thank you for posting here.

    For your question, you want to enable your mouse successfully after the video is finished.

    It is best for you to put MouseClickMessageFilter outside the code.

    In the picture, the first time video is in disabled and the second time video is in enabled.

    You could try the following code, please pay attention to my bold code.

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            MouseClickMessageFilter B = new MouseClickMessageFilter();
            private void Form1_Load(object sender, EventArgs e)
            {
                this.Width = 233;
                this.Height = 215;
                axWindowsMediaPlayer1.Width = 233;
                axWindowsMediaPlayer1.Height = 215;
                Point pt = new Point((Screen.GetWorkingArea(this).Width - this.Width) / 2, (Screen.GetWorkingArea(this).Height - this.Height) / 2);
                this.Location = pt;
                axWindowsMediaPlayer1.URL = @"C: \test11.mp4";
                axWindowsMediaPlayer1.Ctlcontrols.play();
                B.DisableMouseClicks();
            }
    
            private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
            {
                if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
                {
    
                    this.Width = 233;
                    this.Height = 215;
                    axWindowsMediaPlayer1.Width = 233;
                    axWindowsMediaPlayer1.Height = 215;
                    B.EnableMouseClicks();
                    this.Close();
                }
            }
    
    }
    
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 26, 2019 6:59 AM
    Moderator
  • Hi Jack, thank you so much, it worked :)

    However i have one more question,

    after i have created a new object like : 

    MouseClickMessageFilter B = new MouseClickMessageFilter();

    I was trying to delete it to free the memory,

    so every time user plays a video it would create a new object.

    I could not call B.Dispose(); Is there another way to do it?

    And of course i will mark as answer shortly.

    Replay back...




    • Edited by Gennady46 Tuesday, February 26, 2019 9:43 AM
    • Marked as answer by Gennady46 Wednesday, February 27, 2019 5:41 AM
    Tuesday, February 26, 2019 9:43 AM
  • Hi 

    Thanks for feedback.

    For your new question, you want to release object resources.

    I suggest that you don't worry about this problem,because each class will automatically clean up the object after it is used due to Garbage Collection.

    You could refer to the following link to understand it.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/destructors

    https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals

    Hope my explanation could be helpful.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 27, 2019 5:07 AM
    Moderator