Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Zoom function (Read 6536 times)
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Zoom function
Jan 10th, 2011 at 4:00pm
Print Post  
Hi, i'm trying to write some functions to allow user to create a box that is used in diag_view.FitToRect(zoom_box).
But for some reasons my box doesn't follow exactly the mouse cursor ...
Does someone could help please ?

there is the code :

First we save the first clic emplacement :
Code
Select All
	   private void diag_view_MouseDown(object sender, MouseEventArgs e) // initialise le coin suppérieur gauche du rectangle
		{
		if (e.Button == MouseButtons.Left && !isMenuClic)
			{
		    start_screen_position = Control.MousePosition;
		    start_diag_position = diag_view.PointToClient(start_screen_position);

		    if (debug)
		    {
			  fd.start_px.Text = start_diag_position.X.ToString();
			  fd.start_py.Text = start_diag_position.Y.ToString();
		    }
			}
		}

 



Secondly the mouse mouse event permits to create the zoom_box
Code
Select All
  private void diag_view_MouseMove(object sender, MouseEventArgs e) //dessine un rectangle transparent

 {

     if (zoom_node != null)


   diag_component.Nodes.Remove(zoom_node);


     if ((e.Button == System.Windows.Forms.MouseButtons.Left) && (!isMenuClic))


{


    PointF current_diag_position = diag_view.PointToClient(Control.MousePosition);



    float width = 0; float height = 0;


    if (current_diag_position.X > start_diag_position.X)


    {



  if (current_diag_position.Y > start_diag_position.Y)



  {




width = current_diag_position.X - start_diag_position.X;




height = current_diag_position.Y - start_diag_position.Y;








if (debug)




    fd.dtp.Text = diagToPixel.ToString();





RectangleF zoom_box = new RectangleF();




zoom_box.Width = width/diagToPixel;




zoom_box.Height = height/diagToPixel;





zoom_box.X = start_diag_position.X / diagToPixel + diag_view.ScrollX;




zoom_box.Y = start_diag_position.Y / diagToPixel + diag_view.ScrollY;





zoom_node = diag_component.Factory.CreateShapeNode(zoom_box);




zoom_node.Shape = Shapes.Rectangle;




zoom_node.Pen = new Pen(Color.LightGray, 0);




zoom_node.Brush = new SolidBrush(Color.Transparent);




zoom_node.Expandable = false;




zoom_node.Text = "Sélectionnez la zone de zoom";





if (debug)




{




    fd.box_x.Text = zoom_box.X.ToString();




    fd.box_y.Text = zoom_box.Y.ToString();




    fd.box_w.Text = zoom_box.Width.ToString();




    fd.box_h.Text = zoom_box.Height.ToString();




    fd.node_x.Text = zoom_node.Bounds.X.ToString();




    fd.node_y.Text = zoom_node.Bounds.Y.ToString();




    fd.node_w.Text = zoom_node.Bounds.Width.ToString();




    fd.node_h.Text = zoom_node.Bounds.Height.ToString();




}



  }


    }


}


if (debug)


{


    fd.tb_current_mp_x.Text = diag_view.PointToClient(Control.MousePosition).X.ToString();


    fd.tb_current_mp_y.Text = diag_view.PointToClient(Control.MousePosition).Y.ToString();


}

  }
 



Thirdly the mouse up event does zoom

Code
Select All
 public void ApplyDiagToPixel(bool zoom)

 {

     RectangleF visibleRegion = diag_view.ClientToDoc(diag_view.ClientRectangle);

     float visible_Width = visibleRegion.Width;

     float visible_Height = visibleRegion.Height;

     if (debug)

     {


   fd.visible_diag_h.Text = visible_Height.ToString();


   fd.visible_diag_w.Text = visible_Width.ToString();

     }


     RectangleF fullRegion = diag_view.Bounds;

     if (debug)

     {


   fd.full_diag_h.Text = fullRegion.Height.ToString();


   fd.full_diag_w.Text = fullRegion.Width.ToString();

     }


     float facteur_X = visible_Width / fullRegion.Width;

     float facteur_Y = visible_Height / fullRegion.Height;


     float scale_factor;

     if (facteur_X > facteur_Y)

     {


   scale_factor = facteur_X;

     }

     else

     {


   scale_factor = facteur_Y;

     }


     if (zoom)


   diagToPixel = diagToPixel / scale_factor;

     else


   diagToPixel = diagToPixel * scale_factor;


    if(debug)


fd.dtp.Text = diagToPixel.ToString();


 }
 



  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Zoom function
Reply #1 - Jan 10th, 2011 at 4:11pm
Print Post  
Finally every time the zoomFactor changes we vompute the "diagtopixel" variable.

Code
Select All
	 private void diag_view_ZoomFactorChanged(object sender, EventArgs e)
	 {
	     bool zoom;
	     if (save_zoomFactor > diag_view.ZoomFactor)
	     {
		   zoom = false;
	     }
	     else
	     {
		   zoom = true;
	     }
	     save_zoomFactor = diag_view.ZoomFactor;

	     ApplyDiagToPixel(zoom);
	 }
	 public void ApplyDiagToPixel(bool zoom)
	 {
	     RectangleF visibleRegion = diag_view.ClientToDoc(diag_view.ClientRectangle);
	     float visible_Width = visibleRegion.Width;
	     float visible_Height = visibleRegion.Height;
	     if (debug)
	     {
		   fd.visible_diag_h.Text = visible_Height.ToString();
		   fd.visible_diag_w.Text = visible_Width.ToString();
	     }

	     RectangleF fullRegion = diag_view.Bounds;
	     if (debug)
	     {
		   fd.full_diag_h.Text = fullRegion.Height.ToString();
		   fd.full_diag_w.Text = fullRegion.Width.ToString();
	     }

	     float facteur_X = visible_Width / fullRegion.Width;
	     float facteur_Y = visible_Height / fullRegion.Height;

	     float scale_factor;
	     if (facteur_X > facteur_Y)
	     {
		   scale_factor = facteur_X;
	     }
	     else
	     {
		   scale_factor = facteur_Y;
	     }

	     if (zoom)
		   diagToPixel = diagToPixel / scale_factor;
	     else
		   diagToPixel = diagToPixel * scale_factor;

	    if(debug)
		fd.dtp.Text = diagToPixel.ToString();

	 }

 

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Zoom function
Reply #2 - Jan 10th, 2011 at 4:24pm
Print Post  
Hi,

I can't see ClientToDoc being called to get the diagram coordinates of the mouse pointer, so I suppose you are using the view-relative pixel coordinates as values of the zoom-node Bounds.

You can see an alternative way to implement a zoom tool here:
http://mindfusion.eu/Forum/YaBB.pl?board=fcnet_disc;action=display;num=129079283...

You can find several other methods to do the same if you search the forum for ZoomToRect.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Zoom function
Reply #3 - Jan 11th, 2011 at 7:37am
Print Post  
Thanks for this example ... I solved my problem !
One more question; Is it possible to customise  the zoom rectangle by using ZoomTool class ?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Zoom function
Reply #4 - Jan 11th, 2011 at 8:29am
Print Post  
Hi,

ZoomTool uses the Selection object to display a zoom rectangle, so you could customize it through the Pen and Brush properties of diagram.Selection. If you need to show additional graphics, you could custom-draw them from a DrawForeground handler, or use a temporary node as a zoom rect instead of Selection.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Zoom function
Reply #5 - Jan 11th, 2011 at 9:05am
Print Post  
Hi,

public override InteractionState StartDraw(PointF point)
allow to customise the box.

Thanks again.
  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Zoom function
Reply #6 - Jan 11th, 2011 at 9:26am
Print Post  
hmmm ... any way to custome text of rectangle ?
i tryed to cast it to ShapeNode but it's not possible...

i just would the same thing :


Code
Select All
zoom_node = diag_component.Factory.CreateShapeNode(num_box);
zoom_node.Shape = Shapes.Rectangle;
zoom_node.Pen = new Pen(Color.LightGray, 0);
zoom_node.Brush = new SolidBrush(Color.Transparent);
zoom_node.Expandable = false;
zoom_node.Text = "Sélectionnez la zone de zoom";
 



-------------------------------
you told me about a tempoary node but how to cast it and where ?
i tryed DrawForeground handler without sucess...
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Zoom function
Reply #7 - Jan 11th, 2011 at 1:22pm
Print Post  
Try this as DrawForeground implementation:

Code
Select All
private void diagram_DrawForeground(object sender, DiagramEventArgs e)
{
	if (diagramView.CustomBehavior is ZoomTool &&
		diagram.Interaction != null)
	{
		e.Graphics.DrawString("Sélectionnez la zone de zoom",
			new Font("Arial", 12 * 100 / diagramView.ZoomFactor),
			Brushes.Black, diagram.Selection.Bounds);
	}
}
 



but also add the following to ZoomTool, or otherwise the draw event will be raised just once upon Mousedown:

Code
Select All
protected override void OnMouseMove(Point mousePosition)
{
	base.OnMouseMove(mousePosition);
	DiagramView.RecreateCacheImage();
} 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Zoom function
Reply #8 - Jan 11th, 2011 at 2:10pm
Print Post  
Thanks it works !
The following picture shows you what i wish exactly. could you help me ?
I just would like to color the background of my box in gray transparent and recenter my text.

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Zoom function
Reply #9 - Jan 11th, 2011 at 2:51pm
Print Post  
Use the DrawString overload that takes a StringFormat argument, and pass a StringFormat instance whose Alignment and LineAlignment members are set to Center.

Regarding the box background, set Selection.Brush to a SolidBrush instance with its Color set to transparent gray.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Zoom function
Reply #10 - Jan 11th, 2011 at 3:05pm
Print Post  
Thanks,

how do you do the transparency effect ? I only have Color.Gray ...
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Zoom function
Reply #11 - Jan 11th, 2011 at 3:38pm
Print Post  
For example: Color.FromArgb(100, Color.Gray);
  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Zoom function
Reply #12 - Jan 12th, 2011 at 7:17am
Print Post  
Thanks i didn't know that ...

I've got one more problem. I implemented a function mouse_wheel :
Code
Select All
if (e.Delta > 0)
{
if (diag_view.ScrollY >= 0)
diag_view.ScrollY += -5;
}
else
{

diag_view.ScrollY += +5;
}
 


"if (diag_view.ScrollY >= 0)" permits to stop wheeling on the begining but no way to stop wheeling at the end of the diagram ...

I looked at the FAQ but the attribute .DocExtents is unknown..
Code
Select All
void diagramView_MouseWheel(object sender,     MouseEventArgs args)
{
diagramView fcSender = sender as diagramView;
float newScrollY = fcSender.ScrollY - args.Delta / 50;

if (newScrollY > fcSender.DocExtents.Top)
fcSender.ScrollY = newScrollY;

}
 


  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Zoom function
Reply #13 - Jan 12th, 2011 at 7:50am
Print Post  
It seems that's from an old version. Replace DocExtents with Diagram.Bounds.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
lead8209
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 20
Joined: Aug 9th, 2010
Re: Zoom function
Reply #14 - Jan 12th, 2011 at 8:39am
Print Post  
Thanks for all...
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint