Wednesday, October 22, 2008

Ask For Confirmation Before Exit Progarm

When the user will try to end this program by clicking the X button, by pressing Alt + F4, or by any other way, a message box will pop up and will ask him for confirmation.

Form Code

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim Answer As Integer
Answer = MsgBox("Are you sure you want to exit this program?", _
vbQuestion + vbYesNo, "Confirmation")
If Answer = vbNo Then Cancel = -1
End Sub

Tuesday, October 21, 2008

Launch URL With Default Browser

'Add 1 CommandButton to your form (named Command1),'To launch URL with default browser press the button.'Insert the following code to your form:

Private Sub Command1_Click()
'Replace http://www.yahoo.com with the URL you want to launch.
'You can replace 'vbHide' with following settings, according to in which state
'you want to open the browser :
'vbHide ; vbMaximizedFocus ; vbMinimizedFocus ;vbMinimizedNoFocus ;
'vbNormalFocus ; vbNormalNoFocus
RetVal = SHELL("Start.exe http://www.yahoo.com", vbHide)
End Sub

Print File


Preparations


Add 1 Text Box to your form. Set the Text Box's MultiLine property to True, and the Text Box's Visible property to False.

Form Code

Private Sub Form_Load()
Dim file As String

'Replace 'c:\autoexec.bat' with the file you want to print
file = "c:\autoexec.bat"
Open file For Input As #1
Text1.Text = Input(LOF(1), #1)
Close
Printer.Print Text1.Text
Printer.EndDoc

End Sub

Monday, October 20, 2008

Browse And Preview Installed Fonts


'Add 1 List Box to your form. The List Box will be populated with all installed fonts.
'When you click on one of the fonts, the List Box will change its font to the clicked font.

'Insert this code to your form:

Private Sub Form_Load()
Dim counter As Integer
For counter = 0 To Screen.FontCount - 1
List1.AddItem Screen.Fonts(counter)
Next
End Sub

Private Sub List1_Click()
Static tempheight As Single
If tempheight = 0 Then tempheight = List1.Height
List1.Font.Name = List1.List(List1.ListIndex)
List1.Height = tempheight
End Sub

Add New Font Temporary

Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
Add 1 Command Button to your form.

Insert this code to the module :

Declare Function AddFontResource& Lib "gdi32" Alias "AddFontResourceA" (ByVal lpFileName As String)
Declare Function RemoveFontResource& Lib "gdi32" Alias "RemoveFontResourceA" (ByVal lpFileName As String)

'Insert this code to your form:

Private Sub Command1_Click()
Dim retvalue As Long
'Replace all 'MyFont' below with your font file.
retvalue = RemoveFontResource("c:\MyFont.ttf")
Command1.Caption = "uninstall"
End Sub
Private Sub Form_Load()
Dim retvalue As Long
Command1.Caption = "uninstall"
retvalue = AddFontResource("c:\MyFont.ttf")
Command1.FontName = "MyFont"
End Sub

Get MP3 File Tag Info

'Insert the following code to your form:

Private Sub Form_Load()
Dim fNum As Integer
Dim sTagIdent As String * 3
Dim sTitle As String * 30
Dim sArtist As String * 30
Dim sAlbum As String * 30
Dim sYear As String * 4
Dim sComment As String * 30
fNum = FreeFile
'Replace 'c:\MySong.mp3' with the name of the MP3 file that you want to get its info.
Open "c:\MySong.mp3" For Binary As fNum
Seek #fNum, LOF(fNum) - 127
Get #fNum, , sTagIdent
If sTagIdent = "TAG" Then
Get #fNum, , sTitle
Get #fNum, , sArtist
Get #fNum, , sAlbum
Get #fNum, , sYear
Get #fNum, , sComment
End If
Close #fNum
MsgBox sTitle & "," & sArtist & "," & sAlbum & "," & sYear & "," & sComment
End Sub

Hide Your Program From The Ctrl-Alt-Del Process List


Add 2 Command Buttons to your form.Press the first to hide you program, and the second to show it again.


Module Code


Public Const RSP_SIMPLE_SERVICE = 1


Public Const RSP_UNREGISTER_SERVICE = 0
Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Declare Function RegisterServiceProcess Lib "kernel32" (ByVal dwProcessID _As Long, ByVal dwType As Long) As Long

Form Code

Public Sub HideApp(Hide As Boolean)
Dim ProcessID As Long
ProcessID = GetCurrentProcessId()
If Hide Then
retval = RegisterServiceProcess(ProcessID, RSP_SIMPLE_SERVICE)
Else
retval = RegisterServiceProcess(ProcessID, RSP_UNREGISTER_SERVICE)
End If
End Sub

Private Sub Command1_Click()
HideApp (True)
End Sub

Private Sub Command2_Click()
HideApp (False)
End Sub


Detect If Sound Card Can Play Sound Files


'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)



'Insert this code to the module :

Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long

'Insert this code to your form:

Private Sub Form_Load()
Dim I As Integer
I = waveOutGetNumDevs()
If I > 0 Then
MsgBox "Your system can play sound files."
Else
MsgBox "Your system can not play sound files."
End If

End Sub

Retrieve The Length Of WAV, AVI And MIDI Files

Module Code

Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal _ lpstrCommand As String, ByVal lpstrReturnString As String, _ ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Form Code

Function GetMediaLength(FileName As String)
Dim MediaLength As Long
Dim RetString As String * 256
Dim CommandString As String

'open the media file
CommandString = "Open " & FileName & " alias MediaFile"
mciSendString CommandString, vbNullString, 0, 0&

'get the media file length
CommandString = "Set MediaFile time format milliseconds"
mciSendString CommandString, vbNullString, 0, 0&
CommandString = "Status MediaFile length"
mciSendString CommandString, RetString, Len(RetString), 0&
GetMediaLength = CLng(RetString)

'close the media file
CommandString = "Close MediaFile"
mciSendString CommandString, vbNullString, 0, 0&

End Function

Private Sub Form_Load()

Dim Seconds, Minutes As Integer
Dim MilliSeconds As Long
' replace "c:\my_media_file.wav" with the path to your media file
MilliSeconds = GetMediaLength("c:\my_media_file.wav")
' the function GetMediaLength return the media length in milliseconds,
' so we will calculate the total minutes and seconds
Seconds = Int(MilliSeconds / 1000) Mod 60
Minutes = Int(MilliSeconds / 60000)
MilliSeconds = MilliSeconds Mod 1000

TotalTime = Minutes & ":" & Seconds & ":" & MilliSeconds
MsgBox (TotalTime)

End Sub


Play AVI File


'Add a module to your project (In the menu choose Project -> Add Module, Then click Open) Add 2 CommandButtons to your form (named Command1 and Command2). When you press the first button the AVI movie will start to play. Even after the AVI Finish playing, it is still takes memory. To remove it from the memory press the second button.

'Insert this code to the module :

Declare Function mciSendString Lib "winmm.dll" Alias _"mciSendStringA" (ByVal lpstrCommand As String, ByVal _lpstrReturnString As String, ByVal uReturnLength As Long, _ByVal hwndCallback As Long) As Long

'Insert the following code to your form:

Private Sub Command1_Click()
Dim returnstring As String
Dim FileName As String
returnstring = Space(127)
'Replace c:\MyMovie.avi with the AVI file you want to play
FileName = "c:\MyMovie.avi"
erg = mciSendString("open " & Chr$(34) & FileName & _
Chr$(34) & " type avivideo alias video", returnstring, 127, 0)
erg = mciSendString("set video time format ms", returnstring, 127, 0)
erg = mciSendString("play video from 0", returnstring, 127, 0)
End Sub

Private Sub Command2_Click()
erg = mciSendString("close video", returnstring, 127, 0)
End Sub

Play MIDI Files

Add a module to your project (In the menu choose Project -> Add Module, Then click Open) Add 2 CommandButtons to your form (named Command1 and Command2). When you press the first button the Midi File will start playing. When you press the second button the Midi File will stop playing.

Insert this code to the module :

Declare Function mciSendString Lib "winmm.dll" Alias _"mciSendStringA" (ByVal lpstrCommand As String, _ByVal lpstrReturnString As String, ByVal uReturnLength _As Long, ByVal hwndCallback As Long) As Long

Insert the following code to your form:

Public Sub StopMIDI(MidiFileName As String)
Call mciSendString("stop " + MidiFileName, 0&, 0, 0)
Call mciSendString("close " + MidiFileName, 0&, 0, 0)
End Sub

Function PlayMIDI(MidiFileName As String)
On Error Resume Next
Call mciSendString("open " + MidiFileName + " type sequencer", 0&, 0, 0)
If mciSendString("play " + MidiFileName + Flags, 0&, 0, 0) = 0 Then
PlayMIDI = 0
Else
PlayMIDI = 1
End If
End Function
Private Sub Command1_Click()
'Replace c:\mydir\song1.mid with the Midi file name you want to play
PlayMIDI ("c:\mydir\song1.mid")
End Sub

Private Sub Command2_Click()
'Replace c:\mydir\song1.mid with the Midi file name you want to stop
StopMIDI ("c:\mydir\song1.mid")
End Sub

Make a CD Player

'Add Class Module to your project (In the menu choose Project -> Add Class Module, Then click Open). Change the Class Module name to CDAudio (In the Project Explorer press on Class1 and press F4). Add 14 Command Buttons and 2 Text Boxes to your form. Insert into Text1 the track number to play. Insert into Text2 the Rewind\FastForward speed.

'Insert the following code to your Class Module :


Private Declare Function mciGetErrorString Lib "winmm.dll" Alias _
"mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, _
ByVal uLength As Long) As Long
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal _
uReturnLength As Long, ByVal hwndCallback As Long) As Long

Function StartPlay()
mciSendString "play cd", 0, 0, 0
End Function

Function SetTrack(Track%)
mciSendString "seek cd to " & Str(Track), 0, 0, 0
End Function

Function StopPlay()
mciSendString "stop cd wait", 0, 0, 0
End Function

Function PausePlay()
mciSendString "pause cd", 0, 0, 0
End Function

Function EjectCD()
mciSendString "set cd door open", 0, 0, 0
End Function

Function CloseCD()
mciSendString "set cd door closed", 0, 0, 0
End Function

Function UnloadAll()
mciSendString "close all", 0, 0, 0
End Function

Function SetCDPlayerReady()
mciSendString "open cdaudio alias cd wait shareable", 0, 0, 0
End Function

Function SetFormat_tmsf()
mciSendString "set cd time format tmsf wait", 0, 0, 0
End Function

Function SetFormat_milliseconds()
mciSendString "set cd time format milliseconds", 0, 0, 0
End Function

Function CheckCD$()
Dim s As String * 30
mciSendString "status cd media present", s, Len(s), 0
CheckCD = s
End Function

Function GetNumTracks%()
Dim s As String * 30
mciSendString "status cd number of tracks wait", s, Len(s), 0
GetNumTracks = CInt(Mid$(s, 1, 2))
End Function

