Hi, Amda,
Based on my understanding, you want to get 1) the Control you click and 2) the coordinates of your click from IMessageFilter, don't you?
To solve this problem, lets review Windows Messages and Message Queuesfirst.
http://msdn2.microsoft.com/en-us/library/ms644927.aspx
http://msdn2.microsoft.com/en-us/library/ms644928.aspx
As you can see, the Windows applications will receive and respond tothe Windows Messages when it is running in Windows.
And the IMessageFilter is used to "allow an application to capture a message before it is dispatched to a control or form."
http://msdn2.microsoft.com/en-us/library/system.windows.forms.imessagefilter.aspx
So you should know the WindowsMessage name before you want to get that from the Message object.
And here theWM_LBUTTONDOWN message is helpful here, because the Windows Form gets this Message when it is clicked. But it could be complex to get the Control Messages, because you will have to deal with WM_COMMAND or WM_NOTIFY seperately, I would rather do that with common WinForm functions.
http://msdn2.microsoft.com/en-us/library/bb775494.aspx
For example,
MyFilter.cs (My IMessageFilter to capture the Windows Messages)
Code Block
class MyFilter : IMessageFilter
{
public delegate void MyMouseHandler(object sender, MyArgs e);
public event MyMouseHandler MyMouseClick;
private const int WM_LBUTTONDOWN = 0x0201;
#region public bool PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case WM_LBUTTONDOWN :
Point mouse = Control.MousePosition;
OnMouseClick(new MyArgs(mouse));
break;
default:
break;
}
return false;
}
#endregion
private void OnMouseClick(MyArgs e)
{
if (MyMouseClick != null)
{
MyMouseClick(this, e);
}
}
}
public class MyArgs : EventArgs
{
private Point position;
public Point Position
{
get { return position; }
set { position = value; }
}
public MyArgs(Point _position)
{
position = _position;
}
}
Code Block
public Form1()
{
InitializeComponent();
filer = new MyFilter();
filer.MyMouseClick += new MyFilter.MyMouseHandler(filer_MyMouseClick);
Application.AddMessageFilter(filer);
}
void filer_MyMouseClick(object sender, MyArgs e)
{
Point location = PointToClient(e.Position);
label1.Text = "X="+ location.X.ToString() + " Y=" + location.Y.ToString();
foreach (Control c in this.Controls)
{
if (c.Bounds.Contains(location))
{
label1.Text += " " + c.Name + " clicked";
}
}
}
MyFilter filer;
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.RemoveMessageFilter(filer);
}
Hope this helps,
Regards