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 :
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
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
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();
}