Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic How to show the selected node in center of the diagram? (Read 1844 times)
Joost
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 2
Joined: Jun 18th, 2020
How to show the selected node in center of the diagram?
Jun 28th, 2020 at 3:26pm
Print Post  
Hi, does anybody know how to show the selected node in the center of the diagram?
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: How to show the selected node in center of the diagram?
Reply #1 - Jun 29th, 2020 at 6:35am
Print Post  
Hi,

1) If you want to show the selected node in the center of the current viewport, use the following code:

Code
Select All
diagram.NodeSelected += (s, e) =>
{
  var node = e.Node;
  var dpi = 96; // 96 dpi used just for testing, you'll want the actual device dpi when using this conversion
  var graphics = diagram.CreateMeasureGraphics();
  var viewportHeight = MeasureUnit.Inch.Convert(diagramView.Height / dpi, diagram.MeasureUnit, graphics);
  var viewportWidth = MeasureUnit.Inch.Convert(diagramView.Width / dpi, diagram.MeasureUnit, graphics);

  diagram.ScrollX = node.Bounds.X - viewportWidth / 2 + node.Bounds.Width / 2;
  diagram.ScrollY = node.Bounds.Y - viewportHeight / 2 + node.Bounds.Height / 2;
}; 



Please note, that the view will scroll only within the actual diagram bounds, so if the selected node is close to the top or left edge of the document, it won't show in the center. To make sure that doesn't happen, you may want to increase the diagram bounds to ensure enough space between the nodes and the edges.

2) If you wish to actually move the node to the center of the document, and not just set the viewport on it, set its Bounds or use the Move method:
Code
Select All
node.Move(diagram.Bounds.Width / 2 - node.Bounds.Width / 2,
    diagram.Bounds.Height / 2 - node.Bounds.Height / 2); 



Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint