用鼠标在窗体上移动椭圆(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
发布评论