Visual Basic 6.0 Tutorial & Tips

Get some help of Visual Basic 6.0 and Tips

Hi all,

I am Harun. I have skilled in Visual Basic. One day I wished to make a multimedia application. But I would not know any programming language. In 1999, I wished to make a mulimedia player which can be shown me scrolling my added text and picture. For this, I started to research many programs. Firstly, I used code of Flash. Then, I used the Visual Basic 5.0. I thought that It will be difficalt for me when first I used this programming language. Actualy, It was a wonderfull programming language. Because It fullfilled my wish. I can't imagine that I will be success to make this application easily. Now following codes I have tried to expline some basic code of Visual Basic 6.0. I hope you will get help form this tutorial who want to learn Visual Basic 6.0.

 

Visual Basic 6.0

 

Start Visual Basic First Day

You can use VB6 simple appliaction? No problem. First install your vb6 on your pc. click start menu > programs > Microsoft Visual Studio 6.0 > Microsoft Visual Basic 6.0 then open a vb6. Select 'Standard exe' on New project dialog box. click Open button.

  • Insert a CommandButton of the left Tools Box and Draw
  • Dobble Click in Command1 (Inserted CommandButton) or Right Click on Mouse, show popup menu, click view code, type following code:

Private Sub Form_Load()
Command1.Caption = "Welcome"
End sub

Private Sub Command1_Click()
Msgbox "Welcome my First Application", vbInformation
End Sub

  • Press F5 to Run your application.

 

Using Gradient your Form

Private Sub Form_Resize()
    Gradient Me, 200, 100, 300, 0
End Sub

Sub Gradient(TheObject As Object, ByVal Redval As Long, ByVal Greenval As Long, ByVal Blueval As Long, ByVal Direction As Integer)
Dim Step As Integer, Reps As Integer, FillTop As Integer
Dim FillLeft As Integer, FillRight As Integer, FillBottom As Integer
If Direction < 1 Or Direction > 4 Then Direction = 1
FillTop = 0
FillLeft = 0
If Direction < 3 Then
Step = (TheObject.Height / 100)
If Direction = 2 Then FillTop = TheObject.Height - Step
FillBottom = FillTop + Step
FillRight = TheObject.Width
Else
Step = (TheObject.Width / 100)
If Direction = 4 Then FillLeft = TheObject.Width - Step
FillRight = FillLeft + Step
FillBottom = TheObject.Height
End If
For Reps = 1 To 100
If Direction = 2 And Reps = 100 Then FillTop = 0
If Direction = 4 And Reps = 100 Then FillLeft = 0
Redval = Redval - 3
Greenval = Greenval - 3
Blueval = Blueval - 3
If Redval <= 0 Then Redval = 0
If Greenval <= 0 Then Greenval = 0
If Blueval <= 0 Then Blueval = 0
TheObject.Line (FillLeft, FillTop)-(FillRight, FillBottom), RGB(Redval, Greenval, Blueval), BF
If Direction < 3 Then
If Direction = 1 Then
FillTop = FillBottom
Else
FillTop = FillTop - Step
End If
FillBottom = FillTop + Step
Else
If Direction = 3 Then
FillLeft = FillRight
Else
FillLeft = FillLeft - Step
End If
FillRight = FillLeft + Step
End If
Next Reps
End Sub

Using Opacity your Form

Private Declare Function GetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const LWA_ALPHA = &H2&

