简体   繁体   中英

Moving image on windows form

Is it possible to make image, which is 'standing' still, move when I hover with my mouse over the image? I'm working in vs 2012, c# and win forms and I want simple movement like gif image, something similar to that, just to make it 'alive'

And how to make that? Thank you very much

It is - you can alter the Picturebox.Location like that. Note that this is an illustration and some edge cases may exist - for example you may want to add logic that detects when the image moved so much, that mouse went out (but within the original location)

private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
    int x = pictureBox1.Location.X;
    int y = pictureBox1.Location.Y;
    pictureBox1.Location = new Point(x + 25, y + 15);
}

private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
    int x = pictureBox1.Location.X;
    int y = pictureBox1.Location.Y;
    pictureBox1.Location = new Point(x - 25, y - 15);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM