2011-09-04 31 views
0

從用戶從硬盤上的任何給定文件夾中預先選擇的圖像中更改VB應用程序中的背景圖像的最佳和最簡單的方法是什麼?以不同的圖像作爲背景在視覺基礎上滾動

所以說「Mike」在自然文件夾中選擇D:\ Images \ Nature中的一些圖像,我們可以說「20」圖像。現在VB應用程序讀取它並保存路徑,以便下次再次從該文件夾打開其提取圖像。每隔幾秒就可以說45秒後爲背景加載新圖像。

+0

您是使用Windows Forms,Web Forms還是什麼? –

+0

你指的僅僅是你的應用程序的背景,對吧?現在整個Windows桌面? –

+0

@John Saunders它的Windows窗體 –

回答

0

當用戶選擇一個圖片文件夾時,將該文件夾存儲在註冊表中。 當程序加載時,從註冊表中獲取文件夾。 你可以將它保存這樣的:

SaveSetting ("mayappname","settings","picturefolder",PicFolderName) 

和恢復時,程序加載與文件夾:

Dim PicFolderName As String = GetSetting("mayappname", "settings", "picturefolder", My.Computer.FileSystem.SpecialDirectories.MyDocuments) 

這個例子是閱讀的文件夾和改變圖像的所有JPG圖像每隔45秒該文件夾中的下一張圖片。如果您希望用戶能夠選擇20張圖像並在它們之間旋轉,讓我知道,那麼我們必須存儲用戶選擇的每張圖片並僅在它們之間旋轉。但是,此代碼在所選文件夾中的所有圖片之間旋轉。

我想你知道如何改變它只是與用戶選擇的圖片一起工作。我不會給出答案,因爲它可以通過很多方式完成,所以我需要更多的信息來說明在用戶和程序之間交互的方式。

好吧。如果你有一個文件夾存放在註冊表中的代碼,那麼這個粘貼到表格內改變畫面從選定文件夾中的圖片每45秒:

Private ImageNames As New List(Of String) 
Private ImageIndexNow As Integer = 0 
Private PictureTimer As New Timer 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    'Get the saved path for where the images are stored. 
    Dim PicFolderName As String = GetSetting("mayappname", "settings", "picturefolder", My.Computer.FileSystem.SpecialDirectories.MyDocuments) 
    'PicFolderName="c:\mypictures" ' remove rem if you just want to test with a specific folder 
    'Call sub that read in all names of images in that path. 
    LoadImageNames(PicFolderName) 
    PictureTimer.Interval = 45000 '45 seconds 
    PictureTimer.Enabled = True 
    AddHandler PictureTimer.Tick, AddressOf PictureTimer_Tick 
End Sub 

Sub LoadImageNames(ByVal ImagePath As String) 
    'Load image names in a list of strings for the provided Imagepath 
    For Each file As String In IO.Directory.GetFiles(ImagePath , "*.jpg") 
     ImageNames.Add(file) 
    Next 
End Sub 

Private Sub PictureTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    'set background image of the form to image number imageIndexNow in the list of images 
    Me.BackgroundImageLayout = ImageLayout.Stretch 
    Me.BackgroundImage = Image.FromFile(ImageNames(ImageIndexNow)) 
    ImageIndexNow += 1 ' Add one to the number so next picture is selected next time the timer-tick is fired. 
    If ImageIndexNow > ImageNames.Count-1 Then ImageIndexNow = 0 ' Start from zero if imageIndexNow is larger than amount of images. 
End Sub 

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    RemoveHandler PictureTimer.Tick, AddressOf PictureTimer_Tick 
End Sub 

如果你想改變這種只之間旋轉20個用戶選擇的圖片,更改程序以將這些圖片名稱存儲在ImageNames列表中。然後將這個列表存儲在註冊表,文件或數據庫中。當程序加載時,恢復列表,你可以使用上面的代碼,幾乎沒有任何改變。