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

By far the simplest way is to use the "tunneling" event on the window, PreviewMouseDown. It is delivered to the window first and works its way up the hierarchy. So it doesn't matter at all which other elements you have in the window. In code:

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

    }

2番目の方法

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 に答える