Yoshi Walsh
Yoshi Walsh

Reputation: 2077

Mouse events not fired

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);

Upvotes: 6

Views: 5537

Answers (3)

John Idarraga
John Idarraga

Reputation: 1

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

Momentary = true

Upvotes: 0

Walter Stabosz
Walter Stabosz

Reputation: 7735

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.

Upvotes: 2

Damien
Damien

Reputation: 106

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

Upvotes: 1

Related Questions