Function GetCDLength$()
Dim s As String * 30
mciSendString "status cd length wait", s, Len(s), 0
GetCDLength = s
End Function

Function GetTrackLength$(TrackNum%)
Dim s As String * 30
mciSendString "status cd length track " & TrackNum, s, Len(s), 0
GetTrackLength = s
End Function

Function GetCDPosition$()
Dim s As String * 30
mciSendString "status cd position", s, Len(s), 0
GetCDPosition = s
End Function

Function CheckIfPlaying%()
CheckIfPlaying = 0
Dim s As String * 30
mciSendString "status cd mode", s, Len(s), 0
If Mid$(s, 1, 7) = "playing" Then CheckIfPlaying = 1
End Function

Function SeekCDtoX(Track%)
StopPlay
SetTrack Track
StartPlay
End Function

Function ReadyDevice()
UnloadAll
SetCDPlayerReady
SetFormat_tmsf
End Function

Function FastForward(Spd%)
Dim s As String * 40
SetFormat_milliseconds
mciSendString "status cd position wait", s, Len(s), 0
CheckIfPlaying%
If CheckIfPlaying = 1 Then
mciSendString "play cd from " & CStr(CLng(s) + Spd), 0, 0, 0
Else
mciSendString "seek cd to " & CStr(CLng(s) + Spd), 0, 0, 0
End If
SetFormat_tmsf
End Function

Function ReWind(Spd%)
Dim s As String * 40
SetFormat_milliseconds
mciSendString "status cd position wait", s, Len(s), 0
CheckIfPlaying%
If CheckIfPlaying = 1 Then
mciSendString "play cd from " & CStr(CLng(s) - Spd), 0, 0, 0
Else
mciSendString "seek cd to " & CStr(CLng(s) - Spd), 0, 0, 0
End If
SetFormat_tmsf
End Function


'Insert the following code to your Class Module :



Dim Snd As CDAudio
Private Sub Command1_Click()
Snd.SeekCDtoX Val(Text1)
End Sub

Private Sub Command10_Click()
MsgBox Snd.CheckIfPlaying
End Sub

Private Sub Command11_Click()
s = Snd.GetCDPosition
MsgBox "Track: " & CInt(Mid$(s, 1, 2)) & " Min: " & _
CInt(Mid$(s, 4, 2)) & " Sec: " & CInt(Mid$(s, 7, 2))
Track = CInt(Mid$(s, 1, 2))
Min = CInt(Mid$(s, 4, 2))
Sec = CInt(Mid$(s, 7, 2))
End Sub

Private Sub Command12_Click()
s = Snd.GetCDPosition
MsgBox Snd.GetTrackLength(CInt(Mid$(s, 1, 2)))
End Sub

Private Sub Command13_Click()
Snd.PausePlay
End Sub

Private Sub Command14_Click()
Snd.StartPlay
End Sub

Private Sub Command2_Click()
s$ = Snd.GetCDLength
MsgBox "Total length of CD: " & s, , "CD len"
End Sub

