How to capture UserControl mouse events in it's container

S

Stefan

Hello,

I want to be able to capture UserControl mouse events by the container of
the UC.

My final goal is to be able to move at run-time the UC in the container. I
have been able to do this with regular controls, by capturing the
MouseDown,MouseMove and MouseUp events. But when it comes to UserControls,
the mouse events are captured by the UC's different controls and not the
container.

I have tried setting the Capture property of the container to True but it
does'nt help.

If someone knows the answer to this, please let me know.

Thanks
 
S

Stefan

Here is more info:

Mouse Event on the UserControl are captured by it's container but mouse
events on the UserControls contained controls are not captured. If the user
clicks a TextBox contained by the UserControl the mouse event is not
propagated to the container...

Thanks
 
C

ClayB [Syncfusion]

You can have your UserControl implement IMessageFilter. This will allow it
to get all messages application wide. You can then filter those messages
down to the ones over your user control, and even further filter to to the
specific you want to know about. Below is a code snippet.

--
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools


public class UserControl1 : System.Windows.Forms.UserControl ,
IMessageFilter
{
public UserControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
Application.AddMessageFilter(this);
}

const int WM_MOUSEMOVE = 0x200;
const int WM_LBUTTONUP = 0x202;

public bool PreFilterMessage(ref Message m)
{
if(!this.IsDisposed &&
this.Bounds.Contains(this.PointToClient(Control.MousePosition)))
{
if(m.Msg >= WM_MOUSEMOVE && m.Msg <= WM_LBUTTONUP)
{
Console.WriteLine(m.ToString());
// return true; //if you have handled teh message your self
}
}
return false;
}

//.....more code
}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Stefan,

This is the way Windows works. You can catch other control's mouse events
because they are simple controls. The have single window surface(the have no
children). With user control's it might be different depending on the
structure of the control. Usually they have children so, each children is a
window on its own and as such it handles all the windcdows messages. The
only way I can think of is too hook all the children's mouse events from the
user control and reraise them. Don't forget to adjust the mouse coordinates
to be relative to the user control.
 
S

Stefan

I tried implementing IMessageFilter, and it works great.

In order to verify that the mouse event is in within the control
boundairies, I used ClientRectangle instead of Bounds.

this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition))
instead of :
this.Bounds.Contains(this.PointToClient(Control.MousePosition))

Thanks a lot for you help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top