Write a Digital Picture Frame in Visual Basic. This program is given, as input, the name of a directory containing a set of picture files. It will then display each of these pictures, in arbitrary order, one after another. (Initially, nothing is drawn) There will be some user specified delay between each picture, and the pictures should cycle indefinitly, unless explicitly stopped. The user will be able to change directories in the same session of the program.
This will give you experience with several tools that will be essential to the game you will build for your final project. Most importantly, you will learn to use DirectDraw to display you images to the screen. You will also learn to access the file system, and to use the timer.
As before, you will have at least one form, and at least one module. If you have common routines to handle frequently required tasks, it would be advisable to put this in an additional module file (seperate from the file that handles assignment-specific tasks) This will allow you to easily reuse this module in later projects. Again, the code in the form itself should be kept to a minimum.
You must have a menu for your form. There should be two main menu options, one marked "File", that will contain commands to open a directory, and to close the program. The other will be marked "Options", and will have suboptions to allow you to change the timer frequency, and freeze the current image (that is, stop cycling through images).
You will use DirectDraw to draw directly on the form. There is a tutorial that demonstrates how to draw an image, which can be found in the
in the directory where you installed the DirectX SDK. You may use this example as the basis of your project, with the following exceptions
1) The tutorial draws on a picture box control located on the form; you are to modify the code so there is NO PICTURE BOX. You are to USE THE FORM ITSELF to draw the image.
2) Nothing is drawn on the form UNTIL THE USER LOADS AN IMAGE DIRECTORY.
3) The form MUST have a Menu. The Menu should
consist of one main heading called File, as well as Options (see above).
When the File item on the menu is clicked, you should have two options:
Open and Exit.
4 a) When the user chooses OPEN, you are to pop up an input box asking the user to type in a path to open. You will pass the path to your Initialize procedure to process.
4 b) If the directory cannot be opened you will report an error and not continue with Initialize (the program will continue to run, awaiting the next Open attempt).
4 c) The tutorial uses a module called MediaDir (media.bas) to locate the image's directory- DO NOT USE THIS MODULE OR ITS ROUTINES. Don't even include the file in your project. It is not necessary and will only make the project more complex. IF USED OR INCLUDED YOU WILL LOSE POINTS!
5) When the user chooses EXIT, you are to end the application.
6) The user should be able to load a different directory in the same session.
7) IMPORTANT: The sample uses a lot of code in the form. YOU ARE TO PUT ALL CODE IN THE MODULE, except
Form_Paint (which, if everything's initialized, calls objDD.RestoreAllSurfaces and your draw/"blit" routine),
Form_Resize which only calls your draw/blit routine,
tmrDisplay_Timer, which should call the module routine to update the image, and
a handler for clicking under File and Options on the menu. Use a CONTROL ARRAY for the submenu items (see Chapter 6).
The program should not crash, regardless of input. If the user specifies a directory that does not exist, or if you are unable to open the file, you should handle this gracefully. The program will display a message box alerting the user to the error. It will then do nothing but await the next input.
You might want to use the Scripting.FileSystemObject library to read the input directory. This is a set of classes that allow to manipulate the file system itself, rather than just a file. This includes operations such as enumerating, creating, and deleting a directory. It is discussed on Page 731 of the Visual Basic Programmer's Guide. To use this library, add the Microsoft Scripting Runtime in the Project->References dialog box. You can use the following code example as a guide.
'Declare a new File System object, which is the root object of the file system classes
'Also declare two additional objects
Dim objFso As New FileSystemObject
Dim objFiles As Files
Dim objFile As File
'Access the files in a particular folder
'C:\ is just an example, you should use the directory specified by the user
Set objFiles = objFso.GetFolder("C:\").Files()
'Loop through each file in the directory, using the For Each syntax
'If the filename ends in .bmp, assume we have a bitmap
For Each objFile InobjFiles
If Right(objFile.Path, 4) = ".bmp"< /FONT > Then
'objFile.Path contains the full path to the file
A good way to handle the timer-based updates is the Visual Basic timer control. You can drop this onto a form (though it has no graphical representation at run-time) and it has a single event "Timer" that is fired when the timeout specified by property "Interval" has elapsed. Remember to perform actual processing in the module.
Your DirectDraw Initialize procedure is called only as the user selects the image file to open.
Copy the image files to a directory with a much shorter path name, so you avoid errors and save time typing the path and filename.
The partial code (in Initialize procedure) to handle the error when the image file cannot be opened can be as follows:
'set it up so VB
doesn't pop an error message if this doesn't work
'we want to handle it ourselves (see following if Err...)
On Error Resume Next
'this is the function
inside our Initialize routine that reads
'the image file (sFile is passed to Initialize)
Set sfcBufferOffScreen = objDD.CreateSurfaceFromFile(sFile, dsfcBufferOffScreen)
'if err.number is 0
that means there was no error
If Err.Number= 0 Then
'proceed with creating clipper, etc then ultimately call our blit routine
'... code ...
'there was an error of some sort
'pop up a msgbox saying can't load image
'reset the error handle to 0 and let vb be ready to handle error messages
On Error GoTo 0
End If 'end if else err = 0
Use a common dialog control and another form to handle selection of the directory visually. See Chapter 7 in the VB Programmer's Guide. You will have to add the component to your toolbox by right-clicking the toolbox, selecting Components, and browsing your windows[winnt]\system directory and selecting comdlg32.ocx.
Extra credit of 2 points will also be given for use of "Hungarian Notation"
When you start the program, the window should appear like this:
When you click on "File", you should be shown a menu:
After selecting "Open", you will be prompted with an input box to select a directory:
If you provide a directory that contains bitmap images, you should see an image from that directory displayed immediatly:
Another image from that directory should be shown in several seconds: