Domanda

I have been trying to code this for a while and after a couple of weeks of searching the web for an answer I decided to ask

All I want to do is gradually resize pictureBox1 to a set limit from a variable starter value when the mouse hovers over it, the furthest I got was using a forloop which made it instantly change size. I would like it to also change height and width at the same time (pictureBox1 will be a square and i just want it to be a bigger square with a bit of smooth movement)

Also I need it to gradually change back to the original size once the mouse moves off of pictureBox1.

I have been toying about with a couple of solutions found on websites but none seem to work properly, also you might need to know that I have two forms involved in this code; Form1 and frmMenu and because of a mass amount of errors I commented out the bottom two methods.

I do not get any errors but it just doesn't work.

public partial class frmMenu : Form
{
    //private int size = 100;

    public Timer timer1;



    public frmMenu()
    {
        InitializeComponent();
        pictureBox1.MouseEnter += new EventHandler(pictureBox1_MouseEnter);
        //pictureBox1.MouseLeave += new EventHandler(pictureBox1_MouseLeave);  

    }

    private string frmMenu_Load
    {
        set
        {
            timer1.Interval = 1;
        }
    }

    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {

        //for (int i = 140; i > size; size++)
        //{
        //}
        {
            timer1.Interval = 1;
        }
        timer1.Enabled = true;

        if (pictureBox1.Height <= 140)
        {
            pictureBox1.Size = new Size(pictureBox1.Size.Width, pictureBox1.Size.Height + 1);
        }
        else
        {
            timer1.Enabled = false;
        }

    }




//    private void pictureBox1_MouseLeave(object sender, EventArgs e)
//    {
//        if (size > 100)
//            for (int i = size; i > 100; i--)
//            {
//                size = i;
//            }
//        pictureBox1.Height = pictureBox1.Width = size;
//    }

//    private void pictureBox1_Click(object sender, EventArgs e)

//    {
//        var Form1 = new Form1();
//        Form1.Show();

//        var Menu = new frmMenu();
//        Menu.Close();
//    }
}

This is my first time asking so sorry if I haven't given enough information ^.^

È stato utile?

Soluzione

Try this code:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        bool mouseHover;
        int width;
        int height;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 25;
            timer1.Tick += timer1_Tick;
            width = pictureBox1.Width;
            height = pictureBox1.Height;
            timer1.Start();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            if (mouseHover)
            {
                pictureBox1.Width += (pictureBox1.Width < 100) ? 5 : 0;
                pictureBox1.Height += (pictureBox1.Height < 100) ? 5 : 0;
            }
            else
            {
                pictureBox1.Width += (pictureBox1.Width > width) ? -5 : 0;
                pictureBox1.Height += (pictureBox1.Height > height) ? -5 : 0;
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {            
            mouseHover = true;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            mouseHover = false;
        }
    }
}

You can adjust the interval to how you like it, but increasing at 5 pixels horizontally/vertically every 25 milliseconds is pretty smooth. You need to set the initial height and width so you can go back to that size after the mouse leaves the picture box. I use the null coalescing operator so you don't have to stop the timer. As long as the condition on the left side of the ? is true, it will evaluate to the value on the left side of the :. When the condition is false, it evaluates to the right side of the :.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top