GDI+ - Scrolling Background-VBForums
Results 1 to 5 of 5

Thread: GDI+ - Scrolling Background

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,151

    GDI+ - Scrolling Background

    So you may have seen my XNA - Scrolling Background, well here is the GDI+ version.

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private bgImage1 As Bitmap
        Private bgImage2 As Bitmap
        Private xPosition As Integer
        Private scrolling As Boolean
        Private scrollingThread As Threading.Thread
    
        Private Sub ResetGlobals()
            xPosition = 0
            scrolling = False
            scrollingThread = New Threading.Thread(AddressOf ScrollLoop)
            Me.SplitBackground()
        End Sub
    
        Private Sub SplitBackground()
            'The background1 will be the left half of the image and background2 will be the right half
            bgImage1 = My.Resources.gamebackground
            bgImage2 = My.Resources.gamebackground
        End Sub
    
        Private Sub ScrollLoop()
            'Update -> Draw -> Loop
    
            Dim s As Stopwatch = New Stopwatch
            s.Start()
    
            'Update
            Me.UpdateBackground()
    
            'Draw
            Me.DrawBackground()
    
            'Loop
            While s.Elapsed <= TimeSpan.FromMilliseconds(16.6) '60 FPS
                Application.DoEvents()
            End While
    
            'Stop the watch
            s.Stop()
    
            'Display FPS
            Console.WriteLine("Frames Per Second: " & (1000 / s.ElapsedMilliseconds))
    
            'Continue if applicable
            If scrolling Then
                scrollingThread = New Threading.Thread(AddressOf ScrollLoop)
                scrollingThread.Start()
            End If
        End Sub
    
        Private Sub UpdateBackground()
            'Move the x position to the left by 1 until we've scrolled to the width of the background
            xPosition = If(xPosition > -Me.ClientSize.Width, xPosition - 1, 0)
        End Sub
    
        Private Sub DrawBackground()
            'Refresh the form if the handle is still available
            If scrolling Then
                Me.BeginInvoke(Sub() RefreshForm())
            End If
        End Sub
    
        Private Sub RefreshForm()
            'Refresh
            Me.Refresh()
        End Sub
    
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            'Be sure to stop scrolling if applicable
            scrolling = False
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            'If the space key is hit then start scrolling
            'If the escape key is hit the stop scrolling
            If e.KeyCode = Keys.Space AndAlso Not scrolling Then
                scrolling = True
                scrollingThread.Start()
            ElseIf e.KeyCode = Keys.Escape AndAlso scrolling Then
                scrolling = False
            End If
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            e.Graphics.DrawImage(bgImage1, New Rectangle(xPosition, 0, Me.ClientSize.Width, Me.ClientSize.Height))
            e.Graphics.DrawImage(bgImage2, New Rectangle(xPosition + Me.ClientSize.Width, 0, Me.ClientSize.Width, Me.ClientSize.Height))
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Reset the globals
            Me.ResetGlobals()
    
            'Set the form's properties
            With Me
                .DoubleBuffered = True
                .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
                .KeyPreview = True
                .ClientSize = New Size(bgImage1.Size.Width \ 2, bgImage1.Size.Height)
                .Text = "GDI+ Scrolling Background Example"
            End With
    
        End Sub
    
        
    End Class
    This is the image that I use which I've named gamebackground and is located in My.Resources:
    Name:  gamebackground.jpg
Views: 22900
Size:  13.7 KB

  2. #2
    New Member
    Join Date
    May 2015
    Posts
    2

    Re: GDI+ - Scrolling Background

    Hi

    Where can I download My.Resources?

    Cheers

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,151

    Re: GDI+ - Scrolling Background

    You do not download My.Resources, instead you upload any resources(documents, images, etc.) into My.Resources and then you get the resource when needed. Take a look at this link on How To: Add or Remove Resources.

  4. #4
    New Member
    Join Date
    May 2015
    Posts
    2

    Re: GDI+ - Scrolling Background

    A my bad.

    I should've taken the time to ask where I can get a copy of the background you are using. My apologies

    Thanks

  5. #5
    March 2015 Winner kfcSmitty's Avatar
    Join Date
    May 2005
    Location
    Kingston, Ontario
    Posts
    2,124

    Re: GDI+ - Scrolling Background

    His gamebackground is the picture in the first post. If you right click it and save as, you should then be able to add it to My.Resources in your project.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.