Mouse events not fired
Asked Answered
H

3

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);
Hearten answered 11/2, 2012 at 10:40 Comment(5)
Please can you provide your code?Cheju
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'.Phelips
Going to sleep now, will check again in the morning.Hearten
Not a clue why mouse move events aren't triggered, but my click events where being intercepted by a PictureBox.Hearten
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.Sulphone
H
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.

Hussey answered 17/12, 2021 at 18:47 Comment(0)
A
1

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

Antecede answered 11/2, 2012 at 10:52 Comment(2)
My form definitely has focus, I have a Deactivate event that get's triggered successfully if I click anywhere else.Hearten
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.Hearten
H
0

I had the situation where MouseDown worked correctly but MouseUp wouldn't trigger. This property set to true fixes it:

Momentary = true

Halftone answered 16/1, 2024 at 17:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.