将矩形传递给事件函数 [英] pass rectangle into a events function
本文介绍了将矩形传递给事件函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个函数中创建了一个矩形
现在我想把这个矩形传递给一个事件函数
我该怎么做?
我创建的示例代码:
I created a rectangle within a function
Now I want to pass this rectangle into a function of events
How can I do this?
Sample code I created:
private void button2_Checked(object sender, RoutedEventArgs e)
{
//create new rectengle
Rectangle rect = new Rectangle();
rect.Visibility = Visibility.Visible;
rect.Width = slider1.Value;
rect.Height = slider2.Value;
SolidColorBrush myBrush = new SolidColorBrush(Colors.Green);
rect.Stroke = Brushes.Red;
rect.StrokeThickness = 1;
rect.Fill = myBrush;
// Creat event to the new rect
rect.MouseLeftButtonDown += rect_MouseLeftButtonDown;
rect.MouseLeftButtonUp += rect_MouseLeftButtonUp;
rect.MouseMove += rect_MouseMove;
myCanvas.Children.Insert(0, rect);
}
private bool _isRectDragInProg;
// one of the event function
//Function is called but the value rectangle unidentified
private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_isRectDragInProg = true;
//Here I want to pass the rectangle
rect.CaptureMouse();
}
推荐答案
嗨会员,
所以你的问题是如何在eventhandler方法中获得对clicked(draged ...)矩形的引用?这很容易 - 你会通过sender参数得到它:
Hi Member,
So your Problem is just how to obtain a reference to the clicked (draged...) rectangle in the eventhandler method? This is easy - you will get it through the sender parameter like this:
Rectangle rect = sender as Rectangle;
或者你的意思是什么?
Or did you mean something else?
这篇关于将矩形传递给事件函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文