Sprites as Rollovers

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
2 posts Page 1 of 1
Contributors
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

Sprites as Rollovers
mikethedj4
Does anyone know how to use sprites as rollovers in VB.NET?

At the moment I'm using separate images, and that's annoying, loads slower, and just looks unprofessional. I've always used sprites in web development, and love how sprites work, as it saves me so much time, but not sure how to do it in code.

This is an example of how I'm using a picturebox for image rollovers, but separate images, not sprites.
Code: Select all
    Private Sub HighDef_MouseMove(ByVal sender As Object, ByVal e As System.EventArgs) Handles HighDef.MouseMove
        HighDef.Image = Image.FromFile(Application.StartupPath & "\images\highdef-hover.png")
    End Sub

    Private Sub HighDef_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles HighDef.MouseLeave
        HighDef.Image = Image.FromFile(Application.StartupPath & "\images\highdef.png")
    End Sub

    Private Sub HighDef_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles HighDef.MouseDown
        HighDef.Image = Image.FromFile(Application.StartupPath & "\images\highdef-click.png")

        Form1.Size = New System.Drawing.Size(427, 240)
        Form1.CamResize()

        CamWidth.Text = "427"
        CamHeight.Text = "240"
    End Sub

    Private Sub HighDef_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles HighDef.MouseUp
        HighDef.Image = Image.FromFile(Application.StartupPath & "\images\highdef-hover.png")
    End Sub
This is one of the sprites I want to use for a rollover.
sprite.png
You do not have the required permissions to view the files attached to this post.
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: Sprites as Rollovers
CodenStuff
Maybe something like this using a picturebox:
Code: Select all
Dim Rollover = 0 'Y Location of part of sprite to use - each is 24 pixels high = 3 images
    Private Sub PictureBox1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles PictureBox1.MouseEnter
        Rollover = 24 'Change Y to second image in sprite
        PictureBox1.Invalidate()
    End Sub

    Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawImage(My.Resources.sprite, New Rectangle(0, 0, 105, 24), New Rectangle(0, Rollover, 105, 24), GraphicsUnit.Pixel)
    End Sub

    Private Sub PictureBox1_MouseLeave(sender As System.Object, e As System.EventArgs) Handles PictureBox1.MouseLeave
        Rollover = 0 'Change Y to first image in sprite
        PictureBox1.Invalidate()
    End Sub

    Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Rollover = 48 'Change Y to third image in sprite
        PictureBox1.Invalidate()
    End Sub

    Private Sub PictureBox1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Rollover = 24 'Change Y to second image in sprite
        PictureBox1.Invalidate()
    End Sub
I used the sprite image you provided for the test.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
2 posts Page 1 of 1
Return to “Coding Help & Support”