INCLUDES:
1)MSAGENT CHARACTER(MERLIN) USAGE AND MENU BUILDING 2)READING\WRITING TEXT FILES AND SEARCHING 3)SIMPLE ENCRYPTION (CAESAR CIPHER)
4)RANDOMIZE THE APPLICATION
APPLICATION NAME: JK'S ONE-LINERS
MSAGENT CHARACTER MERLIN can:
- CAN READ RANDOM ONELINERS (FROM JOKES TO QUOTES) FROM A TEXT DATABASE.
- SAVE YOUR FAVOURITE ONE-LINERS TO A SEPERATE FILE (SavedOne.txt)
- CONTRIBUTE ONE-LINERS TO DATABASE
- SPELL A WORD\SENTENCE.
- MSAGENT CHARACTER MERLIN INSTALLED IN YOUR SYSTEM (in c:\windows\msagent\chars\merlin.acs) Download details: Merlin( 1.8 MB ) ---> http://agent.microsoft.com/agent2/chars/Merlin.exe
- LERNOUT AND HAUSPIE TEXT TO SPEECH ENGINE Download details: Lernout and Hauspie Engine( 1MB ) -->
http://activex.microsoft.com/activex/controls/agent2/tv_enua.exe
3) Patience and a spark of creativity :-)
I could have started the tutorial by just showing you the source code but that is not justice. The most important thing is to get the feel that VB 6.0 is going to be the tool that brings out the creator in you.
I will be discussing the tutorial in 4 sections:
- MSAGENT Characters: Introduction (A walk through "Hello Merlin" example) Microsoft Agent Animations for Merlin Character Creating Menus
- Simple Textfile Database using VB 6.0: Reading/Writing/Appending Searching
- Encryption\Decryption: Introduction Program in VB 6.0
- Putting everything together (JK_Oneliners.exe) The source code.
And now, on with the show.....
i) Introduction:
Microsoft Agent Characters have been around for a long time which provides interactive communication with the user. Its a fun way to interact with the user by using speech output from the MSAGENT charcater
There are many characters : Merlin the wizard; Genie (Ahem).. the Genie; Robbie the robot etc....
I think you're probably familiar with the animated assistants that Microsoft Office provides for interacting as a Help assistant. Microsoft offers an SDK called Microsoft Agent that enables you to create and manipulate similar assistants from within your VB applications.
MERLIN has independent mobility on your desktop, giving the character a wider scope of movement than the constrained characters in Microsoft Office
The MSAgent.ocx (ActiveX file) provides the basic functionality for the assistants. This control includes a complete object model that lets you execute tasks as simple as displaying a character or as intricate as responding to voice commands. Perhaps the best part about the control is that you can use VB5 or VB6 to extend its usefulness in the same way you would a VB textbox.
The MSAgent.ocx provides the basic functionality for the assistants. This control includes a complete object model that lets you execute tasks as simple as displaying a character or as intricate as responding to voice commands. Perhaps the best part about the control is that you can use VB5 or VB6 to extend its usefulness in the same way you would a VB textbox.
i) Walk Through "Hello Merlin" example:
Steps:
- Start VB 6.0 --> Select New Project ---> Select Standard EXE. You have the Form in your screen.
- Select Project ---> Components ---> In the "Controls" tab select the check box for "Microsoft Agent Control 2.0"
- You can see the Microsoft Agent icon in your Toolbox ( A man with glasses looking like an FBI agent :-) )
- Now Drag-drop this control in your form and name the control as "ctlAgent"
- Goto the code window of your form (name: Form1)and add the following code:
Private Sub Form_Load()
Form1.Hide
Dim CharPath As String
'Activate Merlin
'Charpath is the address of the merlin.acs file
CharPath = "c:\windows\msagent\chars\"
'Load the character specified in Path
ctlAgent.Characters.Load "Merlin", CharPath & "merlin.acs"
Set Merlin = ctlAgent.Characters("Merlin")
'Display charcter on screen
Merlin.Show
End Sub
Notes:
There are three types of character files:
>The .AAF file contains the character animation information. >The .ACF file contains the character data. >The .ACS file contains both the character animations and the character data. (We use this)
6) You can see Merlin the Wizard on your screen. But he is dumb without relevant code. Next we command him to speak "Hello Merlin" and wave at us.
7) Terminate the application run.
ii) Microsoft Agent Animations for Merlin Character:
Follow Steps 1 to 4 from prev section
Modified code for Step 5:
Private Sub Form_Load()
Form1.Hide
Dim CharPath As String
'Activate Merlin
CharPath = "c:\windows\msagent\chars\"
ctlAgent.Characters.Load "Merlin", CharPath & "merlin.acs"
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Show
'CHARACTER ANIMATIONS ( See List Below )
'Character waves with hand
Merlin.Play "Wave"
Merlin.Speak "Hello"
'Character bows down and greets
Merlin.Play "Greet"
Merlin.Speak "Welcome"
'Character opens a book and reads
Merlin.Play "Read"
Merlin.Speak "I will be reading some random one liners"
End Sub
Other Animations to Try out:
Syntax:
CharacterName.Play "AnimationName"
Acknowledge
Announce
Blink
Congratulate
DoMagic1
DoMagic2
Explain
GestureDown
GestureLeft
GestureRight
GetAttention
LookUpBlink
MoveDown
MoveLeft
MoveRight
MoveUp
Pleased
Process
Read
Sad
Search
Show
Think
Wave
Write
TRY ADDING THIS TO YOUR CODE FOR MOVING MERLIN AROUND THE SCREEN
With Screen
CenterX = Int((.Width / .TwipsPerPixelX) / 2)
CenterY = Int((.Height / .TwipsPerPixelY) / 2)
End With
Merlin.MoveTo CenterX, CenterY
iii) Creating Menus:
When you right click the character a popup menu appears. Here you can add your own menus and execute the command
Here's how
Step1: Add this in Private Sub Form_load ()
'Add all the previous code here
'Add this code after it
Merlin.Commands.Add "HELLO", "HELLO", "...HELLO...", True, True
Merlin.Commands.Add "EXIT", "EXIT", "...EXIT...", True, True
Step2: Add this in your code window
Private Sub ctlAgent_Command(ByVal UserInput As Object)
Select Case UserInput.Name
Case "HELLO":
Set Merlin = ctlAgent.Characters("Merlin") ' DON'T FORGET RE-REFERENCE MERLIN for each case
Merlin.Play "Wave"
Merlin.Speak "Hello"
Case "EXIT":
End
Case Else:
Set Merlin = ctlAgent.Characters("Merlin") ' DON'T FORGET RE-REFERENCE MERLIN for each case
Merlin.Play "Idle1_1"
End Select
End Sub
Step 3:
Run. There you have it, the popup menu
Experiment by adding more menus.
Do more research on this topic in the Web.
Now let's move onto the next section
i) Reading/Writing/Appending:
First create a textfile (NoDat1.fil)
and fill it with some contents like
Apple
Mac
Windows
Next use the following steps to read/write/append the file.
To Read:
Step 1: Open the file for input:
Open App.Path & "\NoDat1.fil" For Input As #1
Step 2: Read until end of file: EOF(file number #1)
Do While Not (EOF(1))
Input #1, parameter1 [, parameter2,....]
'Read the Delimited Text File and store it in parameters
Loop
MsgBox parameter1
Step 3: Close the file number #1
Close 1
If you are reading two or more files simultaneously use different Filenumbers #1, #2,.....
To Write/Append:
Open App.Path & "\NoDat1.fil" For Append As #3
Write #3, "Hello","Great"
Close #3
ii) Searching:
To perform a search do read process and check for search criteria. If true get out of loop.
This may not be an effiecient search ...but it will do. (Try other search methods)
Code:
Open App.Path & "\NoDat1.fil" For Input As #1
Do While Not (EOF(1))
Input #1, parameter1 [,parameter2,...]
If (Conditions) Then
'...Do Something here...
GoTo out1
End If
Loop
out1:
'... Do something here ...
Close 1
Why use Encryption? To protect critical data from unauthorized users or in my case to protect the contents of the oneliner database from the users.
Okay, I will be using a simple Caesar Cipher Algorithm. Don't lift your eyebrows by hearing that name.
One of the simplest examples of a substitution cipher is the Caesar cipher, which is said to have been used by Julius Caesar to communicate with his army. Caesar is considered to be one of the first persons to have ever employed encryption for the sake of securing messages. Caesar decided that shifting each letter in the message would be his standard algorithm, and so he informed all of his generals of his decision, and was then able to send them secured messages.
Well for example you wanna encrypt "HELLO" with the key "3". That is we are going to shift each alphabet 3 places and write down the alphabet.
H E L L O --> K H O O R
Now put that in the code format.
One code coming up.... Pay up buddy!! ;-)
Private Function CCipher(ByVal PlainText As String, ByVal ShiftSize As Integer) As String
Dim CipherText As String
Dim Letter As String
Dim C As Integer
' Extract each alphabet and give it to the ROtate function which
' performs the rotation (encryption).
' In Rotate you have to mention the Letter extracted and by how
' much you gotta shift
For C = 1 To Len(PlainText)
Letter = Mid$(PlainText, C, 1)
CipherText = CipherText & Rotate1(Letter, ShiftSize)
Next C
' Return the Encrypted text to CCipher
CCipher = CipherText
End Function
Private Function Rotate1(ByVal Letter As String, ByVal ShiftAmount As Integer) As String
Dim pos1 As Integer
Dim rot1 As Integer
Dim i As Integer
'Check whether the character coming in is only an Alphabet (Uppercase/Lowercase)
If ((Asc(Letter) - 7) >= Asc("a") And (Asc(Letter) - 7) <= Asc("z")) Or ((Asc(Letter) - 7) >=
Asc("A") And (Asc(Letter) - 7) <= Asc("Z")) Then
Rotate1 = Chr$(Asc(Letter) - ShiftAmount)
End If
'Do not perform encryption if the character is a Space, Period, Semicolon or Underscore
If ((Asc(Letter)) = Asc(" ")) Then
Rotate1 = Chr$(Asc(Letter))
End If
If ((Asc(Letter)) = Asc(".")) Then
Rotate1 = Chr$(Asc(Letter))
End If
If ((Asc(Letter)) = Asc(";")) Then
Rotate1 = Chr$(Asc(Letter))
End If
If ((Asc(Letter)) = Asc("-")) Then
Rotate1 = Chr$(Asc(Letter))
End If
End Function
Well if you encrypt....somebody is gonna decrypt. Our friend "Merlin" is gonna decrypt that.
How?...
Just reverse the encrypt code
Instead of :
Rotate1 = Chr$(Asc(Letter) - ShiftAmount)
include this:
Rotate1_2 = Chr$(Asc(Letter2) + ShiftAmount)
Simple :-)
Putting everything together (JK_Oneliners.exe)
(Do everything as told on the form and add two timers (Timer1 - 1000 msec and Timer2 - 1000 msec) for synchronizing the Animations
Another thing: The Text file (Nodat.fil) should contain text in the following format:
1@@,"TO BE OR NOT TO BE"
2@@,"GOD IS GREAT"
etc..
Now Encrypt this file and rename it as "NoDat1.fil" This file is used by Merlin to read the one-liners.
Dim SavedOne1 As String
Dim Contribute1 As String
Dim Lastnum As Double
Dim Spell As String
Sub ReadDelimitedTextFile()
Dim IndX As String
Dim QQ1 As String
Dim QQ2 As String
Dim InRd As String
Dim GDValue
'Activate Merlin again
Set Merlin = ctlAgent.Characters("Merlin")
'******(VER 3)******* GET THE LAST REC NUMBER
Open App.Path & "\NoDat1.fil" For Input As #1
Lastnum = 0
Do While Not (EOF(1))
Input #1, IndX
If (IndX >= "1" And IndX <= "5000") Then
Lastnum = Val(IndX)
End If
' MsgBox Lastnum
Loop
'MsgBox (Lastnum)
Close #1
Randomize
GDValue = Int((Lastnum * Rnd) + 1)
' Open the file for Input.
Open App.Path & "\NoDat1.fil" For Input As #1
InRd = GDValue
Do While Not (EOF(1))
Input #1, IndX, QQ1
QQ2 = CCipher(QQ1, 7)
''*******(VER 3)*********
SavedOne1 = QQ2
If (IndX = InRd + "@@" Or IndX = InRd) Then
' If (IndX = "1216@@" Or IndX = "1216") Then <-- For Testing only
'(VER 1)--> MsgBox QQ2, , "JK's OneLiner #" + Str(GDValue)
Merlin.Play "Read"
Merlin.Speak QQ2
'(VER 1)--> End
GoTo jack
End If
Loop
jack:
' Close the file.
Close 1
End Sub
Private Sub ctlAgent_Command(ByVal UserInput As Object)
Select Case UserInput.Name
Case "READ":
ReadDelimitedTextFile
Case "EXIT":
Timer1.Enabled = True
'(VER 3)
Case "SAVE":
Open App.Path & "\SavedOne.txt" For Append As #3
Write #3, SavedOne1
Close #3
Case "CONTRIBUTE":
Open App.Path & "\NoDat1.fil" For Input As #1
Lastnum = 0
Do While Not (EOF(1))
Input #1, IndX
If (IndX >= 1 And IndX <= 2000) Then
Lastnum = IndX
End If
' MsgBox Lastnum
Loop
Lastnum = Lastnum + 1
'MsgBox (Lastnum)
Close #1
Open App.Path & "\NoDat1.fil" For Append As #1
Contribute1 = InputBox("Please restrict your oneliner to 80 characters max", "JK's OneLiner
Contribution")
Contribute1 = CCipher_2(Contribute1, 7)
Write #1, Lastnum; Contribute1
Close #1
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Play "Write"
Case "SPELL IT":
Spell = InputBox("Which word\sentence do you want me to spell?", "SPELL IT")
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Speak Spell
Case "ABOUT":
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Play "Announce"
Merlin.Speak "J.Karthik did his B.E EEE in Amrita Institute of Technology,
Coimbator........."
Merlin.Play "Search"
Merlin.Speak "He is currently residing in Bangalore and is enjoying every moment of his Life"
Case Else:
Merlin.Play "Idle1_1"
End Select
End Sub
Private Sub Form_Load()
Form1.Hide
Dim CharPath As String
Dim UserName1 As String
Dim CenterX As Long
Dim CenterY As Long
UserName1 = InputBox("Please type your name below", "JK's OneLiners Alpha 1.4")
'Activate Merlin
CharPath = "c:\windows\msagent\chars\"
ctlAgent.Characters.Load "Merlin", CharPath & "merlin.acs"
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Show
With Screen
CenterX = Int((.Width / .TwipsPerPixelX) / 2)
CenterY = Int((.Height / .TwipsPerPixelY) / 2)
End With
Merlin.MoveTo CenterX, CenterY
Merlin.Play "Wave"
Merlin.Speak "Hello " + UserName1
Merlin.Play "Greet"
Merlin.Speak "Welcome to the JK's One Liners"
Merlin.Play "Read"
Merlin.Speak "I will be reading some random one liners"
Merlin.Play "Pleased"
Merlin.Speak "But to do that please right click me..."
Merlin.Play "GetAttention"
Merlin.Speak "and select the" + EMP + " read command."
Merlin.Play "Write"
Merlin.Speak "Send your feedback to karthik_mail@indiatimes.com....."
Merlin.Play "Congratulate"
Merlin.Speak "...Enjoy"
'Add Commands
Merlin.Commands.Add "READ", "READ", "...READ...", True, True
'(VER 3) --> ADD MENU FOR SAVING ONELINER
Merlin.Commands.Add "SAVE", "SAVE", "...SAVE...", True, True
Merlin.Commands.Add "CONTRIBUTE", "CONTRIBUTE", "...CONTRIBUTE...", True, True
'(VER 6) SPELL IT
Merlin.Commands.Add "SPELL IT", "SPELL IT", "...SPELL IT...", True, True
Merlin.Commands.Add "ABOUT", "ABOUT", "...ABOUT...", True, True
Merlin.Commands.Add "EXIT", "EXIT", "...EXIT...", True, True
End Sub
Private Function CCipher(ByVal PlainText As String, ByVal ShiftSize As Integer) As String
Dim CipherText As String
Dim Letter As String
Dim C As Integer
For C = 1 To Len(PlainText)
Letter = Mid$(PlainText, C, 1)
CipherText = CipherText & Rotate1(Letter, ShiftSize)
Next C
CCipher = CipherText
End Function
Private Function Rotate1(ByVal Letter As String, ByVal ShiftAmount As Integer) As String
Dim pos1 As Integer
Dim rot1 As Integer
Dim i As Integer
If ((Asc(Letter) - 7) >= Asc("a") And (Asc(Letter) - 7) <= Asc("z")) Or ((Asc(Letter) - 7) >=
Asc("A") And (Asc(Letter) - 7) <= Asc("Z")) Then
Rotate1 = Chr$(Asc(Letter) - ShiftAmount)
End If
If ((Asc(Letter)) = Asc(" ")) Then
Rotate1 = Chr$(Asc(Letter))
End If
If ((Asc(Letter)) = Asc(".")) Then
Rotate1 = Chr$(Asc(Letter))
End If
If ((Asc(Letter)) = Asc(";")) Then
Rotate1 = Chr$(Asc(Letter))
End If
If ((Asc(Letter)) = Asc("-")) Then
Rotate1 = Chr$(Asc(Letter))
End If
End Function
'(VER 3)''*************** DECIPHER **********************
Private Function CCipher_2(ByVal PlainText As String, ByVal ShiftSize As Integer) As String
Dim CipherText2 As String
Dim Letter2 As String
Dim C2 As Integer
For C2 = 1 To Len(PlainText)
Letter2 = Mid$(PlainText, C2, 1)
CipherText2 = CipherText2 & Rotate1_2(Letter2, ShiftSize)
Next C2
CCipher_2 = CipherText2
End Function
Private Function Rotate1_2(ByVal Letter2 As String, ByVal ShiftAmount As Integer) As String
Dim pos12 As Integer
Dim rot12 As Integer
Dim i2 As Integer
'pos1 = Asc(Letter) - 65
' rot1 = (pos1 - ShiftAmount) Mod 26
' Rotate1 = Chr$(65 + rot1)
If ((Asc(Letter2)) >= Asc("a") And (Asc(Letter2)) <= Asc("z")) Or ((Asc(Letter2)) >= Asc("A")
And (Asc(Letter2)) <= Asc("Z")) Then
Rotate1_2 = Chr$(Asc(Letter2) + ShiftAmount)
End If
If ((Asc(Letter2)) = Asc(" ")) Then
Rotate1_2 = Chr$(Asc(Letter2))
End If
If ((Asc(Letter2)) = Asc(".")) Then
Rotate1_2 = Chr$(Asc(Letter2))
End If
If ((Asc(Letter2)) = Asc(";")) Then
Rotate1_2 = Chr$(Asc(Letter2))
End If
If ((Asc(Letter2)) = Asc("-")) Then
Rotate1_2 = Chr$(Asc(Letter2))
End If
End Function
'*******SYNC THE ANIMATIONS*******
Private Sub Timer1_Timer()
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Play "Hide"
Timer1.Enabled = False
Timer2.Enabled = True
End Sub
Private Sub Timer2_Timer()
Set Merlin = Nothing
Timer2.Enabled = False
End
End Sub
END NOTE:
If you feel this tutorial is great or short of any information or feel like banging your head
kindly mail me ----> karthik_mail@indiatimes.com
ADIOS