Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse event on transparent background

I have create several canvas with transparent background and wanna make some move event on it.

However, I found that all mouse event (e.g. MouseLeftButtonDown) cannot function of the background of the canvas is Null or Transparent. But if I set the background to some solid color (say, red or blue), the mouse event function well.

Why?

Could I make the mouse event function properly with transparent background?

Thank you!

like image 357
user883434 Avatar asked Nov 03 '11 07:11

user883434


2 Answers

Note that there is a difference in setting the background to transparent as opposed to not setting it (or setting it to null). My experience is that hit-testing works on transparent, but not on a null background.

like image 188
Arjan Einbu Avatar answered Oct 08 '22 09:10

Arjan Einbu


I'm not sure why you get the results you get but it should work fine when the background is transparent (i.e. you explicitly set it to Brushes.Transparent, either through XAML or code). If it is null, WPF will not include it in hit testing, and thus it won't be eligible for mouse events.

See e.g. http://msdn.microsoft.com/en-us/library/ms752097.aspx (A visual object that is transparent can also be hit test.)

Most likely you have another UIElement in your element tree that is capturing and handling the mouse event before your canvas sees it (i.e. by setting e.Handled to true)

like image 35
Isak Savo Avatar answered Oct 08 '22 10:10

Isak Savo