Private Sub Command3_Click()
Snd.CloseCD
End Sub

Private Sub Command4_Click()
Snd.EjectCD
End Sub

Private Sub Command5_Click()
Snd.StopPlay
End Sub

Private Sub Command6_Click()
Snd.ReWind Val(Text2) * 1000
End Sub

Private Sub Command7_Click()
Snd.FastForward Val(Text2) * 1000
End Sub

Private Sub Command8_Click()
MsgBox Snd.CheckCD
End Sub

Private Sub Command9_Click()
MsgBox Snd.GetNumTracks
End Sub

Private Sub Form_Load()
Set Snd = New CDAudio
Snd.ReadyDevice
Command1.Caption = "Play track"
Command2.Caption = "Get CD Length"
Command3.Caption = "Close CD"
Command4.Caption = "Eject CD"
Command5.Caption = "Stop"
Command6.Caption = "Rewind"
Command7.Caption = "Fast Forward"
Command8.Caption = "Check if CD in drive"
Command9.Caption = "Get numbre of tracks"
Command10.Caption = "Check If Playing"
Command11.Caption = "Get CD Position"
Command12.Caption = "Get current track Length"
Command13.Caption = "Pause"
Command14.Caption = "Resume"
Text1.Text = "1"
Text2.Text = "5"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Snd.StopPlay
Snd.UnloadAll
End Sub

Visual Basic

History of Visual Basic

Visual basic evolved from BASIC(Beginners' All-purpose Symbolic Instruction Code). The BASIC language was created by Professors John Kemeny and Thomas Kurtz of Dartmouth College in the mid 1960s(Deitel&Deitel, 1999) . It is a carefully constructed English-Like language basically used by the programmers to write simple computer programs. It served the purpose of educating laymen like we all the basic concepts of programming. From then on many versions of BASIC were developed to accommodate different computer platforms. Some of the versions are Microsoft QBASIC, QUICKBASIC, GWBASIC ,IBM BASICA, Apple BASIC and etc. Apple BASIC was developed by Steve Wozniak, a former employee of Hewlett-Packard and a good friend of steve Jobs(the founded of Apple Inc.). Steve Jobs had worked with Wozniak in the past (together they designed the arcade game "Breakout" for Atari). They pooled their financial resources together to have PC boards made, and on April 1st, 1976 they officially formed the Apple Computer Company.

The concept of computer programming

Before we begin programming, let us understand some basic concepts of programming. According to Webopedia, a computer program is an organized list of instructions that, when executed, causes the computer to behave in a predetermined manner. Without programs, computers are useless. Therefore, programming means designing or creating a set of instructions to ask the computer to carry out certain jobs which normally are very much faster than human beings can do.  A lot of people think that computer CPU is a very intelligent thing, which in actual fact it is a dumb and inanimate object that can do nothing without human assistant. The microchips of a CPU can only understand two distinct electrical states, namely, the on and off states, or 0 and 1 codes in the binary system. So, the CPU only understands a combinations of 0 and 1 codes, a language which we called machine language. Machine language is extremely difficult to learn and it is not for us laymen to master it easily. Fortunately , we have many smart programmers who wrote interpreters and compilers that can translate human language-like programs such as BASIC into machine language so that the computer can carry out the instructions entered by the users. Machine language is known as the primitive language while Interpreters and compilers like Visual Basic are called high-level language. Some of the high level computer languages beside Visual Basic are Fortran, Cobol, Java, C, C++, Turbo Pascal, and etc .   FORTRAN stands for FORmula TRANslator and it was developed by IBM Inc. between 1954 and 1957 which was used specifically for scientific ad engineering applications. It is still widely used today in the engineering fields. COBOL stands for Common Business Oriented Language which was created by a group of computer manufacturers and industrial computer users in 1959. It was designed for commercial applications that required large amount of data processing. It is still being used today in the business fields. C was developed by Dennis Richie at Bell Laboratories in 1972. It is a system implementation language that was used to develop the UNIX operating system. C++ is an extension of C which was created by Bjarne Stroustrup in 1980's. It added the OOP (Object-Oriented Programming) feature to C and now it is the main systems implementation language (Deitel&Deitel, 1999) . PASCAL was created by Professor Nicklaus Wirth for teaching the concepts of structured programming. Its use is primarily confined to the academic world. JAVA is the latest but one of the hottest programming languages developed by the Sun Microsystems in 1995. It was actually an extension of C++ but it has included extensive libraries for doing multimedia, networking, multithreading , graphics, database access, GUI programming. Microsoft also come out with its own version of Javawhich is known as Visual J++. Other programming languages are Power Builder which was developed by Powersoft Corporation and Delphi which was developed by Borland Inc.

http://www.vbtutor.net/vbtutor.html