Visual Basic 6 Win32 API Tutorial, Page 12
The Encryption.vbp file is where we'll start incorporating API calls into our code. Here's a screen shot of the main form:

Here's a screenshot of the About form:

Let's cover some of the real basic glue code that we'll need throughout the development of this project. First off, here are some of the basic project information you need to know. Note the Contained In entry in the table that designates where the control should go. Because the project has a lot of UI components, this is needed for clarification. Bear with me as we go through the list, but rest assured, this is by far the biggest list of project properties we'll see. Also note that I've left the labels out of the controls list that are used for descriptive purposes only. All the controls can be found in your default Toolbox except for the Progress Bar which is located in Microsoft Common Controls 6.0. By looking at the screen shot, you should be able to add them in if you think it's necessary:
| Object | Contained In | Property | Value |
| Form | Name | frmMain | |
| Caption | The Encryption Program | ||
| Borderstyle | 3 - Fixed Dialog | ||
| Frame | frmMain | Name | fraMain |
| Index | 0 | ||
| Caption | File: | ||
| Frame | frmMain | Name | fraMain |
| Index | 1 | ||
| Caption | Algorithm Parameters | ||
| Drive List Box | fraMain(0) | Name | drvMain |
| Directory List Box | fraMain(0) | Name | dirMain |
| File List Box | fraMain(0) | Name | filMain |
| Text Box | fraMain(0) | Name | txtFileFilter |
| Text | (leave blank) | ||
| Command Button | fraMain(0) | Name | cmdSetFilter |
| Caption | Set | ||
| Option Button | fraMain(1) | Name | optEncrypt |
| Index | 0 | ||
| Caption | Encrypt Selected File | ||
| Option Button | fraMain(1) | Name | optEncrypt |
| Index | 1 | ||
| Caption | Decrypt Selected File | ||
| Text Box | fraMain(1) | Name | txtKey |
| Text | (leave blank) | ||
| PasswordChar | * | ||
| Text Box | fraMain(1) | Name | txtSeedValue |
| Text | (leave blank) | ||
| PasswordChar | * | ||
| Check Box | fraMain(1) | Name | chkShowValues |
| Caption | Show Values? | ||
| Check Box | frmMain | Name | chkSaveAs |
| Caption | Save Altered File As... | ||
| Command Button | frmMain | Name | cmdRunEncryption |
| Caption | Start Encryption | ||
| Text Box | frmMain | Name | txtStatus |
| Text | (leave blank) | ||
| BackColor | &H00C0C0C0& | ||
| Progress Bar | frmMain | Name | ProgressBar |
| Command Button | frmMain | Name | cmdAbout |
| Caption | About... | ||
| Command Button | frmMain | Name | cmdExit |
| Caption | E&xit | ||
| Form | Name | frmAbout | |
| Caption | About the Encryption Program | ||
| Label | frmAbout | Name | lblTitle |
| Caption | The Encryption Program | ||
| Label | frmAbout | Name | lblCreatedBy |
| Caption | Created By Victor | ||
| Label | frmAbout | Name | lblVersion |
| Caption | (leave blank) | ||
| Timer | frmAbout | Name | tmrAbout |
| Enabled | True | ||
| Interval | 5000 | ||
| Command Button | frmAbout | Name | cmdClose |
| Caption | &Close |
There's some code we need to add to this skeleton layout before we start adding API calls to the project. We won't spend too much time on them, but they give some basic functionality to the project. Let's start with frmMain. The first method is called SetFilePattern:
Private Sub SetFilePattern() If Trim(txtFileFilter.Text) = "" Then filMain.Pattern = "*.*" Else filMain.Pattern = Trim(txtFileFilter.Text) End If End Sub
This is called from the Click event of cmdSetPattern:
Private Sub cmdSetPattern_Click() SetFilePattern End Sub
SetFilePattern sets the Pattern property of the file list box to show either all the files or files with a specific pattern given by the user.
The next function we need is called ValidateInterface. This function checks entries made in the interface and returns "" on success and something on failure.
Private Function ValidateInterface() As String On Error Resume Next Dim lngCheck As Long If Trim$(txtKey.Text) = "" Then ValidateInterface = "Please enter in a key." Exit Function End If If Trim$(txtSeedValue.Text) = "" Then ValidateInterface = "Pleae enter in the seed value." Exit Function Else lngCheck = CLng(txtSeedValue.Text) If Err.Number <> 0 Then ValidateInterface = "The seed value is too large." Exit Function ElseIf lngCheck < 1 Then ValidateInterface = "The seed value must be greater than 0." Exit Function End If End If End Function
This isn't called yet by a control in the project, but we'll need it to verify what the data that the user entered.
The function begins by checking that a key was entered:
If Trim$(txtKey.Text) = "" Then ValidateInterface = "Please enter in a key." Exit Function End If
Then we ensure a seed value was entered:
If Trim$(txtSeedValue.Text) = "" Then ValidateInterface = "Pleae enter in the seed value." Exit Function
If a seed value has been entered we check for an overflow:
Else lngCheck = CLng(txtSeedValue.Text) If Err.Number <> 0 Then ValidateInterface = "The seed value is too large." Exit Function ElseIf lngCheck < 1 Then ValidateInterface = "The seed value must be greater than 0." Exit Function End If End If
The Click event of the cmdAbout command button brings up the frmAbout form:
Private Sub cmdAbout_Click() Screen.MousePointer = vbHourglass frmAbout.Show vbModal End Sub
The Click event of the cmdExit command button shuts down the application:
Private Sub cmdExit_Click() Unload Me Set frmMain = Nothing End Sub
The drive, directory, and file list boxes changes are all coordinated using the following lines of code:
Private Sub drvMain_Change() dirMain.Path = drvMain.Drive End Sub
Private Sub dirMain_Change() On Error Resume Next SetFilePattern filMain.Path = dirMain.Path End Sub
The chkShowValues check box is used to let the user see what is actually contained in the seed and key text boxes. It does this by calling the CheckKeyAndSeed method in its Click event:
Private Sub chkShowValues_Click() CheckKeyAndSeed End Sub
Here's what the CheckKeyAndSeed looks like:
Private Sub CheckKeyAndSeed() On Error Resume Next If chkShowValues.Value = vbUnchecked Then txtKey.PasswordChar = "*" txtSeedValue.PasswordChar = "*" Else txtKey.PasswordChar = "" txtSeedValue.PasswordChar = "" End If End Sub
If the user want's to see what's in the boxes, we set PasswordChar to an empty string. Otherwise, the text is masked with the "*" character.
Now let's move on to frmAbout. The first method to look at is InitializeForm:
Private Sub InitializeForm() Screen.MousePointer = vbHourglass lblVersion.Caption = "Beta Version " & CStr(App.Major) & _ "." & CStr(App.Minor) & "." & CStr(App.Revision) Randomize Screen.MousePointer = vbDefault End Sub
It's called from the Load event of the Form:
Private Sub Form_Load() InitializeForm End Sub
The other method is called AlterLabels:
Private Sub AlterLabels() End Sub
It does nothing yet; trust me, it will! It's called from the Timer event of tmrTimer:
Private Sub tmrAbout_Timer() AlterLabels End Sub
Now that the skeleton code is done, let's go over what we really have to worry about:
- Implement the encryption algorithm defined above and show the status of the algorithm to the screen
- Add a Save As process
- Have some fun with the About screen
- Change each form to look more appealing - gray is getting old
We'll tackle all of these issues along with some other neat tricks in the next three chapters. Whenever we add code that uses the API calls, we'll try to achieve the same functionality using VB code only. We'll compare them from a performance standpoint as well, and see which one "wins".
Page 12 of 13
