Routed Input Enents(1)译
wpf程序展示了鼠标捕捉和许多鼠标事件。
效果图如下:左键可以画圆,右键可以拖动,滚轮可以改变颜色

代码
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 WpfApplication4
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
       
//绘图相关的字段
bool isDrawing;
Ellipse elips;
Point ptCenter;
//拖拽相关的字段
bool isDragging;
FrameworkElement elDragging;
Point ptMouseStart, ptElementStart;
public Window1()
{
InitializeComponent();
Title = "draw circles";
        
           
}
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new Window1());
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
if (isDragging)
return;
//创建一个新的ellipse对象,并加入canvas中
ptCenter = e.GetPosition(canv);
label1.Content ="根据鼠标左键点击获得"+ ptCenter.X.ToString() +"//" +ptCenter.Y.ToString();
elips = new Ellipse();
elips.Stroke = SystemColors.WindowBrush;
elips.StrokeThickness = 1;
elips.Width = 10;
elips.Height =10;
canv.Children.Add(elips);
Canvas.SetLeft(elips,ptCenter.X);
Canvas.SetTop(elips,ptCenter.Y);
CaptureMouse();
isDrawing = true;
}
protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
{
base.OnMouseRightButtonDown(e);
if (isDrawing)
{
return;
}
ptMouseStart = e.GetPosition(canv);
label2.Content = "根据鼠标右键获取鼠标初始值" + ptMouseStart.X.ToString() + "//" + ptMouseStart.Y;
elDragging = canv.InputHitTest(ptMouseStart) as FrameworkElement;
if (elDragging != null)
{
ptElementStart = new Point(Canvas.GetLeft(elDragging),Canvas.GetTop(elDragging));
// label3.Content = "获取元素的初始位置" + ptElementStart.X + "//" + ptElementStart.Y;
label3.Content = "获取元素的初始位置" + Canvas.GetLeft(elDragging).ToString() + "//" + Canvas.GetTop(elDragging).ToString();
isDragging = true;
}
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.ChangedButton == MouseButton.Middle)
{
Shape shape = canv.InputHitTest(e.GetPosition(canv)) as Shape;
if (shape != null)
{
shape.Fill=(shape.Fill==Brushes.Red?Brushes.Transparent:Brushes.Red);
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
Point ptMouse = e.GetPosition(canv);
//改变椭圆的位置和尺寸
if (isDrawing)
{
double dRadius = Math.Sqrt(Math.Pow(ptCenter.X-ptMouse.X,2)+Math.Pow(ptCenter.Y-ptMouse.Y,2));
Canvas.SetLeft(elips,ptCenter.X-dRadius);
Canvas.SetTop(elips,ptCenter.Y-dRadius);
elips.Width = 2 * dRadius;
elips.Height = 2 * dRadius;
               
}
else if (isDragging)
{
Canvas.SetLeft(elDragging,ptElementStart.X+ptMouse.X- ptMouseStart.X);
Canvas.SetTop(elDragging,ptElementStart.Y+ptMouse.Y-ptElementStart.Y);
label4.Content = ptElementStart.Y.ToString() +"//"+ ptMouse.Y.ToString() +"//"+ ptElementStart.Y.ToString();
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
//结束绘图操作
if (isDrawing && e.ChangedButton == MouseButton.Left)
{
elips.Stroke = Brushes.Blue;
elips.StrokeThickness = 2;// Math.Min(24,elips.Width/2);
elips.Fill = Brushes.Red;
isDrawing = false;
ReleaseMouseCapture();
}
// 结束捕捉操作
else if (isDragging && e.ChangedButton == MouseButton.Right)
{
isDragging = false;
}
}
protected override void OnTextInput(TextCompositionEventArgs e)
{
base.OnTextInput(e);
//接下escape,结束绘图或拖拽
if(e.Text.IndexOf('\x1B')!=-1)
{
if (isDrawing)
ReleaseMouseCapture();
else if (isDragging)
{
Canvas.SetLeft(elDragging,ptElementStart.X);
Canvas.SetTop(elDragging,ptElementStart.Y);
isDragging = false;
}
}
}
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
//不正常结束绘图,将此ellipse从孩子中删除
if (isDrawing)
{
canv.Children.Remove(elips);
isDrawing = false;
}
}
        
     
}
}
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 WpfApplication4
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
//绘图相关的字段
bool isDrawing;
Ellipse elips;
Point ptCenter;
//拖拽相关的字段
bool isDragging;
FrameworkElement elDragging;
Point ptMouseStart, ptElementStart;
public Window1()
{
InitializeComponent();
Title = "draw circles";
}
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new Window1());
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
if (isDragging)
return;
//创建一个新的ellipse对象,并加入canvas中
ptCenter = e.GetPosition(canv);
label1.Content ="根据鼠标左键点击获得"+ ptCenter.X.ToString() +"//" +ptCenter.Y.ToString();
elips = new Ellipse();
elips.Stroke = SystemColors.WindowBrush;
elips.StrokeThickness = 1;
elips.Width = 10;
elips.Height =10;
canv.Children.Add(elips);
Canvas.SetLeft(elips,ptCenter.X);
Canvas.SetTop(elips,ptCenter.Y);
CaptureMouse();
isDrawing = true;
}
protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
{
base.OnMouseRightButtonDown(e);
if (isDrawing)
{
return;
}
ptMouseStart = e.GetPosition(canv);
label2.Content = "根据鼠标右键获取鼠标初始值" + ptMouseStart.X.ToString() + "//" + ptMouseStart.Y;
elDragging = canv.InputHitTest(ptMouseStart) as FrameworkElement;
if (elDragging != null)
{
ptElementStart = new Point(Canvas.GetLeft(elDragging),Canvas.GetTop(elDragging));
// label3.Content = "获取元素的初始位置" + ptElementStart.X + "//" + ptElementStart.Y;
label3.Content = "获取元素的初始位置" + Canvas.GetLeft(elDragging).ToString() + "//" + Canvas.GetTop(elDragging).ToString();
isDragging = true;
}
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.ChangedButton == MouseButton.Middle)
{
Shape shape = canv.InputHitTest(e.GetPosition(canv)) as Shape;
if (shape != null)
{
shape.Fill=(shape.Fill==Brushes.Red?Brushes.Transparent:Brushes.Red);
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
Point ptMouse = e.GetPosition(canv);
//改变椭圆的位置和尺寸
if (isDrawing)
{
double dRadius = Math.Sqrt(Math.Pow(ptCenter.X-ptMouse.X,2)+Math.Pow(ptCenter.Y-ptMouse.Y,2));
Canvas.SetLeft(elips,ptCenter.X-dRadius);
Canvas.SetTop(elips,ptCenter.Y-dRadius);
elips.Width = 2 * dRadius;
elips.Height = 2 * dRadius;
}
else if (isDragging)
{
Canvas.SetLeft(elDragging,ptElementStart.X+ptMouse.X- ptMouseStart.X);
Canvas.SetTop(elDragging,ptElementStart.Y+ptMouse.Y-ptElementStart.Y);
label4.Content = ptElementStart.Y.ToString() +"//"+ ptMouse.Y.ToString() +"//"+ ptElementStart.Y.ToString();
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
//结束绘图操作
if (isDrawing && e.ChangedButton == MouseButton.Left)
{
elips.Stroke = Brushes.Blue;
elips.StrokeThickness = 2;// Math.Min(24,elips.Width/2);
elips.Fill = Brushes.Red;
isDrawing = false;
ReleaseMouseCapture();
}
// 结束捕捉操作
else if (isDragging && e.ChangedButton == MouseButton.Right)
{
isDragging = false;
}
}
protected override void OnTextInput(TextCompositionEventArgs e)
{
base.OnTextInput(e);
//接下escape,结束绘图或拖拽
if(e.Text.IndexOf('\x1B')!=-1)
{
if (isDrawing)
ReleaseMouseCapture();
else if (isDragging)
{
Canvas.SetLeft(elDragging,ptElementStart.X);
Canvas.SetTop(elDragging,ptElementStart.Y);
isDragging = false;
}
}
}
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
//不正常结束绘图,将此ellipse从孩子中删除
if (isDrawing)
{
canv.Children.Remove(elips);
isDrawing = false;
}
}
}
}
                    
                

                
            
        
浙公网安备 33010602011771号