Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / VBScript
Article

MERLIN's RANDOM ONELINERS

Rate me:
Please Sign up or sign in to vote.
1.29/5 (16 votes)
10 Dec 20036 min read 69.2K   835   8   1
COVERS MSAGENT CHARACTER(MERLIN) USAGE AND MENU BUILDING, READING\WRITING TEXT FILES AND SEARCHING , SIMPLE ENCRYPTION (CAESAR CIPHER)

A HARDCORE TUTORIAL IN VISUAL BASIC 6.0

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


USAGE:

MSAGENT CHARACTER MERLIN can:

  1. CAN READ RANDOM ONELINERS (FROM JOKES TO QUOTES) FROM A TEXT DATABASE.
  2. SAVE YOUR FAVOURITE ONE-LINERS TO A SEPERATE FILE (SavedOne.txt)
  3. CONTRIBUTE ONE-LINERS TO DATABASE
  4. SPELL A WORD\SENTENCE.

WHAT YOU NEED:

  1. 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
  2. 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 :-)



INTRODUCTION:

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:

  1. MSAGENT Characters: Introduction (A walk through "Hello Merlin" example) Microsoft Agent Animations for Merlin Character Creating Menus
  2. Simple Textfile Database using VB 6.0: Reading/Writing/Appending Searching
  3. Encryption\Decryption: Introduction Program in VB 6.0
  4. Putting everything together (JK_Oneliners.exe) The source code.

And now, on with the show.....



SECTION 1:

MSAGENT CHARACTERS

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:

  1. Start VB 6.0 --> Select New Project ---> Select Standard EXE. You have the Form in your screen.
  2. Select Project ---> Components ---> In the "Controls" tab select the check box for "Microsoft Agent Control 2.0"
  3. You can see the Microsoft Agent icon in your Toolbox ( A man with glasses looking like an FBI agent :-) )
  4. Now Drag-drop this control in your form and name the control as "ctlAgent"
  5. Goto the code window of your form (name: Form1)and add the following code:
Private Sub Form_Load()
Form1.Hide<BR>
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<BR>

ctlAgent.Characters.Load "Merlin", CharPath & "merlin.acs"

Set Merlin = ctlAgent.Characters("Merlin")

'Display charcter on screen<BR>
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<BR>
Dim CharPath As String


'Activate Merlin

CharPath = "c:\windows\msagent\chars\"<BR>

ctlAgent.Characters.Load "Merlin", CharPath & "merlin.acs"
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Show


'CHARACTER ANIMATIONS ( See List Below )


'Character waves with hand<BR>
Merlin.Play "Wave"<BR>
Merlin.Speak "Hello"


'Character bows down and greets<BR>
Merlin.Play "Greet"<BR>
Merlin.Speak "Welcome"


'Character opens a book and reads<BR>
Merlin.Play "Read"<BR>
Merlin.Speak "I will be reading some random one liners"

End Sub

Other Animations to Try out:

Syntax:
CharacterName.Play "AnimationName"

Animation Names:

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

XTRAS:

TRY ADDING THIS TO YOUR CODE FOR MOVING MERLIN AROUND THE SCREEN

With Screen<BR>

  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


SECTION 2

SIMPLE TEXTFILE DATABASE USING VB 6.0:

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"<BR>
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

SECTION 3:

ENCRYPTION\DECRYPTION:

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).<BR>
' 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<BR>
End Function

Private Function Rotate1(ByVal Letter As String, ByVal ShiftAmount As Integer) As String

   Dim pos1 As Integer<BR>
   Dim rot1 As Integer<BR>
   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<BR>
   Rotate1 = Chr$(Asc(Letter))<BR>
  End If

  If ((Asc(Letter)) = Asc(".")) Then<BR>
   Rotate1 = Chr$(Asc(Letter))<BR>
  End If

  If ((Asc(Letter)) = Asc(";")) Then<BR>
   Rotate1 = Chr$(Asc(Letter))<BR>
  End If

  If ((Asc(Letter)) = Asc("-")) Then<BR>
   Rotate1 = Chr$(Asc(Letter))<BR>
  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 :-)

SECTION 4:

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<BR>
  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"<BR>
   Merlin.Speak QQ2<BR>
   '(VER 1)-->   End<BR>
   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<BR>
  Case "READ":<BR>
  ReadDelimitedTextFile


  Case "EXIT":


     Timer1.Enabled = True


'(VER 3)
  Case "SAVE":


      Open App.Path & "\SavedOne.txt" For Append As #3


    Write #3, SavedOne1<BR>
    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<BR>
    Close #1<BR>
    Set Merlin = ctlAgent.Characters("Merlin")
    Merlin.Play "Write"


  Case "SPELL IT":<BR>
    Spell = InputBox("Which word\sentence do you want me to spell?", "SPELL IT")
    Set Merlin = ctlAgent.Characters("Merlin")
    Merlin.Speak Spell



  Case "ABOUT":<BR>
    Set Merlin = ctlAgent.Characters("Merlin")
    Merlin.Play "Announce"<BR>
    Merlin.Speak "J.Karthik did his B.E EEE in Amrita Institute of Technology,


