Get the node clicked
-
18-06-2021 - |
题
I have the code in C# for getting node clicked:
public Node ChartMouseDownFindNode(Graphics graphics, Font font, Point mousePosition)
{
this.mousePosition = Cursor.Position;
Pen pen = new Pen(Color.FromArgb(255, 124, 176, 34), 2);
foreach (Node node in Nodes)
{
Size nodeSize = node.GetNodeSize(graphics, font);
if (node.Position.X < mousePosition.X + Math.Abs(mousePosition.X) && node.Position.X + nodeSize.Width > mousePosition.X + Math.Abs(mousePosition.X))
if (node.Position.Y < mousePosition.Y + Math.Abs(mousePosition.Y) && node.Position.Y + nodeSize.Height > mousePosition.Y + Math.Abs(mousePosition.Y))
return node;
MessageBox.Show("clicked");
}
return null;
}
It does not work. I guess there is something wrong with the cursor. My idea is to compare node's position with mouse's position. THanks a lot!
解决方案
I think the code of your friend is wrong. These if statements will be better.
if (node.Position.X < mousePosition.X && node.Position.X + nodeSize.Width > mousePosition.X)
if (node.Position.Y < mousePosition.Y && node.Position.Y + nodeSize.Height > mousePosition.Y)
And before this statements, you may need to match position coordinates. Cursor.Position is in screen space coordinate but nodes might be in client space coordinate, I guess. This code will work.
mousePosition = PointToClient(mousePosition);
不隶属于 StackOverflow