有人可以帮我解决这个问题:如何将数字划分为字段,因此根据鼠标点击哪个区域会进行特定事件?
private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//if (!isDragging)
{
//creating of my user control element
NodePicture node = new NodePicture();
node.Width = 100;
node.Height = 100;
//use cursor position as the center of the figure
Point point = e.GetPosition(this);
node.SetValue(Canvas.TopProperty, point.Y - node.Height / 2);
node.SetValue(Canvas.LeftProperty, point.X - node.Width / 2);
node.MouseLeftButtonDown += controlReletionshipsLine;
LayoutRoot.Children.Add(node);
}
}
private void controlReletionshipsLine(object sender, MouseButtonEventArgs e)
{
//creating parant element of node
ParentNode parentNode = new ParentNode();
//creating connected element of the node
ConnectedNode connectedNode = new ConnectedNode();
//creating node element
NodePicture node = (NodePicture)sender;
//getting the relative position of the element
Point point = e.GetPosition(this);
答案 0 :(得分:3)
你可以用数学方式划分对象,使用鼠标位置“相对于对象”来决定你点击的位置,或者你可以叠加多个多边形,每个多边形的颜色alpha通道设置为1%(所以他们可以进行重击测试,但不可见。
由于您只想查看单击的圆圈的四分之一,请在LeftMouseButtonDown事件args上调用GetPosition,将控件本身作为参数传递。这将返回一个Point对象,其位置相对于控件的左上角。
然后只是看看它在哪个季度:
private void ControlX_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get the relative position of the element
Point point = e.GetPosition(sender as UIElement);
if (point.X > control.Width/2)
{
if (point.Y > control.Height/2)
{
// You are in the bottom right quarter
}
else
{
// You are in the top right quarter
}
}
else
{
if (point.Y > control.Height/2)
{
// You are in the bottom left quarter
}
else
{
// You are in the top left quarter
}
}
}
在您发送给我的示例代码中(controlReletionshipsLine
):
// getting the relative position of the element
Point point = e.GetPosition(this);
应该是:
// getting the relative position of the element
Point point = e.GetPosition(sender as UIElement);