How to put in resize mode a PictureBox created dynamically in C #?

2

Good morning.

I am creating an application in C # under VisualStudio 2013.

In it when the user clicks on an option in the context menu of the right mouse button on a Panel, I add a PictureBox to the Panel.

So far everything is perfect but the problem comes when I try to give it the functionality to resize the PictureBox because I can not put it in resizable mode (when it appears with points in the corners and in the middle of the sides) when I click on the. I have reviewed all the properties of the PictureBox object but I have not found any that allow me to do what I try.

Code where I create the PictureBox and add it to the Panel

PictureBox cuadroImagenPictureBox = new PictureBox();

cuadroImagenPictureBox.SetBounds(cuadroImagen.PosX, cuadroImagen.PosY, cuadroImagen.Ancho, cuadroImagen.Alto);
cuadroImagenPictureBox.BorderStyle = BorderStyle.FixedSingle;
cuadroImagenPictureBox.Tag = cuadroImagen;
cuadroImagenPictureBox.MouseDown += new MouseEventHandler(pulsarEnPictureBox);
cuadroImagenPictureBox.MouseMove += new MouseEventHandler(MoverEnPictureBox);

this.panelHojaActual.Controls.Add(cuadroImagenPictureBox);

Code where I collect the click event

private void pulsarEnPictureBox(object sender,  MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        location = new Point(e.X, e.Y);
    }
    else
    {
        if (e.Button == MouseButtons.Middle)
        {
            PictureBox cuadro = (PictureBox)sender;
           // -> Aqui es donde intento cambiar el PictureBox
        }
    }
}

Does anyone know where my error is or guide me where I should shoot?

Thank you very much

    
asked by MrCode 31.07.2017 в 19:46
source

1 answer

1

After much thought I decided to change the focus and instead of marking the component with the selected boxes (corners and sides) use the cursor in the form of resized arrows to indicate that it is resized.

In addition, I managed to implement the resizing in the following way:

NOTE : In my case the mouse button that does the resizing is the central one. For other buttons change MouseButtons.Middle by its corresponding value

Variables within the class inherited from Form

public partial class Principal : Form
{
   private Boolean redimensionandoCuadroImagen = false;
   private int marginRedimension = 10;
   private Size tamanoInicioCuadroImagen;
   private Rectangle rectaPunteadaApoyoRedimensionCuadroImagen = Rectangle.Empty;
   private Point puntoInicioArrastreRedimensionCuadroImagen;
   private Control objetoArrastradoCuadroImagen = null;

   ...... // Resto de variables

Method that receives the event of the click on the component (PictureBox)

private void pulsarEnPictureBox(object sender,  MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        locationCuadroImagen = new Point(e.X, e.Y);
    }
    else
    {
        if (e.Button == MouseButtons.Middle)
        {
            objetoArrastradoCuadroImagen = (Control)sender;

            // Si el click se ha hecho dentro de los margenes cercanos al perimetro del control
            if ((e.X <= marginRedimension) || (e.X >= objetoArrastradoCuadroImagen.Width - marginRedimension) ||
                (e.Y <= marginRedimension) || (e.Y >= objetoArrastradoCuadroImagen.Height - marginRedimension))
            {
                // Activamos la redimension y cambiamos el cursor
                redimensionandoCuadroImagen = true;
                this.Cursor = Cursors.SizeNWSE;

                // Tamaño inicial
                this.tamanoInicioCuadroImagen = new Size(e.X, e.Y);
                // Obtenemos la localizacion del control
                Point pt = this.PointToScreen(objetoArrastradoCuadroImagen.Location);
                // Creamos el rectangulo pestunteado y lo pintamos
                rectanguloPunteadaApoyoRedimensionCuadroImagen = new Rectangle(pt, tamanoInicioCuadroImagen);
                ControlPaint.DrawReversibleFrame(rectanguloPunteadaApoyoRedimensionCuadroImagen, this.ForeColor, FrameStyle.Dashed);
            }
            else
            {
                redimensionandoCuadroImagen = false;
                this.Cursor = Cursors.Default;
            }

            // Obtenemos el punto de inicio del arrastre
            this.puntoInicioArrastreRedimensionCuadroImagen = e.Location;
        }
    }
}

