gaby90boy Posted June 8, 2007 Report Posted June 8, 2007 This is a sample program showing you how to make a program to time when to shutdown your computer. It uses 1 Form, 1 Module and the source code is listed below. Either download the project below or build a form called frmMain and copy the form source code into and and make a Module called modMain and copy the module source code into it. The properties for your form are listed below. The options in this program are as follows:1. Shutdown your operating system at a certain time2. Turn off your computer at a certain time3. Restart your computer at a certain time4. Log off your computer at a certain time5. Use force to shutdown your computer6. Force your computer to shutdown if it FreezesScreenshot of Software ProgramThe properties for this project is listed below.3 Command Buttons4 Combo Boxes1 Form6 Labels2 List Boxes8 Menus1 Timer1 ModuleYou can also download the http://www.11amdesign.com/visualbasic/shutdown_timer.zipsource code which has the project already built to learn from using Visual Basic 6.Form Source CodeOption ExplicitPrivate Sub btnExit_Click()frmCancelUnload = FalseUnload MeEnd SubPrivate Sub btnTurnOFF_Click()btnTurnON.Enabled = TruebtnTurnOFF.Enabled = FalsemnuPopupTurnON.Enabled = TruemnuPopupTurnOFF.Enabled = FalsecboHour.Enabled = TruecboMinute.Enabled = TruecboSecond.Enabled = TruecboAMPM.Enabled = TruelstOptions.Enabled = TruelstExtra.Enabled = TrueMe.Caption = "Shutdown Timer - OFF"tmrShutdown.Enabled = FalseEnd SubPrivate Sub btnTurnON_Click()btnTurnON.Enabled = FalsebtnTurnOFF.Enabled = TruemnuPopupTurnON.Enabled = FalsemnuPopupTurnOFF.Enabled = TruecboHour.Enabled = FalsecboMinute.Enabled = FalsecboSecond.Enabled = FalsecboAMPM.Enabled = FalselstOptions.Enabled = FalselstExtra.Enabled = FalseMe.Caption = "Shutdown Timer - ON"strShutdown = cboHour.Text & ":" & cboMinute.Text & ":" & cboSecond.Text & " " & cboAMPM.TexttmrShutdown.Enabled = TrueEnd SubPrivate Sub cboAMPM_Click()strShutdown = cboHour.Text & ":" & cboMinute.Text & ":" & cboSecond.Text & " " & cboAMPM.TextEnd SubPrivate Sub cboHour_Click()strShutdown = cboHour.Text & ":" & cboMinute.Text & ":" & cboSecond.Text & " " & cboAMPM.TextEnd SubPrivate Sub cboMinute_Click()strShutdown = cboHour.Text & ":" & cboMinute.Text & ":" & cboSecond.Text & " " & cboAMPM.TextEnd SubPrivate Sub cboSecond_Click()strShutdown = cboHour.Text & ":" & cboMinute.Text & ":" & cboSecond.Text & " " & cboAMPM.TextEnd SubPrivate Sub Form_Load()Dim intCnt As IntegerDim strOptSel As StringDim strExtSel As StringDim strHour As StringDim strMinute As StringDim strSecond As StringDim strAMPM As StringFor intCnt = 1 To 12DoEventscboHour.AddItem intCntNext intCntFor intCnt = 0 To 59DoEventscboMinute.AddItem intCntNext intCntFor intCnt = 0 To 59DoEventscboSecond.AddItem intCntNext intCntWith lstOptions.AddItem "Shutdown OS".AddItem "Turn off Computer".AddItem "Restart".AddItem "Log off"End WithcboAMPM.AddItem "AM"cboAMPM.AddItem "PM"lstExtra.AddItem "Use Force"lstExtra.AddItem "Force only if Freezes"strIniPath = App.Path & "\" & App.Title & ".ini"strOptSel = String(255, vbNullChar)strExtSel = String(255, vbNullChar)Call GetPrivateProfileString("Options", "Selected", 1, strOptSel, 255, strIniPath)Call GetPrivateProfileString("Extra", "Selected", 1, strExtSel, 255, strIniPath)lstOptions.Selected(Int(strOptSel)) = TruelstExtra.Selected(Int(strExtSel)) = TruestrHour = String(255, vbNullChar)strMinute = String(255, vbNullChar)strSecond = String(255, vbNullChar)strAMPM = String(255, vbNullChar)Call GetPrivateProfileString("Shutdown", "Hour", 3, strHour, 255, strIniPath)Call GetPrivateProfileString("Shutdown", "Minute", 15, strMinute, 255, strIniPath)Call GetPrivateProfileString("Shutdown", "Second", 45, strSecond, 255, strIniPath)Call GetPrivateProfileString("Shutdown", "AMPM", "AM", strAMPM, 255, strIniPath)cboHour.Text = strHourcboMinute.Text = strMinutecboSecond.Text = strSecondcboAMPM.Text = strAMPMstrShutdown = cboHour.Text & ":" & cboMinute.Text & ":" & cboSecond.Text & " " & cboAMPM.TextIf IsWinNT = False Then lstExtra.Enabled = FalseEnd SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)Dim xTray As SinglexTray = x / Screen.TwipsPerPixelXSelect Case xTrayCase WM_RBUTTONDOWNCall SetForegroundWindow(Me.hwnd)Call PopupMenu(mnuPopup)Case WM_LBUTTONDBLCLKCall SetForegroundWindow(Me.hwnd)Me.ShowEnd SelectEnd SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)Cancel = frmCancelUnloadIf frmCancelUnload = True ThenMe.WindowState = vbMinimizedMe.HideMe.WindowState = vbNormalEnd IfEnd SubPrivate Sub Form_Unload(Cancel As Integer)Call Shell_NotifyIcon(NIM_DELETE, nid_Tray)Call SavePos(Me, strIniPath)Call WriteINI("Options", "Selected", lstOptions.ListIndex, strIniPath)Call WriteINI("Extra", "Selected", lstExtra.ListIndex, strIniPath)Call WriteINI("Shutdown", "Hour", cboHour.Text, strIniPath)Call WriteINI("Shutdown", "Minute", cboMinute.Text, strIniPath)Call WriteINI("Shutdown", "Second", cboSecond.Text, strIniPath)Call WriteINI("Shutdown", "AMPM", cboAMPM.Text, strIniPath)End SubPrivate Sub lstExtra_Click()lstExtra.Selected(lstExtra.ListIndex) = TrueEnd SubPrivate Sub lstExtra_ItemCheck(Item As Integer)Dim iLst As IntegerFor iLst = 0 To (lstExtra.ListCount - 1)If iLst <> Item Then lstExtra.Selected(iLst) = FalseNext iLstEnd SubPrivate Sub lstOptions_Click()lstOptions.Selected(lstOptions.ListIndex) = TrueEnd SubPrivate Sub lstOptions_ItemCheck(Item As Integer)Dim iLst As IntegerFor iLst = 0 To (lstOptions.ListCount - 1)DoEventsIf iLst <> Item Then lstOptions.Selected(iLst) = FalseNext iLstEnd SubPrivate Sub mnuPopup_Click()Select Case Me.VisibleCase TruemnuPopupHide.Enabled = TruemnuPopupShow.Enabled = FalseCase FalsemnuPopupHide.Enabled = FalsemnuPopupShow.Enabled = TrueEnd SelectEnd SubPrivate Sub mnuPopupExit_Click()Call btnExit_ClickEnd SubPrivate Sub mnuPopupHide_Click()Me.HideEnd SubPrivate Sub mnuPopupShow_Click()Me.ShowEnd SubPrivate Sub mnuPopupTurnOFF_Click()Call btnTurnOFF_ClickEnd SubPrivate Sub mnuPopupTurnON_Click()Call btnTurnON_ClickEnd SubPrivate Sub tmrShutdown_Timer()Dim lngFlags As LongIf FormatDateTime(strShutdown, vbLongTime) = FormatDateTime(Time, vbLongTime) ThenSelect Case lstOptions.ListIndexCase 0 'Shutdown OSlngFlags = EWX_SHUTDOWNCase 1 'Turn off SystemlngFlags = EWX_POWEROFFCase 2 'RestartlngFlags = EWX_REBOOTCase 3 'LogofflngFlags = EWX_LOGOFFEnd SelectSelect Case lstExtra.ListIndexCase 0 'Use forcelngFlags = lngFlags Or EWX_FORCECase 1 'Force only if freezeslngFlags = lngFlags Or EWX_FORCEIFHUNGEnd SelectIf IsWinNT = True Then Call EnableNTShutdownCall ExitWindowsEx(lngFlags, 0)Call btnTurnOFF_ClickEnd IfEnd SubModule Source CodeOption ExplicitPublic Const ANYSIZE_ARRAY As Long = 1Public Const EWX_FORCE As Long = 4Public Const EWX_FORCEIFHUNG As Long = &H10Public Const EWX_LOGOFF As Long = 0Public Const EWX_POWEROFF As Long = &H8Public Const EWX_REBOOT As Long = 2Public Const EWX_SHUTDOWN As Long = 1Public Const MAX_COMPUTERNAME As Long = 15Public Const SE_PRIVILEGE_ENABLED As Long = &H2Public Const TOKEN_ADJUST_DEFAULT As Long = &H80Public Const TOKEN_ADJUST_GROUPS As Long = &H40Public Const TOKEN_ADJUST_PRIVILEGES As Long = &H20Public Const TOKEN_ADJUST_SESSIONID As Long = &H100Public Const TOKEN_QUERY As Long = &H8Public Const VER_PLATFORM_WIN32_NT As Long = 2Public Const NIF_ICON = &H2Public Const NIF_MESSAGE = &H1Public Const NIF_TIP = &H4Public Const NIM_ADD = &H0Public Const NIM_DELETE = &H2Public Const NIM_MODIFY = &H1Public Const WM_LBUTTONDBLCLK As Long = &H203Public Const WM_MOUSEMOVE As Long = &H200Public Const WM_RBUTTONDOWN As Long = &H204Public Const HWND_TOPMOST As Long = -1Public Const SWP_NOMOVE As Long = &H2Public Const SWP_NOSIZE As Long = &H1Public Type LARGE_INTEGERLowPart As LongHighPart As LongEnd TypePublic Type LUIDLowPart As LongHighPart As LongEnd TypePublic Type LUID_AND_ATTRIBUTESpLuid As LUIDAttributes As LongEnd TypePublic Type OSVERSIONINFOdwOSVersionInfoSize As LongdwMajorVersion As LongdwMinorVersion As LongdwBuildNumber As LongdwPlatformId As LongszCSDVersion As String * 128 ' Maintenance string for PSS usageEnd TypePublic Type TOKEN_PRIVILEGESPrivilegeCount As LongPrivileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTESEnd TypePublic Type NOTIFYICONDATAcbSize As Longhwnd As LonguID As LonguFlags As LonguCallbackMessage As LonghIcon As LongszTip As String * 64End Type'ADVAPI32Public Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" ( _ByVal lpSystemName As String, _ByVal lpName As String, _ByRef lpLuid As LUID) As Long 'change lpLuid from LARGE_INTEGER to LUIDPublic Declare Function AdjustTokenPrivileges Lib "advapi32.dll" ( _ByVal TokenHandle As Long, _ByVal DisableAllPrivileges As Long, _ByRef NewState As TOKEN_PRIVILEGES, _ByVal BufferLength As Long, _ByRef PreviousState As TOKEN_PRIVILEGES, _ByRef ReturnLength As Long) As LongPublic Declare Function OpenProcessToken Lib "advapi32.dll" ( _ByVal ProcessHandle As Long, _ByVal DesiredAccess As Long, _ByRef TokenHandle As Long) As Long'COMCTL32Public Declare Sub InitCommonControls Lib "comctl32.dll" ()'KERNEL32Public Declare Function GetVersionEx Lib "kernel32.dll" Alias "GetVersionExA" ( _ByRef lpVersionInformation As OSVERSIONINFO) As LongPublic Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" ( _ByVal lpBuffer As String, _ByRef nSize As Long) As LongPublic Declare Function GetCurrentProcess Lib "kernel32.dll" () As Long'USER32Public Declare Function ExitWindowsEx Lib "user32.dll" ( _ByVal uFlags As Long, _ByVal dwReserved As Long) As LongPublic Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" ( _ByVal lpApplicationName As String, _ByVal lpKeyName As String, _ByVal lpDefault As String, _ByVal lpReturnedString As String, _ByVal nSize As Long, _ByVal lpFileName As String) As LongPublic Declare Function SetWindowPos Lib "user32.dll" ( _ByVal hwnd As Long, _ByVal hWndInsertAfter As Long, _ByVal x As Long, _ByVal y As Long, _ByVal cx As Long, _ByVal cy As Long, _ByVal wFlags As Long) As LongPublic Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" ( _ByVal lpApplicationName As String, _ByVal lpKeyName As Any, _ByVal lpString As Any, _ByVal lpFileName As String) As LongPublic Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As LongPublic Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongPublic OSVerInfo As OSVERSIONINFOPublic nid_Tray As NOTIFYICONDATAPublic frmCancelUnload As BooleanPublic strIniPath As StringPublic strShutdown As StringPublic Sub Main()Dim strBuffLeft As StringDim strBuffTop As StringDim lngFlags As LongDim blnTrig As BooleanIf App.PrevInstance = True Then EndCall InitCommonControlsIf Command <> "" ThenIf InStr(1, Command, "shutdown") <> 0 ThenlngFlags = EWX_SHUTDOWNblnTrig = TrueElseIf InStr(1, Command, "poweroff") <> 0 ThenlngFlags = EWX_POWEROFFblnTrig = TrueElseIf InStr(1, Command, "reboot") <> 0 ThenlngFlags = EWX_REBOOTblnTrig = TrueElseIf InStr(1, Command, "logoff") <> 0 ThenlngFlags = EWX_LOGOFFblnTrig = TrueEnd IfIf InStr(1, Command, "force") <> 0 ThenlngFlags = lngFlags Or EWX_FORCEElseIf InStr(1, Command, "forceifhung") <> 0 ThenlngFlags = lngFlags Or EWX_FORCEIFHUNGEnd IfIf blnTrig = True ThenIf IsWinNT = True Then Call EnableNTShutdownCall ExitWindowsEx(lngFlags, 0)EndEnd IfEnd IfLoad frmMainWith nid_Tray.cbSize = Len(nid_Tray).hIcon = frmMain.Icon.hwnd = frmMain.hwnd.szTip = frmMain.Caption & vbNullChar.uCallbackMessage = WM_MOUSEMOVE.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP.uID = vbNullEnd WithCall Shell_NotifyIcon(NIM_ADD, nid_Tray)frmCancelUnload = True 'cancel unload by defaultstrBuffLeft = String(255, vbNullChar)strBuffTop = String(255, vbNullChar)strIniPath = App.Path & "\" & App.Title & ".ini"Call GetPrivateProfileString("Position", "Left", 0, strBuffLeft, 255, strIniPath)Call GetPrivateProfileString("Position", "Top", 0, strBuffTop, 255, strIniPath)frmMain.Left = strBuffLeftfrmMain.Top = strBuffTopCall SetWindowPos(frmMain.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)frmMain.ShowEnd SubPublic Sub WriteINI(strSection As String, strKey As String, strValue As String, strPath As String)Call WritePrivateProfileString(strSection, strKey, strValue, strPath)End SubPublic Sub SavePos(frmSave As Form, strPath As String)If frmSave.WindowState = vbNormal ThenCall WriteINI("Position", "Left", frmSave.Left, strPath)Call WriteINI("Position", "Top", frmSave.Top, strPath)End IfEnd SubPublic Function IsWinNT() As BooleanOSVerInfo.dwOSVersionInfoSize = Len(OSVerInfo)Call GetVersionEx(OSVerInfo)If OSVerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT Then IsWinNT = TrueEnd FunctionPublic Sub EnableNTShutdown()Dim TknPriv_Old As TOKEN_PRIVILEGESDim TknPriv_New As TOKEN_PRIVILEGESDim LUID_NTShutdown As LUIDDim CurProc As LongDim TknHnd As LongCurProc = GetCurrentProcessCall OpenProcessToken(CurProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, TknHnd)Call LookupPrivilegeValue(CompName, "SeShutdownPrivilege", LUID_NTShutdown)TknPriv_Old.PrivilegeCount = 1TknPriv_Old.Privileges(0).Attributes = SE_PRIVILEGE_ENABLEDTknPriv_Old.Privileges(0).pLuid = LUID_NTShutdownCall AdjustTokenPrivileges(TknHnd, False, TknPriv_Old, 4 + (12 * TknPriv_Old.PrivilegeCount), TknPriv_New, 4 + (12 * TknPriv_New.PrivilegeCount))End SubPublic Function CompName() As StringDim lngInStr As LongCompName = String(MAX_COMPUTERNAME, vbNullChar)Call GetComputerName(CompName, MAX_COMPUTERNAME + 1)lngInStr = InStr(1, CompName, vbNullChar) 'error protectionIf lngInStr <> 0 Then CompName = Mid(CompName, 1, lngInStr - 1)End Function Quote