home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 5 Developer's Kit / vb5 dev kit.iso / dev / tbexit / frmright.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-01-12  |  7.3 KB  |  221 lines

  1. VERSION 4.00
  2. Begin VB.Form frmRightClick 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   3510
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1515
  7.    ClientWidth     =   5355
  8.    Height          =   3915
  9.    Icon            =   "frmRightClick.frx":0000
  10.    Left            =   1080
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   3510
  13.    ScaleWidth      =   5355
  14.    ShowInTaskbar   =   0   'False
  15.    Top             =   1170
  16.    Visible         =   0   'False
  17.    Width           =   5475
  18.    Begin MsghookLib.Msghook Msghook1 
  19.       Left            =   60
  20.       Top             =   2940
  21.       _version        =   65536
  22.       _extentx        =   873
  23.       _extenty        =   926
  24.       _stockprops     =   0
  25.    End
  26.    Begin VB.Menu mnuShutDown 
  27.       Caption         =   "Shut Down"
  28.       Visible         =   0   'False
  29.       Begin VB.Menu mnuNormShutdown 
  30.          Caption         =   "Shut down the computer"
  31.       End
  32.       Begin VB.Menu mnuRebootSystem 
  33.          Caption         =   "Restart the computer"
  34.       End
  35.       Begin VB.Menu mnuRebootDos 
  36.          Caption         =   "Restart in MS-DOS mode"
  37.       End
  38.       Begin VB.Menu Sep1 
  39.          Caption         =   "-"
  40.       End
  41.       Begin VB.Menu mnuClose 
  42.          Caption         =   "Close WinExit"
  43.       End
  44.       Begin VB.Menu Sep2 
  45.          Caption         =   "-"
  46.       End
  47.       Begin VB.Menu mnuCancel 
  48.          Caption         =   "Cancel"
  49.       End
  50.    End
  51. Attribute VB_Name = "frmRightClick"
  52. Attribute VB_Creatable = False
  53. Attribute VB_Exposed = False
  54. Option Explicit
  55. Private Type OsVersionInfo
  56.     dwVersionInfoSize As Long
  57.     dwMajorVersion As Long
  58.     dwMinorVersion As Long
  59.     dwBuildNumber As Long
  60.     dwPlatform As Long
  61.     szCSDVersion As String * 128
  62. End Type
  63. Private OsVer As OsVersionInfo
  64. Const VER_PLATFORM_WIN32_WINDOWS = 1
  65. Const VER_PLATFORM_WIN32_NT = 2
  66. Private Declare Function GetVersionEx& Lib "kernel32.dll" _
  67. Alias "GetVersionExA" (lpStruct As OsVersionInfo)
  68. 'message constants for mouse events
  69. Const WM_MOUSEMOVE = &H200
  70. Const WM_LBUTTONDOWN = &H201
  71. Const WM_LBUTTONUP = &H202
  72. Const WM_LBUTTONDBLCLK = &H203
  73. Const WM_RBUTTONDOWN = &H204
  74. Const WM_RBUTTONUP = &H205
  75. Const WM_RBUTTONDBLCLK = &H206
  76. Const WM_MBUTTONDOWN = &H207
  77. Const WM_MBUTTONUP = &H208
  78. Const WM_MBUTTONDBLCLK = &H209
  79. Dim TaskBar As New clsTaskBar
  80. Const ICONID = 1
  81. 'Functions and constants for exiting Windows
  82. Private Declare Function AbortSystemShutdown& Lib "advapi32.dll" _
  83. Alias "AbortSystemShutdownA" (ByVal lpMachineName$)
  84. Private Declare Function ExitWindowsEx& Lib "user32" _
  85. (ByVal uFlags&, ByVal dwReserved&)
  86. Const EWX_FORCE = 4
  87. Const EWX_LOGOFF = 0
  88. Const EWX_REBOOT = 2
  89. Const EWX_SHUTDOWN = 1
  90. 'Functions for getting the Windows and Windows\System directories
  91. Private Declare Function GetWindowsDirectory& Lib "kernel32" _
  92. Alias "GetWindowsDirectoryA" (ByVal lpBuffer$, ByVal nSize&)
  93. Private Declare Function GetSystemDirectory& Lib "kernel32" _
  94. Alias "GetSystemDirectoryA" (ByVal lpBuffer$, ByVal nSize&)
  95. Private sWinDir$ 'Windows directory
  96. Private sSysDir$ 'Windows\System directory
  97. Function StandardShutDown() As Long
  98. StandardShutDown = ExitWindowsEx(EWX_SHUTDOWN, 0&)
  99. End Function
  100. Private Sub Form_Load()
  101. Dim lTemp&
  102. Dim iVersion%
  103. Dim sTip$
  104. Dim sBuffer$
  105. Dim lBufferSize&
  106. If App.PrevInstance Then End
  107. 'Determine the version of Windows that is running
  108. OsVer.dwVersionInfoSize = 148&
  109. lTemp = GetVersionEx(OsVer)
  110. Select Case OsVer.dwPlatform
  111.     Case VER_PLATFORM_WIN32_NT
  112.         iVersion = (OsVer.dwMajorVersion * 100) + OsVer.dwMinorVersion
  113.         If iVersion < 351 Then
  114.             MsgBox "This program is not compatible with Windows NT 3.50 and earlier."
  115.             Unload Me
  116.             Exit Sub
  117.         End If
  118.     Case VER_PLATFORM_WIN32_WINDOWS
  119.         'Running Windows 95 so no problem
  120.     Case Else
  121.         'Should only occur if Win32s is installed on Win 3.1, but the
  122.         'program still won't work because there will be no task bar.
  123.         MsgBox "This program can only run under Windows 95 or NT."
  124.         Unload Me
  125. End Select
  126. 'Check for presence of Taskbar.  The user may have a different
  127. 'shell that doesn't support one.
  128. If TaskBar.TaskbarExists <> 1 Then
  129.     MsgBox "There is no taskbar currently available"
  130.     Unload Me
  131.     Exit Sub
  132. End If
  133. 'Enable the MsgHook control to receive messages from the taskbar
  134. Msghook1.HwndHook = Me.hWnd
  135. Msghook1.Message(TaskBar.CallbackMessage) = True
  136. 'Assign the forms' hwnd to the taskbar notify structure
  137. TaskBar.hWnd = Me
  138. 'Now, add an icon to the system tray
  139. TaskBar.AddIconToTray Me.Icon, ICONID
  140. 'Assign the tool tip for the icon
  141. sTip = "Double Click to Exit Windows"
  142. TaskBar.ChangeTip sTip, ICONID
  143. 'Get the Windows and Windows\System directories
  144. sBuffer = Space(145)
  145. lBufferSize = Len(sBuffer)
  146. lTemp = GetWindowsDirectory(sBuffer, lBufferSize)
  147. sWinDir = Left$(sBuffer, lTemp)
  148. sBuffer = Space(145)
  149. lTemp = GetSystemDirectory(sBuffer, lBufferSize)
  150. sSysDir = Left$(sBuffer, lTemp)
  151. End Sub
  152. Private Sub Form_Unload(Cancel As Integer)
  153. 'Remove the icon from the task bar
  154. TaskBar.DeleteIcon (ICONID)
  155. 'Disable subclassing
  156. Msghook1.HwndHook = 0
  157. End Sub
  158. Private Sub mnuCancel_Click()
  159. 'Don't need to do anything here.  Just need a menu item
  160. End Sub
  161. Private Sub mnuClose_Click()
  162. Unload Me
  163. End Sub
  164. Private Sub mnuNormShutdown_Click()
  165. Dim lResult&
  166. lResult = StandardShutDown
  167. End Sub
  168. Private Sub mnuRebootDos_Click()
  169. Dim lResult&
  170. Dim sPifFile$
  171. 'First, let's make sure the pif file exists
  172. sPifFile = sWinDir & "\pif\exit to dos.pif"
  173. If Not FileExists(sPifFile) Then
  174.     'It's not where it's supposed to be, but let's check
  175.     'in the Windows directory.
  176.     sPifFile = sWinDir & "\exit to dos.pif"
  177.     If Not FileExists(sPifFile) Then
  178.         'Still can't find it.  One last check in the System directory
  179.         sPifFile = sSysDir & "\exit to dos.pif"
  180.         If Not FileExists(sPifFile) Then
  181.             'Time to give up
  182.             MsgBox "Can't find ""Exit To Dos.pif"".  Aborting Procedure", vbCritical
  183.             Exit Sub
  184.         End If
  185.     End If
  186. End If
  187. lResult = Shell(sPifFile, 1)
  188. End Sub
  189. Function FileExists(filename$) As Boolean
  190. 'This sub checks for the existance of any filename passed to it
  191. 'Make sure that a filename was passed or the function
  192. 'will return TRUE since it will find any file that exists
  193. 'in the current directory.
  194. If Len(filename) = 0 Then
  195.     FileExists = False
  196.     Exit Function
  197. End If
  198. If Len(Dir$(filename)) Then
  199.     FileExists = True
  200.     FileExists = False
  201. End If
  202. End Function
  203. Private Sub mnuRebootSystem_Click()
  204. Dim lResult&
  205. lResult = ExitWindowsEx(EWX_REBOOT, 0&)
  206. End Sub
  207. Private Sub Msghook1_Message(ByVal msg As Long, ByVal wp As Long, ByVal lp As Long, result As Long)
  208. 'Only trapping one message so don't need to be concerned with the
  209. 'actual message.  The mouse event is contained in lParam, and
  210. 'the icon ID is contained in wParam.  Must use unique IDs for
  211. 'multiple icons.
  212. Dim lResult&
  213. Select Case lp
  214.     'Left button double-click; normal Windows shutdown
  215.     Case WM_LBUTTONDBLCLK
  216.         lResult = StandardShutDown
  217.     Case WM_RBUTTONUP
  218.         PopupMenu mnuShutDown
  219. End Select
  220. End Sub
  221.