簡體   English   中英

WPF中的鼠標移動

[英]mouse movements in wpf

有沒有一種方法可以使用鼠標單擊並拖動WPF? 我在xaml代碼中禁用了我的應用程序的邊框,但是在im試圖執行以下操作的代碼中:

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

在您后面的窗口代碼中,重寫OnMouseDown和/或OnPreviewMouseDown

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

其次,檢查MouseButtonEventArgs的ChangedButton屬性以確定按下了哪個Button。 公共枚舉MouseButton(左,中,右,XButton1,XButton2}相關的命名空間是System.Windows.Input; System.Windows.Forms不正確。

您的窗口不應該訂閱以前建議的自己的事件。 使用替代。 this.MouseDown + =新的MouseButtonEventHandler(MainWindow_OnMouseDown); //不正確

除了其他答案,您還可以在后面的代碼中注冊類似事件:

  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">

相應的WPF事件位於System.Windows.Input命名空間中 具體而言,為MouseDown附加事件MouseUp附加事件MouseMove附加事件 要找出按下了哪個按鈕,請使用MouseButtonEventArgsChangedButton屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM