我如何使用CaptureMouse或Mouse.Capture在我的C#WPF应用程序? [英] How do I use CaptureMouse or Mouse.Capture in my C# WPF application?

查看:2272
本文介绍了我如何使用CaptureMouse或Mouse.Capture在我的C#WPF应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够访问该鼠标光标是否在我的应用程序的窗口的坐标。



当我使用Mouse.Capture( IInputElement)或UIElement.CaptureMouse(),既无法捕捉鼠标和返回false。



什么可能是我的问题?



我的窗口CS文件如下:

 使用System.Windows; 
使用System.Windows.Input;

命名空间ScreenLooker
{
///<总结> $ B $为Window1.xaml
///< b ///交互逻辑; /总结>
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent();

布尔bSuccess = Mouse.Capture(本);
bSuccess = this.CaptureMouse();
}

保护覆盖无效的OnMouseMove(MouseEventArgs E)
{
tbCoordX.Text = e.GetPosition(本).X.ToString();
tbCoordY.Text = e.GetPosition(本).Y.ToString();
//System.Drawing.Point oPoint = System.Windows.Forms.Cursor.Position;
//tbCoordX.Text = oPoint.X.ToString();
//tbCoordY.Text = oPoint.Y.ToString();

base.OnMouseMove(E);
}
}
}


解决方案

>传递给必须可见已启用



尝试把Mouse.Capture在加载事件处理程序,例如:



在XAML:

 <窗​​口... ..标题=我的窗口加载=Window_Loaded> 

< /窗GT;

在代码:

 私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
变种b = Mouse.Capture(本);
}



我之前没有捕捉整个窗口,所以不知道它是如何将工作。 ,它的典型用法是




  1. 的MouseDown: - 电话 Mouse.Capture()在子控件

  2. 的MouseMove: - 过程X和鼠标ŸCOORDS

  3. 的MouseUp: - 调用 Mouse.Capture( NULL)来清除鼠标事件捕获。


I want to be able to access the coordinates of the mouse whether or not the cursor is over the window of my application.

When I use Mouse.Capture(IInputElement) or UIElement.CaptureMouse(), both fail to capture the mouse and return false.

What might be my problem?

The cs file for my window is as follows:

using System.Windows;
using System.Windows.Input;

namespace ScreenLooker
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            bool bSuccess = Mouse.Capture(this);
            bSuccess = this.CaptureMouse();
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            tbCoordX.Text = e.GetPosition(this).X.ToString();
            tbCoordY.Text = e.GetPosition(this).Y.ToString();
            //System.Drawing.Point oPoint = System.Windows.Forms.Cursor.Position;
            //tbCoordX.Text = oPoint.X.ToString();
            //tbCoordY.Text = oPoint.Y.ToString();

            base.OnMouseMove(e);
        }
    }
}

解决方案

The control passed to Mouse.Capture() needs to be Visible and Enabled.

Try putting the Mouse.Capture in the Loaded event handler, e.g.

In XAML:

<Window ... .. .. Title="My Window" loaded="Window_Loaded">
...
</Window>

In Code:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  var b = Mouse.Capture(this);
}

I've not captured the whole window before, so not sure about how it will work. The typical usage of it is.

  1. MouseDown:- call Mouse.Capture() on child control
  2. MouseMove:- Process X and Y coords of mouse
  3. MouseUp:- call Mouse.Capture(null) to clear mouse event capture.

这篇关于我如何使用CaptureMouse或Mouse.Capture在我的C#WPF应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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