在wpf中监视mousemove事件 [英] monitor mousemove event in wpf

查看:1082
本文介绍了在wpf中监视mousemove事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在尝试监视鼠标在应用程序窗口上的移动
这样,每当鼠标移动时,其坐标就会反映在两个文本控件中.
是否可以仅在XAML中执行此操作(没有代码)?
感谢

解决方案

Pete是正确的,但是如果您确实坚持要避免代码,则可以这样做.编写一个自定义类ONCE,然后在XAML中使用该类.这样,您的实际UI大部分仍然是免费的.这是一个快速汇总的示例(您可能需要对其进行清理和修饰).

第一步是创建一个类,例如:

  public   class  MousePosition:DependencyObject
{
    [DllImport(" )]
    [返回:MarshalAs(UnmanagedType.Bool)]
    静态 外部 布尔 GetCursorPos(参考 NativePoint pt);

    [StructLayout(LayoutKind.Sequential)]
    内部 结构 NativePoint
    {
        公共  int  X;
        公共  int  Y;
    };

    公共 静态点GetCurrentMousePosition()
    {
        NativePoint nativePoint =  NativePoint();
        GetCursorPos( ref  nativePoint);
        返回  Point(nativePoint.X,nativePoint.Y);
    }

    私有调度程序分派器;

    计时器timer =  Timer( 100 );

    公共 MousePosition()
    {
        调度程序= Application.Current.MainWindow.Dispatcher;
        timer.Elapsed == timer_Elapsed;
        timer.Start();
    }

    无效 timer_Elapsed(对象发​​送者,ElapsedEventArgs e)
    {
        点电流= GetCurrentMousePosition();
        .CurrentPosition=当前;
    }

    公共点CurrentPosition
    {
        获取 {返回(点)GetValue(CurrentPositionProperty); }

        设置
        {
            dispatcher.Invoke((Action)(()= > 
              SetValue(CurrentPositionProperty,)));
        }
    }

    公共 静态 只读 DependencyProperty CurrentPositionProperty
      = DependencyProperty.Register(
        "  typeof (点) , typeof (MousePosition));
} 



现在在XAML中使用它:

 <   Window.Resources  > 
  <   local:MousePosition     x:Key   ="    / > 
<  /Window.Resources  > 

<  网格 > 
  <   StackPanel     ="  <   TextBlock  > 鼠标位置: <  /TextBlock  > 
      <   TextBlock  
 
                     span>   文本  ="  {Binding Source = {StaticResource mousePosition},Path = CurrentPosition}" 
                     span>   宽度  ="  360" / <  /StackPanel  > 
<  /Grid  >  


在XAML中无法完全做到这一点.基本上,获取鼠标位置的方法取决于跟踪鼠标移动事件,并使用GetPosition方法转换该值以获取该点.您的选择是:

a)在传统的C#类中使用代码
b)使用x:Code将代码嵌入XAML
c)使用 AvalonLambda [ 解决方案

Pete is right but if you are really insistant on avoiding code, you could do this. Write a custom class ONCE, and then use that class in XAML. This way your actual UI remains mostly code-behind free. Here''s a quickly put together example (you may need to clean and polish this up).

First step is to create a class such as:

public class MousePosition : DependencyObject
{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(ref NativePoint pt);

    [StructLayout(LayoutKind.Sequential)]
    internal struct NativePoint
    {
        public int X;
        public int Y;
    };

    public static Point GetCurrentMousePosition()
    {
        NativePoint nativePoint = new NativePoint();
        GetCursorPos(ref nativePoint);
        return new Point(nativePoint.X, nativePoint.Y);
    }

    private Dispatcher dispatcher;

    Timer timer = new Timer(100);

    public MousePosition()
    {
        dispatcher = Application.Current.MainWindow.Dispatcher;
        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Point current = GetCurrentMousePosition();
        this.CurrentPosition = current;
    }

    public Point CurrentPosition
    {
        get { return (Point)GetValue(CurrentPositionProperty); }

        set
        {
            dispatcher.Invoke((Action)(() =>
              SetValue(CurrentPositionProperty, value)));
        }
    }

    public static readonly DependencyProperty CurrentPositionProperty
      = DependencyProperty.Register(
        "CurrentPosition", typeof(Point), typeof(MousePosition));
}



Now use it in XAML :

<Window.Resources>
  <local:MousePosition x:Key="mousePosition" />
</Window.Resources>

<Grid>
  <StackPanel Orientation="Horizontal">
      <TextBlock>Mouse Position:</TextBlock>
      <TextBlock

        Text="{Binding Source={StaticResource mousePosition}, Path=CurrentPosition}"

        Width="360" />
  </StackPanel>
</Grid>


There is no way to do this entirely in the XAML. Basically, the method to get the mouse position depends on tracking the mouse move event, and converting the value using the GetPosition method to get the point. Your options are to:

a) Use code in a traditional C# class
b) Use x:Code to embed code in the XAML
c) Use the
AvalonLambda[^] converter extension.


这篇关于在wpf中监视mousemove事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