Private Function Opacity(Value As Byte, Frm As Form)
On Error GoTo ErrorHandler
    Dim MaxVal As Byte, MinVal As Byte
    MinVal = 20: MaxVal = 255
    If Value > MaxVal Then Value = MaxVal
    If Value < MinVal Then Value = MinVal
    SetWindowLongA Frm.hwnd, GWL_EXSTYLE, GetWindowLongA(Frm.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
    SetLayeredWindowAttributes Frm.hwnd, 0, Value, LWA_ALPHA

ErrorHandler:     Exit Function
End Function

Private Sub Form_Load()
    Opacity 150, Me
End Sub

Create Search Engine of your pc

  • Added: Drive1, Dir1, File1, List1, Command1, Text1, Label1
  • Type Following Code:

Dim SearchFlag As Integer

Private Sub Command1_Click()
Dim FirstPath As String, DirCount As Integer, NumFiles As Integer
Dim result As Integer
    If Dir1.Path <> Dir1.List(Dir1.ListIndex) Then
        Dir1.Path = Dir1.List(Dir1.ListIndex)
        Exit Sub
    End If
    FirstPath = Dir1.Path
    DirCount = Dir1.ListCount
    NumFiles = 0
    result = DirDiver(FirstPath, DirCount, "")
    File1.Path = Dir1.Path

End Sub

Private Sub Dir1_Change()
    File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
End Sub

Private Sub Form_Load()
    Me.Width = 6570
    Me.Height = 4695
    Drive1.Top = 120
    Drive1.Left = 120
    Drive1.Width = 2295
    Dir1.Top = 480
    Dir1.Left = 120
    Dir1.Width = 2295
    Dir1.Height = 1665
    File1.Top = 120
    File1.Left = 2520
    File1.Width = 3855
    File1.Height = 2040
    Label1.Left = 120
    Label1.Top = 2280
    Label1.Width = 735
    Text1.Top = 2160
    Text1.Left = 960
    Text1.Width = 1575
    Text1.Height = 405
    Command1.Left = 4320
    Command1.Top = 2160
    Command1.Height = 495
    Command1.Width = 1215
    List1.Top = 2640
    List1.Left = 120
    List1.Width = 6255
    List1.Height = 1425
    Command1.Caption = "Search"
    Text1.Text = "*.dat;*.mpg"
    File1.Pattern = Text1.Text
    Label1.Caption = "Filter:"
End Sub

Private Function DirDiver(NewPath As String, DirCount As Integer, BackUp As String) As Integer
Static FirstErr As Integer
Dim DirsToPeek As Integer, AbandonSearch As Integer, ind As Integer
Dim OldPath As String, ThePath As String, entry As String
Dim retval As Integer
    SearchFlag = True
    DirDiver = False
    retval = DoEvents()
    If SearchFlag = False Then
        DirDiver = True
        Exit Function
    End If
    On Local Error GoTo DirDriverHandler
    DirsToPeek = Dir1.ListCount
    Do While DirsToPeek > 0 And SearchFlag = True
        OldPath = Dir1.Path
        Dir1.Path = NewPath
        If Dir1.ListCount > 0 Then
            Dir1.Path = Dir1.List(DirsToPeek - 1)
            AbandonSearch = DirDiver((Dir1.Path), DirCount%, OldPath)
        End If
        DirsToPeek = DirsToPeek - 1
        If AbandonSearch = True Then Exit Function
    Loop
    If File1.ListCount Then
        If Len(Dir1.Path) <= 3 Then
            ThePath = Dir1.Path
        Else
            ThePath = Dir1.Path + "\"
        End If
        For ind = 0 To File1.ListCount - 1
            entry = ThePath + File1.List(ind)
            List1.AddItem entry
        Next ind
    End If
    If BackUp <> "" Then
        Dir1.Path = BackUp
    End If
    Exit Function
DirDriverHandler:
    If Err = 7 Then
        DirDiver = True
        MsgBox "You've filled the list box. Abandoning search..."
        Exit Function
    Else
        MsgBox Error
        End
    End If
End Function

Private Sub Text1_Change()
    File1.Pattern = Text1.Text
End Sub

Using Flip Picture

  • Add a PictureBox and CommanButton
  • Load a picture in PictureBox (Go properties and click alpabetic tap, choise Picture: ....)

Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Private Const SRCCOPY = &HCC0020

Private Sub Command1_Click()
Picture1.ScaleMode = 3
'flip horizontal
StretchBlt Picture1.hdc, Picture1.ScaleWidth, 0, Picture1.ScaleWidth * -1, Picture1.ScaleHeight, Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SRCCOPY
'flip vertically
StretchBlt Picture1.hdc, 0, Picture1.ScaleHeight, Picture1.ScaleWidth, Picture1.ScaleHeight * -1, Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SRCCOPY
End Sub

Private Sub Form_Load()
Command1.Caption = "Toogle Flip"
End Sub

Added list in your install fonts

  • Add a ListBox (Name: List1)

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

Using scrolling text

  • Added: Label, TextBox, Timer

Dim Txtwidth As Integer

Private Sub Form_Resize()
Me.WindowState = 2
Text1.Font = "Arial"
Text1.FontSize = 24
Text1.Height = 500
Text1.Text = "Add your text"
Label1.Caption = Text1.Text
Label1.AutoSize = True
Label1.FontSize = 24
Text1.Width = Label1.Width
Text1.Top = Me.Height / 2
Timer1.Interval = 1
Label1.Visible = False
End Sub

Private Sub Label1_Change()
Txtwidth = Label1.Width
End Sub

Private Sub Text1_Change()
Label1.Caption = Text1.Text
Text1.Width = Label1.Width + 100
End Sub

Private Sub Timer1_Timer()
'scrolling text
Text1.Left = Text1.Left - 10
If Text1.Left <= -Txtwidth Then Text1.Left = Me.Width
End Sub

Drag and Drop windows file to VB6 application

  • Include: ListBox

Private Sub Form_Load()
List1.OLEDropMode = 1
End Sub

Private Sub List1_Click()
Me.Caption = List1.List(List1.ListIndex)
End Sub

Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
Dim i As Integer
If Data.GetFormat(vbCFFiles) Then
For i = 1 To Data.Files.Count
List1.AddItem Data.Files(i), List1.ListCount
Next
End If
End Sub

How to Get Running Application List

  • Include: Command Botton- 1, List Box- 1

Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const MAX_PATH As Integer = 260

Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type

Private Declare Function CreateToolhelpSnapshot Lib "Kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "Kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "Kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)

Private Sub Command1_Click()
List1.Clear
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim z As Long
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapShot = 0 Then
Exit Sub
End If
uProcess.dwSize = Len(uProcess)
z = ProcessFirst(hSnapShot, uProcess)
Do While z
List1.AddItem uProcess.szExeFile
z = ProcessNext(hSnapShot, uProcess)
Loop
Call CloseHandle(hSnapShot)
End Sub

Private Sub Form_Load()
Command1.Caption = "Get List"
End Sub

How to Break or Pause My Application

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Form_Click()
Sleep 5000
End Sub

Private Sub Form_Resize()
Print "Click Here"
Print "And Move or Minimize or Maximize ....... Don't"
Print "Wakeup ....."
End Sub

How to App Running Then New Loaded App Exit

Private Sub Form_Load()
If App.PrevInstance Then End
End Sub

How to Get Alart Message when close my app

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim sText As Integer
sText = MsgBox("Are you sure exit this program?", vbQuestion + vbYesNo, "Exit Massange")
If sText = vbNo Then Cancel = -1
End Sub

Tips Corner

Change "Recycle Bin" Name?

  • First click on Start button > click on Run option. or Press Log+R
  • Type regedit.exe and press enter
  • In regedit editor you will open HKEY_CLASSES_ROOT folder, In this folder open the CLSID folder then open the {645FF040-5081-101B-9F08-00AA002F954E} folder >ShellFolder folder at last.
  • Here simply change the data value from "40 01 00 20" to "70 01 00 20".

Hide or Show Drive of My Computer

Click Start Menu > Run.... Type: regedit...... and Goto...
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer 


   Create Data: NoDrives 
   Data Type: REG_DWORD 
   Value: see below 
   
To calculate the value, add up the following codes for the drives that you want to hide: 
   
  A=1 
  B=2 
  C=4 
  D=8 
  E=16 
  F=32

Disable Low Disk space Message

To disable this feature, edit the following in the registry   


HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer 


Name: NoLowDiskSpaceChecks 
Data Type: REG_DWORD 
Value: 1

Enable/Disable ZIP Support

If you have upgraded to Windows XP then you will probably have used a program like "WinZip" to open any Zip files you have. 
   
With Windows XP, Microsoft have given a built-in Zip feature. However, this new built-in XP Zip support just tends to get in the way and is no where near as feature laden as WinZip. 
 
So the good news is that you can disable the Windows XP built-in Zip support! (and enable it again if you ever want to!) 
 
To Disable: 
 
 1) Start Menu > Run 
 
 2) Type: 
 
regsvr32 /u %windir%\system32\zipfldr.dll 
 
 
To Enable: 
 
 1) Start Menu > Run 
 
 2) Type: 
 
 regsvr32 %windir%\system32\zipfldr.dll 

Disable Cleanup Warnning


HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
 
Name: NoLowDiskSpaceChecks 
Data Type: REG_DWORD 
Value: 1

Don't group programs on TaskBar

 Many XP newcomers find the grouping of similar taskbar buttons to be an annoyance (I do). To rid yourself of this nonsense, just right-click an empty space (not a button) on your taskbar, then Properties. On the pop-up menu, uncheck "Group similar taskbar buttons". 
 Now we're back to enjoying the cluttered taskbar.

Unkhown Password, but how to start winxp sp-2?

   Reboot your pc.
   Keypress F8 on Startup winxp...
   selected: Save Mode
   and Keypress ENTER
   // Now Start Windows XP-2 without password.
   
   Goto Control Panel
   click: User Accounts
   click: user name on Pick an account to change
   click: change my password
   // Enter your new pass and confir pass........
// or Remove current password
// Now reboot your pc.....enjoy.