6

I'm making a C# WinForms application. The MouseMove and MouseClick events of the form aren't getting fired for some reason. (I'm probably going to feel like an idiot when I find out why.) It is a transparent form (TransparencyKey is set to the background colour) with a semi-transparent animated gif in a Picture Box. I am making a screensaver. Any suggestions?

EDIT: MainScreensaver.cs

    Random randGen = new Random();
    public MainScreensaver(Rectangle bounds)
    {
        InitializeComponent();
        this.Bounds = Bounds;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Rectangle screen = Screen.PrimaryScreen.Bounds;
        Point position = new Point(randGen.Next(0,screen.Width-this.Width)+screen.Left,randGen.Next(0,screen.Height-this.Height)+screen.Top);
        this.Location = position;
    }

    private void MainScreensaver_Load(object sender, EventArgs e)
    {
        Cursor.Hide();
        TopMost = true;
    }
    private Point mouseLocation;

    private void MainScreensaver_MouseMove(object sender, MouseEventArgs e)
    {
        if (!mouseLocation.IsEmpty)
        {
            // Terminate if mouse is moved a significant distance
            if (Math.Abs(mouseLocation.X - e.X) > 5 ||
                Math.Abs(mouseLocation.Y - e.Y) > 5)
                Application.Exit();
        }

        // Update current mouse location
        mouseLocation = e.Location;
    }

    private void MainScreensaver_KeyPress(object sender, KeyPressEventArgs e)
    {
        Application.Exit();
    }

    private void MainScreensaver_Deactive(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void MainScreensaver_MouseClick(object sender, MouseEventArgs e)
    {
        Application.Exit();
    }

Excerpt from MainScreensaver.Designer.cs InitialiseComponent()

    this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseClick);
    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseMove);
Joshua Walsh
  • 1,915
  • 5
  • 25
  • 50
  • 3
    Please can you provide your code? – anonymous Feb 11 '12 at 10:41
  • Go to the designer and remove the Event handler using the 'Properties' panel. Then re-add the event handler. Some times VS2010 plays some funny games in WinForms and readding the handler can rectify the issue. Also, insure that hiding the Cursor is not causing click event to also be 'hidden'. – MoonKnight Feb 11 '12 at 10:58
  • Going to sleep now, will check again in the morning. – Joshua Walsh Feb 11 '12 at 11:19
  • Not a clue why mouse move events aren't triggered, but my click events where being intercepted by a PictureBox. – Joshua Walsh Feb 13 '12 at 06:12
  • I am having this same problem. If you take away the transparency, the events work fine. There has to be a way to accomplish this with the TransparencyKey intact. – Randy Cleary Jun 21 '12 at 14:21

2 Answers2

2

This isn't an answer to you question, but I'm leaving this answer in case anyone else stumbles upon this question while trying to debug this same issue (which is how I got here)

In my case, I had a class that was derived from Form.

I was also using TransparencyKey.

Some things I noticed

  • The events will not fire on the transparent parts of the form.
  • The events will not fire if the mouse cursor is over another control on the form.
  • The events will not fire if you override WndProc and set the result of a WM_NCHITTEST message. Windows doesn't even send out the corresponding mouse messages that would cause the .NET events.

My Solution

In my constructor, I had forgotten to call InitializeComponent().

Which was where the event handlers were being bound to my controls.

Events were not firing because the handlers were not being bound.

Walter Stabosz
  • 7,447
  • 5
  • 43
  • 75
1

Are you sure that your form has focus? If your form does not have focus, the mouse events will not be fired.

Damien
  • 106
  • 4
  • My form definitely has focus, I have a Deactivate event that get's triggered successfully if I click anywhere else. – Joshua Walsh Feb 11 '12 at 10:55
  • I thumbed up your question because I really didn't provide much info, I just wanted suggestions. While yours didn't help, it was still a good (if obvious) idea. I have more than likely missed something obvious. – Joshua Walsh Feb 11 '12 at 10:56