Coimbator........."

    Merlin.Play "Search"<BR>
    Merlin.Speak "He is currently residing in Bangalore and is enjoying every moment of his Life"
  Case Else:

     Merlin.Play "Idle1_1"<BR>
End Select



End Sub


Private Sub Form_Load()


Form1.Hide<BR>
Dim CharPath As String<BR>
Dim UserName1 As String<BR>
Dim CenterX As Long<BR>
Dim CenterY As Long




UserName1 = InputBox("Please type your name below", "JK's OneLiners Alpha 1.4")





'Activate Merlin


CharPath = "c:\windows\msagent\chars\"<BR>
ctlAgent.Characters.Load "Merlin", CharPath & "merlin.acs"
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Show



With Screen<BR>
  CenterX = Int((.Width / .TwipsPerPixelX) / 2)
  CenterY = Int((.Height / .TwipsPerPixelY) / 2)
End With<BR>
Merlin.MoveTo CenterX, CenterY





Merlin.Play "Wave"<BR>
Merlin.Speak "Hello " + UserName1<BR>
Merlin.Play "Greet"<BR>
Merlin.Speak "Welcome to the JK's One Liners"
Merlin.Play "Read"<BR>
Merlin.Speak "I will be reading some random one liners"
Merlin.Play "Pleased"<BR>
Merlin.Speak "But to do that please right click me..."
Merlin.Play "GetAttention"<BR>
Merlin.Speak "and select the" + EMP + " read command."
Merlin.Play "Write"<BR>
Merlin.Speak "Send your feedback to <A href="mailto:karthik_mail@indiatimes.com">karthik_mail@indiatimes.com</A>....."
Merlin.Play "Congratulate"<BR>
Merlin.Speak "...Enjoy"





'Add Commands<BR>
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<BR>
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<BR>
   Dim Letter As String<BR>
   Dim C As Integer


   For C = 1 To Len(PlainText)

       Letter = Mid$(PlainText, C, 1)
       CipherText = CipherText & Rotate1(Letter, ShiftSize)


   Next C


   CCipher = CipherText<BR>
End Function



Private Function Rotate1(ByVal Letter As String, ByVal ShiftAmount As Integer) As String

   Dim pos1 As Integer<BR>
   Dim rot1 As Integer<BR>
   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<BR>
   Rotate1 = Chr$(Asc(Letter))<BR>
  End If


  If ((Asc(Letter)) = Asc(".")) Then<BR>
   Rotate1 = Chr$(Asc(Letter))<BR>
  End If


  If ((Asc(Letter)) = Asc(";")) Then<BR>
   Rotate1 = Chr$(Asc(Letter))<BR>
  End If


  If ((Asc(Letter)) = Asc("-")) Then<BR>
   Rotate1 = Chr$(Asc(Letter))<BR>
  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<BR>
   Dim Letter2 As String<BR>
   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<BR>
End Function



Private Function Rotate1_2(ByVal Letter2 As String, ByVal ShiftAmount As Integer) As String

   Dim pos12 As Integer<BR>
   Dim rot12 As Integer<BR>
   Dim i2 As Integer


   'pos1 = Asc(Letter) - 65<BR>
  ' rot1 = (pos1 - ShiftAmount) Mod 26<BR>
  ' Rotate1 = Chr$(65 + rot1)<BR>
  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<BR>
   Rotate1_2 = Chr$(Asc(Letter2))<BR>
  End If


  If ((Asc(Letter2)) = Asc(".")) Then<BR>
   Rotate1_2 = Chr$(Asc(Letter2))<BR>
  End If


  If ((Asc(Letter2)) = Asc(";")) Then<BR>
   Rotate1_2 = Chr$(Asc(Letter2))<BR>
  End If


  If ((Asc(Letter2)) = Asc("-")) Then<BR>
   Rotate1_2 = Chr$(Asc(Letter2))<BR>
  End If


End Function



'*******SYNC THE ANIMATIONS*******
Private Sub Timer1_Timer()<BR>
Set Merlin = ctlAgent.Characters("Merlin")
Merlin.Play "Hide"<BR>
Timer1.Enabled = False<BR>
Timer2.Enabled = True


End Sub


Private Sub Timer2_Timer()<BR>
Set Merlin = Nothing<BR>
Timer2.Enabled = False<BR>
End<BR>
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDOH! Pin
Muammar©31-Dec-07 7:37
Muammar©31-Dec-07 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.