Method that receives the drag event with the mouse in the component (PictureBox)

private void moverEnPictureBox(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        if (locationCuadroImagen != Point.Empty)
        {
            PictureBox cuadro = (PictureBox)sender;

            Point newlocation = cuadro.Location;
            newlocation.X += e.X - locationCuadroImagen.X;
            newlocation.Y += e.Y - locationCuadroImagen.Y;
            cuadro.Location = newlocation;
        }
    }
    else
    {
        if (e.Button == MouseButtons.Middle)
        {
            if (objetoArrastradoCuadroImagen != null)
            {
                if (redimensionandoCuadroImagen)
                {
                    // Movemos el rectangulo pespunteado
                    if (rectanguloPunteadaApoyoRedimensionCuadroImagen.Width > 0 && rectanguloPunteadaApoyoRedimensionCuadroImagen.Height > 0)
                        ControlPaint.DrawReversibleFrame(rectanguloPunteadaApoyoRedimensionCuadroImagen, this.ForeColor, FrameStyle.Dashed);
                    rectanguloPunteadaApoyoRedimensionCuadroImagen.Width = e.X - this.puntoInicioArrastreRedimensionCuadroImagen.X + this.tamanoInicioCuadroImagen.Width;
                    rectanguloPunteadaApoyoRedimensionCuadroImagen.Height = e.Y - this.puntoInicioArrastreRedimensionCuadroImagen.Y + this.tamanoInicioCuadroImagen.Height;
                    if (rectanguloPunteadaApoyoRedimensionCuadroImagen.Width > 0 && rectanguloPunteadaApoyoRedimensionCuadroImagen.Height > 0)
                        ControlPaint.DrawReversibleFrame(rectanguloPunteadaApoyoRedimensionCuadroImagen, this.ForeColor, FrameStyle.Dashed);
                }

            }
        }

    }
}

Method that receives the event of raising the mouse button on the component (PictureBox)

private void soltarEnPictureBox(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Middle)
    {
        if (redimensionandoCuadroImagen)
        {
            if (rectanguloPunteadaApoyoRedimensionCuadroImagen.Width > 0 && rectanguloPunteadaApoyoRedimensionCuadroImagen.Height > 0)
            {
                // Borramos el rectangulo pespunteado
                ControlPaint.DrawReversibleFrame(rectanguloPunteadaApoyoRedimensionCuadroImagen, this.ForeColor, FrameStyle.Dashed);
            }
            // Comparamos si hemos redimensionado por debajo de los minimos en cuyo caso redimensionaremos al los minimos
            if (rectanguloPunteadaApoyoRedimensionCuadroImagen.Width > 100 && rectanguloPunteadaApoyoRedimensionCuadroImagen.Height > 100)
            {
                // Tamaño seleccionado
                this.objetoArrastradoCuadroImagen.Size = rectanguloPunteadaApoyoRedimensionCuadroImagen.Size;
            }
            else
            {
                // Tamaño minimo
                this.objetoArrastradoCuadroImagen.Size = new Size((int)Math.Max(100, rectanguloPunteadaApoyoRedimensionCuadroImagen.Width), Math.Max(100, rectanguloPunteadaApoyoRedimensionCuadroImagen.Height));
            }                
        }

        // Ponemos las variables a su valor por defecto
        this.objetoArrastradoCuadroImagen = null;
        this.puntoInicioArrastreRedimensionCuadroImagen = Point.Empty;
        this.Cursor = Cursors.Default;
    }
}
    
answered by 01.08.2017 / 20:10
source