vbAccelerator - Contents of code file: fAutoTest.frm

VERSION 5.00
Begin VB.Form frmAutoTest 
   Caption         =   "Auto-Start Demonstration Application"
   ClientHeight    =   4005
   ClientLeft      =   4560
   ClientTop       =   2025
   ClientWidth     =   5820
   Icon            =   "fAutoTest.frx":0000
   LinkTopic       =   "Form1"
   ScaleHeight     =   4005
   ScaleWidth      =   5820
   Begin VB.CommandButton cmdCancel 
      Cancel          =   -1  'True
      Caption         =   "Cancel"
      Height          =   375
      Left            =   4440
      TabIndex        =   7
      Top             =   3540
      Width           =   1335
   End
   Begin VB.CommandButton cmdOK 
      Caption         =   "OK"
      Default         =   -1  'True
      Height          =   375
      Left            =   3000
      TabIndex        =   6
      Top             =   3540
      Width           =   1335
   End
   Begin VB.Frame fraSep 
      Height          =   75
      Left            =   0
      TabIndex        =   5
      Top             =   3360
      Width           =   5895
   End
   Begin VB.OptionButton optAutoStart 
      Caption         =   "Start this application &Every time Windows starts"
      Height          =   315
      Index           =   2
      Left            =   300
      TabIndex        =   4
      Top             =   1680
      Width           =   3735
   End
   Begin VB.OptionButton optAutoStart 
      Caption         =   "Start this application &Once when Windows next
       re-starts"
      Height          =   315
      Index           =   1
      Left            =   300
      TabIndex        =   3
      Top             =   1380
      Width           =   4935
   End
   Begin VB.OptionButton optAutoStart 
      Caption         =   "&Don't Auto-start"
      Height          =   315
      Index           =   0
      Left            =   300
      TabIndex        =   2
      Top             =   1080
      Value           =   -1  'True
      Width           =   3735
   End
   Begin VB.Label Label1 
      BackStyle       =   0  'Transparent
      Caption         =   "VB Source Code and Tips at www.dogma.demon.co.uk"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00FFFFFF&
      Height          =   495
      Left            =   2760
      TabIndex        =   1
      Top             =   240
      Width           =   2835
   End
   Begin VB.Image imgLogo 
      Height          =   660
      Left            =   120
      Picture         =   "fAutoTest.frx":014A
      Top             =   120
      Width           =   2535
   End
   Begin VB.Label lblBlack 
      BackColor       =   &H00000000&
      BorderStyle     =   1  'Fixed Single
      Height          =   795
      Left            =   60
      TabIndex        =   0
      Top             =   60
      Width           =   5655
   End
End
Attribute VB_Name = "frmAutoTest"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Public Enum eAutoRunTypes
    eNever
    eOnce
    eAlways
End Enum

Public Property Let AutoRun(ByVal eType As eAutoRunTypes)
Dim sExe As String

    sExe = App.Path
    If (Right$(sExe, 1) <> "\") Then sExe = sExe & "\"
    sExe = sExe & App.EXEName
    
    Dim cR As New cRegistry
    cR.ClassKey = HKEY_LOCAL_MACHINE
    If (eType = eNever) Then
        ' Remove entry from always Run if it is there:
        cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run"
        cR.ValueKey = App.EXEName
        On Error Resume Next
        cR.DeleteValue
        Err.Clear
        ' Remove entry from RunOnce if it is there:
        cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\RunOnce"
        On Error Resume Next
        cR.DeleteValue
        Err.Clear
    ElseIf eType = eOnce Then
        ' Remove entry from always Run if it is there:
        cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run"
        cR.ValueKey = App.EXEName
        On Error Resume Next
        cR.DeleteValue
        Err.Clear
        ' Add an entry to RunOnce (or just ensure the exe name and path
        ' is correct if it is already there):
        cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\RunOnce"
        cR.ValueKey = App.EXEName
        cR.ValueType = REG_SZ
        cR.Value = sExe
    Else
        ' Remove entry from RunOnce if it is there:
        cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\RunOnce"
        cR.ValueKey = App.EXEName
        On Error Resume Next
        cR.DeleteValue
        Err.Clear
        ' Add an entry to RunOnce (or just ensure the exe name and path
        ' is correct if it is already there):
        cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run"
        cR.ValueKey = App.EXEName
        cR.ValueType = REG_SZ
        cR.Value = sExe
    End If
        
End Property
Public Property Get AutoRun() As eAutoRunTypes
    Dim cR As New cRegistry
    cR.ClassKey = HKEY_LOCAL_MACHINE
    cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run"
    cR.ValueKey = App.EXEName
    cR.Default = "?"
    cR.ValueType = REG_SZ
    If (cR.Value = "?") Then
        cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\RunOnce"
        If (cR.Value = "?") Then
            AutoRun = eNever
        Else
            AutoRun = eOnce
        End If
    Else
        AutoRun = eAlways
    End If
End Property

Private Sub cmdCancel_Click()
    Unload Me
End Sub

Private Sub cmdOK_Click()
    If (optAutoStart(0).Value) Then
        ' remove from run settings:
        AutoRun = eNever
    ElseIf (optAutoStart(1).Value) Then
        ' run once:
        AutoRun = eOnce
    Else
        ' run every time:
        AutoRun = eAlways
    End If
    Unload Me
End Sub

Private Sub Form_Load()
    optAutoStart(AutoRun).Value = True
End Sub