用鼠标在窗体上移动椭圆(Move ellipse on form by mouse)

编程入门 行业动态 更新时间:2024-10-24 22:23:50
用鼠标在窗体上移动椭圆(Move ellipse on form by mouse)

如何在WPF中通过鼠标在窗口上移动椭圆。

private void ellipse_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { Ellipse ellipse = sender as Ellipse; if (ellipse != null && e.LeftButton == MouseButtonState.Pressed) { DragDrop.DoDragDrop(ellipse, ellipse.Fill.ToString(), System.Windows.DragDropEffects.Copy); } }

如何创建方法ellipse_MouseClick ?

How move ellipse by mouse on window in WPF.

private void ellipse_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { Ellipse ellipse = sender as Ellipse; if (ellipse != null && e.LeftButton == MouseButtonState.Pressed) { DragDrop.DoDragDrop(ellipse, ellipse.Fill.ToString(), System.Windows.DragDropEffects.Copy); } }

How to create method ellipse_MouseClick?

最满意答案

Ellipses上没有MouseClick事件,但是有MouseDown和MouseUp事件。 我假设你正在寻找这样的东西。

WPF:

<Window x:Class="WpfApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="350" Width="525" MouseMove="Any_MouseMove" > <Canvas> <Ellipse Fill="Lavender" Height="100" Width="100" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" MouseMove="Any_MouseMove" /> </Canvas> </Window>

代码背后:

using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WpfApplication { public partial class MainWindow : Window { private UIElement _lastClickedUIElement; private Point? _clickOffset; public MainWindow() { InitializeComponent(); } private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _lastClickedUIElement = sender as UIElement; _clickOffset = e.GetPosition(_lastClickedUIElement); } private void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { _lastClickedUIElement = null; } private void Any_MouseMove(object sender, MouseEventArgs e) { if (_lastClickedUIElement == null) return; _lastClickedUIElement.SetValue(Canvas.LeftProperty, e.GetPosition(this).X - _clickOffset.Value.X); _lastClickedUIElement.SetValue(Canvas.TopProperty, e.GetPosition(this).Y - _clickOffset.Value.Y); } } }

单击圆圈可以移动它。 只要您为这些方法提供这些方法,这将适用于任何UI元素。 随意添加一个矩形到画布。

<Rectangle Fill="Lavender" Height="100" Width="100" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" MouseMove="Any_MouseMove" />

There is no MouseClick event on Ellipses, however there is the MouseDown and MouseUp events. I assume you are looking for something like this.

WPF:

<Window x:Class="WpfApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="350" Width="525" MouseMove="Any_MouseMove" > <Canvas> <Ellipse Fill="Lavender" Height="100" Width="100" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" MouseMove="Any_MouseMove" /> </Canvas> </Window>

Code Behind:

using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WpfApplication { public partial class MainWindow : Window { private UIElement _lastClickedUIElement; private Point? _clickOffset; public MainWindow() { InitializeComponent(); } private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _lastClickedUIElement = sender as UIElement; _clickOffset = e.GetPosition(_lastClickedUIElement); } private void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { _lastClickedUIElement = null; } private void Any_MouseMove(object sender, MouseEventArgs e) { if (_lastClickedUIElement == null) return; _lastClickedUIElement.SetValue(Canvas.LeftProperty, e.GetPosition(this).X - _clickOffset.Value.X); _lastClickedUIElement.SetValue(Canvas.TopProperty, e.GetPosition(this).Y - _clickOffset.Value.Y); } } }

Click on the circle to move it around. This will work on any UI element as long as long as you give them those methods. feel free to add a Rectangle to the canvas also.

<Rectangle Fill="Lavender" Height="100" Width="100" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" MouseMove="Any_MouseMove" />

更多推荐

ellipse,Windows,System,How,电脑培训,计算机培训,IT培训"/> <meta name="de

本文发布于:2023-08-04 07:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   椭圆   用鼠标   Move   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!