A rectangle drawn with DrawingContext.DrawRectangle blocks mouse

Posted by dharmatech on Stack Overflow See other posts from Stack Overflow or by dharmatech
Published on 2012-11-10T21:24:58Z Indexed on 2012/11/10 23:00 UTC
Read the original article Hit count: 219

Filed under:
|

The WPF program below puts up a window which looks like this:

enter image description here

Mouse-movement outside the black square causes the window title to be updated with the mouse's position. The updating stops when the mouse enters the square.

I'd like for MouseMove to continue to trigger even when the mouse is over the square. Is there a way to do this?

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Wpf_Particle_Demo
{
    class DrawingVisualElement : FrameworkElement
    {
        public DrawingVisual visual;

        public DrawingVisualElement() { visual = new DrawingVisual(); }

        protected override int VisualChildrenCount { get { return 1; } }

        protected override Visual GetVisualChild(int index) { return visual; }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var canvas = new Canvas();

            Content = canvas;

            var element = new DrawingVisualElement();

            canvas.Children.Add(element);

            CompositionTarget.Rendering += (s, e) =>
                {
                    using (var dc = element.visual.RenderOpen())
                        dc.DrawRectangle(Brushes.Black, null, new Rect(0, 0, 50, 50));
                };

            MouseMove += (s, e) => Title = e.GetPosition(canvas).ToString();
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf