3

下面的 WPF 程序会弹出一个如下所示的窗口:

在此处输入图像描述

在黑色方块之外的鼠标移动会导致窗口标题随着鼠标的位置而更新。当鼠标进入方块时更新停止。

即使鼠标在正方形上,我也想MouseMove继续触发。有没有办法做到这一点?

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();
        }
    }
}
4

3 回答 3

3

到目前为止,最简单的方法是使用窗口上的“隧道”事件 PreviewMouseDown。它首先被传递到窗口并在层次结构中向上工作。因此,您在窗口中有哪些其他元素并不重要。在代码中:

public partial class Window1 : Window {
    public Window1() {
        InitializeComponent();
        this.PreviewMouseMove += new MouseEventHandler(Window1_PreviewMouseMove);
    }
    void Window1_PreviewMouseMove(object sender, MouseEventArgs e) {
        this.Title = e.GetPosition(this).ToString();
    }
}
于 2012-11-10T22:55:32.503 回答
2

您需要捕获鼠标,这将允许您Canvas继续响应MouseMove事件,尝试类似这样的操作,只要按下鼠标,它就会更新您的坐标

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));
        };

        Mouse.Capture(canvas);
        MouseDown += (s, e) => Mouse.Capture((UIElement)s);
        MouseMove += (s, e) => Title = e.GetPosition(canvas).ToString();
        MouseUp += (s, e) => Mouse.Capture(null);

    }

第二种方法

public MainWindow()
{
    InitializeComponent();
    var canvas = new Canvas();
    Content = canvas;

    DrawingVisualElement element = new DrawingVisualElement();
    Grid myElement = new Grid();
    canvas.Children.Add(myElement);

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

        DrawingImage myImage = new DrawingImage(element.visual.Drawing);
        myElement.Height = myImage.Height;
        myElement.Width = myImage.Width;
        myElement.Background = new ImageBrush(myImage);
    };


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

使用钩子 一定要放一个using System.Windows.Interop;

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));
            }
        };
        this.SourceInitialized += new EventHandler(OnSourceInitialized);
    }

    void OnSourceInitialized(object sender, EventArgs e)
    {
        HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
        source.AddHook(new HwndSourceHook(HandleMessages));

    }
    IntPtr HandleMessages(IntPtr hwnd, int msg,IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == 0x200)
            Title = Mouse.GetPosition(this).ToString(); // because I did not want to split the lParam into High/Low values for Position information
        return IntPtr.Zero;
    }
}
于 2012-11-10T22:13:02.313 回答
0

答案就简单多了。您只需要设置 element.IsHitTestVisible=false;

于 2014-01-23T17:39:08.213 回答