【问题标题】:mouse movements in wpfwpf中的鼠标移动
【发布时间】:2012-03-22 20:48:05
【问题描述】:

有没有办法在 wpf 中使用鼠标单击和拖动?我在 xaml 代码中禁用了我的应用程序的边框,但在我试图做这样的事情背后的代码中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Size? _mouseGrabOffset;


        public MainWindow()
        {
            InitializeComponent(); 
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
                _mouseGrabOffset = new Size(e.Location);
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                _mouseGrabOffset = new Size(e.Location);
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            _mouseGrabOffset = null;

            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (_mouseGrabOffset.HasValue)
            {
                this.Location = Cursor.Position - _mouseGrabOffset.Value;
            }

            base.OnMouseMove(e);
        }

基本上,因为我的边框和标题栏被禁用,这段代码应该允许在我点击的任何地方控制应用程序。

我只是无法让它工作,因为这是基于 Windows 窗体的。

编辑

如果我尝试这个,我会得到一个定义错误,但我不确定如何处理它:

public partial class MainWindow : Window
{
    private Size? _mouseGrabOffset;


    public MainWindow()
    {
        InitializeComponent();
        this.MouseDown += new MouseButtonEventHandler(MainWindow_OnMouseDown);

    }
    void MainWindow_OnMouseDown(object sender, MouseEventArgs e)
    {
        if (e.LeftButton != MouseButtonState.Pressed)
            _mouseGrabOffset = new Size(e.Location);
            base.OnMouseMove(e);
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (_mouseGrabOffset.HasValue)
        {
            this.Location = Cursor.Position - _mouseGrabOffset.Value;
        }

        base.OnMouseMove(e);
    }

重新编辑

所以我现在尝试了一种如下所述的新方法,但仍然没有。

public partial class MainWindow : Window
{
    //private Size? _mouseGrabOffset;


    public MainWindow()
    {
        InitializeComponent();
        //this.MouseDown += new MouseButtonEventHandler(MainWindow_OnMouseDown);

    }
    public enum MouseButtonType 
    {
        Left,
        Middle,
        Right,
        XButton1,
        XButton2,

    }
    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
    }
    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseDown(e);
    }

【问题讨论】:

  • 为什么用 asp.net 标记这个?
  • MouseEventArgs 对于MainWindow_OnMouseDown 应该是MouseButtonEventArgs

标签: c# wpf


【解决方案1】:

在你背后的 Window 代码中覆盖 OnMouseDown 和/或 OnPreviewMouseDown

protected override void OnMouseDown(MouseButtonEventArgs e)
{
    base.OnMouseDown(e);
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
    base.OnPreviewMouseDown(e);
}

其次,检查 MouseButtonEventArgs ChangedButton 属性以确定按下了哪个按钮。 公共枚举 MouseButton(左、中、右、XButton1、XButton2} 相关的命名空间是 System.Windows.Input; System.Windows.Forms 不正确。

您的窗口不应像之前建议的那样订阅自己的事件。使用覆盖。 this.MouseDown += new MouseButtonEventHandler(MainWindow_OnMouseDown); // 不正确

【讨论】:

    【解决方案2】:

    除了其他答案之外,您还可以在后面的代码中注册这样的事件:

      public MainWindow()
      {
          InitializeComponent(); 
          this.MouseDown += new MouseButtonEventHandler(MainWindow_MouseDown);
      }
      void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
      { 
            //handle it
      }
    

    或在 XAML 中注册:

    <Window x:Class="WpfApplication1.MainWindow"
            ...
            MouseDown="Window_MouseDown">
    

    【讨论】:

      【解决方案3】:

      对应的 WPF 事件在System.Windows.Input namespace 中。具体来说,MouseDown attached eventMouseUp attached eventMouseMove attached event。要找出按下了哪个按钮,请使用 MouseButtonEventArgsChangedButton property

      【讨论】:

        猜你喜欢
        • 2023-03-22
        • 1970-01-01
        • 2016-02-23
        • 2016-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        相关资源
        最近更新 更多