Note that there are some explanatory texts on larger screens.

plurals
  1. POmouse movements in wpf
    primarykey
    data
    text
    <p>Is there a way to use a mouse click and drag in wpf? I have disabled the borders on my app within the xaml code but in the code behind im trying to do something like this:</p> <pre><code>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 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; 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); } </code></pre> <p>Basically because my borders and title bar is disabled this code should allow control of the application anywhere where i click.</p> <p>I just cant get it to work because this is based on windows forms. </p> <p><strong>EDIT</strong></p> <p>If I try this I get a definition error but im unsure how else to handle it:</p> <pre><code>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); } </code></pre> <p><strong>RE EDIT</strong></p> <p>So I have now tryed a new method as mentioned below but still nothing.</p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload