views:

505

answers:

3

Hi, i am trying to handle a mouseclick event on a particular form that should fire if the mouse cursor falls between a set of coordinates - lets say a square.

I understand that if i had an empty form i could simply tie in to the mousemove event and off i go. But in reality there may be up to 10 different overlapping controls and in my test app the mousemove event only fires if the cursor is on the actual form itself and not if its over a child control.

Does anyone know how to handle this event when there are an unknown number of child controls at design time?

Is there an easy one-liner i can use?

A: 

Why don't you just use the controls' mouseover event handlers?

Robert Harvey
The controls that i am working with do not expose the events i need
Grant
+1  A: 

try this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        AddMouseMoveHandler(this);
    }

    private void AddMouseMoveHandler(Control c)
    {
        c.MouseMove += MouseMoveHandler;
        if(c.Controls.Count>0)
        {
            foreach (Control ct in c.Controls)
                AddMouseMoveHandler(ct);
        }
    }

    private void MouseMoveHandler(object sender, MouseEventArgs e)
    {
        lblXY.Text = string.Format("X: {0}, Y:{1}", e.X, e.Y);
    }
}
TheVillageIdiot
The OP says the controls he is using do not implement MouseMove, unless I am misunderstanding him.
Robert Harvey
You are correct Robert.
Grant
Yes I read it only after submitting answer. This leaves him with the option of installing global mouse hooks.
TheVillageIdiot
+1  A: 

imho there is a bit of a binary situation here : and there is no "one-liner." the only solution I can see is to get your controls that don't implement events into a .NET container that does.

When any control gets a click, the normal expected behavior is that it will become the Active Control of the Form (which can always be accessed by this.ActivceControl).

But, particulary if the control you clicked captures the mouse, something has got to raise an event since .NET does not implement event "bubbling" (as WPF does).

The usual way to deal with extending behavior of any object that is sealed or whatever is to write an extension method, and I have found writing extensions for Control quite easy, but I don't know if that will help you in this case. Unfortunately I am out of my home country right now, and do not have Visual Studio to play around with.

One strategy you can use to determine if a given Point on a Form falls within the bounds of any Control is to enumerate the areas (Bounds) of all controls on the Form via 'forall of the Forms Control.Collection (this.Controls). But, if you have overlapping Controls, you then have the issue of more than one control possibly containing a given point.

best, Bill

BillW
TheVillageIdiot got there first about Global Hooks, and you might wish to accept his answer rather than mine. I also was going to suggest you look into Global Mouse Hooks like : George Mamaladze's "classic" article : http://www.codeproject.com/KB/cs/globalhook.aspx I can verify that his GlobalHook code will compile, run fine in VS 2010 beta 2 compiled against FrameWork <= 3.5 (breaks against FrameWork 4.0). You'll find other articles on GlobalHooks on CodeProject also.I'm curious what kind of control you are using that does not expose a mousedown or click event. best,
BillW