home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / articles / vbbultn / source32 / ctrl_esc.frm (.txt) next >
Encoding:
Visual Basic Form  |  1996-01-24  |  7.9 KB  |  202 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H80000005&
  5.    BorderStyle     =   1  'Fixed Single
  6.    Caption         =   "Ctrl Esc Blocker"
  7.    ClientHeight    =   2325
  8.    ClientLeft      =   1095
  9.    ClientTop       =   1485
  10.    ClientWidth     =   4440
  11.    BeginProperty Font 
  12.       name            =   "MS Sans Serif"
  13.       charset         =   0
  14.       weight          =   700
  15.       size            =   8.25
  16.       underline       =   0   'False
  17.       italic          =   0   'False
  18.       strikethrough   =   0   'False
  19.    EndProperty
  20.    ForeColor       =   &H80000008&
  21.    Height          =   2730
  22.    Left            =   1035
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   2325
  25.    ScaleWidth      =   4440
  26.    Top             =   1140
  27.    Width           =   4560
  28.    Begin VB.CheckBox Check1 
  29.       Appearance      =   0  'Flat
  30.       BackColor       =   &H80000005&
  31.       Caption         =   "Block Ctrl-Esc"
  32.       ForeColor       =   &H80000008&
  33.       Height          =   285
  34.       Left            =   120
  35.       TabIndex        =   3
  36.       Top             =   1260
  37.       Value           =   1  'Checked
  38.       Width           =   1575
  39.    End
  40.    Begin VB.CommandButton Exit 
  41.       Appearance      =   0  'Flat
  42.       BackColor       =   &H80000005&
  43.       Caption         =   "Exit"
  44.       Height          =   555
  45.       Left            =   3000
  46.       TabIndex        =   0
  47.       Top             =   1680
  48.       Width           =   1305
  49.    End
  50.    Begin DwshkLibDemo.WinHook WinHook2 
  51.       Left            =   690
  52.       Top             =   1680
  53.       _Version        =   262144
  54.       _ExtentX        =   847
  55.       _ExtentY        =   847
  56.       _StockProps     =   0
  57.       Notify          =   0
  58.       RegMessage1     =   ""
  59.       RegMessage2     =   ""
  60.       RegMessage3     =   ""
  61.       RegMessage4     =   ""
  62.       RegMessage5     =   ""
  63.       Monitor         =   6
  64.       HookType        =   4
  65.       Messages        =   "CTRL_ESC.frx":0000
  66.       Keys            =   "CTRL_ESC.frx":042C
  67.    End
  68.    Begin DwshkLibDemo.WinHook WinHook1 
  69.       Left            =   120
  70.       Top             =   1680
  71.       _Version        =   262144
  72.       _ExtentX        =   847
  73.       _ExtentY        =   847
  74.       _StockProps     =   0
  75.       Notify          =   0
  76.       RegMessage1     =   ""
  77.       RegMessage2     =   ""
  78.       RegMessage3     =   ""
  79.       RegMessage4     =   ""
  80.       RegMessage5     =   ""
  81.       Monitor         =   6
  82.       HookEnabled     =   -1  'True
  83.       Messages        =   "CTRL_ESC.frx":0454
  84.       Keys            =   "CTRL_ESC.frx":0884
  85.    End
  86.    Begin VB.Label Label2 
  87.       Appearance      =   0  'Flat
  88.       BackColor       =   &H80000005&
  89.       Caption         =   "This allows you to bring up the system Task List only from the System-Switch To menu command."
  90.       ForeColor       =   &H80000008&
  91.       Height          =   405
  92.       Left            =   90
  93.       TabIndex        =   2
  94.       Top             =   690
  95.       Width           =   4245
  96.    End
  97.    Begin VB.Label Label1 
  98.       Appearance      =   0  'Flat
  99.       BackColor       =   &H80000005&
  100.       Caption         =   "This sample program prevents the Ctl+Esc key from bringing up the system TaskList."
  101.       ForeColor       =   &H80000008&
  102.       Height          =   465
  103.       Left            =   360
  104.       TabIndex        =   1
  105.       Top             =   90
  106.       Width           =   3675
  107.    End
  108. Attribute VB_Name = "Form1"
  109. Attribute VB_Creatable = False
  110. Attribute VB_Exposed = False
  111. Option Explicit
  112. Private Declare Sub dwDWORDto2Integers Lib "dwspy32.dll" (ByVal l&, lw%, lh%)
  113. Private Declare Function GetVersion Lib "kernel32.dll" () As Long
  114. Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long
  115. Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
  116. Dim foregroundwin As Long
  117. Const WM_SYSCOMMAND = &H112&
  118. Const WM_HOTKEY = &H312&
  119. Const SC_TASKLIST = &HF130&
  120. Const MOD_CONTROL = &H2
  121. Const VK_ESCAPE = &H1B
  122. Private Sub Check1_Click()
  123.     If Check1.Value Then
  124.         ' prevent the Ctl+Esc key from bringing up the task list
  125.         WinHook1.HookEnabled = True
  126.     Else
  127.         WinHook1.HookEnabled = False
  128.     End If
  129. End Sub
  130. Private Sub Exit_Click()
  131.     Unload Me
  132. End Sub
  133. Private Sub Form_Load()
  134. Dim loword As Integer, hiword As Integer, l As Long
  135.     If Check1.Value Then
  136.         ' prevent the Ctl+Esc key from bringing up the task list
  137.         WinHook1.HookEnabled = True
  138.     Else
  139.         WinHook1.HookEnabled = False
  140.     End If
  141.     ' check if Windows NT
  142.     l = GetVersion()
  143. Debug.Print Hex$(l)
  144.     dwDWORDto2Integers l, loword, hiword
  145.     If hiword And &H8000 Then Exit Sub  ' not Windows NT
  146.     WinHook2.HookEnabled = True
  147. End Sub
  148. Private Sub WinHook1_WndMessage(wnd As Long, msg As Long, wp As Long, lp As Long, nodef As Integer)
  149. Dim loword As Integer, hiword As Integer, l As Long
  150.     ' So far, we found two situations that may occur when the user presses the Ctl+Esc key.
  151.     ' In Windows 95, a WM_SYSCOMMAND message is sent to the Program Manager application (yes
  152.     ' there is such a thing, it's just hidden) to open the "Start" menu. In Windows NT, a
  153.     ' WM_HOTKEY message is sent to the Task Manager application (it also exists, but is also
  154.     ' hidden) to display the Task List. Also, if you double-click the desktop in Windows NT,
  155.     ' a WM_SYSCOMMAND with the SC_TASKLIST flag will be generated. This situation is handled
  156.     ' exactly the same as if the user hit Ctl+Esc in Windows 95. This application shows how
  157.     ' to discard these messages and prevent the default Ctl+Esc command from happening.
  158.     ' You cannot use tbe Keyboard Hook to throw the Ctrl-Esc key message away
  159.     ' because these messages are sent before the Key message.
  160.     If msg = WM_SYSCOMMAND Then
  161.         If wp = SC_TASKLIST Then
  162.             ' Set nodef to True to prevent other windows hooks from processing the message
  163.             ' and set msg to 0 (Windows will ignore the message if it is 0).  Refer to the
  164.             ' SpyWorks-VB manual's section on the nodef parameter in SBCHOOK.VBX on why
  165.             ' we cannot just set nodef = True.
  166.             nodef = True
  167.             msg = 0
  168.         End If
  169.     End If
  170.     If msg = WM_HOTKEY Then
  171.         dwDWORDto2Integers lp, loword, hiword
  172.         ' Make sure it's the Ctl+Esc keys
  173.         If (loword = MOD_CONTROL) And (hiword = VK_ESCAPE) Then
  174.             ' Set nodef to True to prevent other windows hooks from processing the message
  175.             ' and set msg to 0 (Windows will ignore the message if it is 0).  Refer to the
  176.             ' SpyWorks-VB manual's section on the nodef parameter in SBCHOOK.VBX on why
  177.             ' we cannot just set nodef = True.
  178.             nodef = True
  179.             msg = 0
  180.             
  181.             ' One side effect in Windows NT with the Ctl+Esc key is that focus is lost
  182.             ' from the current application even when you prevent the hot key message
  183.             ' from being sent. Therefore, we need to keep track of the foreground window
  184.             ' and set the focus back to that window.
  185.             
  186.             ' Normally, you do not want to change the focus in the middle of a message, but
  187.             ' in this case the focus was never actually set. A safer method is to use the
  188.             ' PostEvent.
  189.             l = SetForegroundWindow(foregroundwin)
  190.         End If
  191.     End If
  192. End Sub
  193. Private Sub WinHook2_WndMessage(wnd As Long, msg As Long, wp As Long, lp As Long, nodef As Integer)
  194. Dim loword As Integer, hiword As Integer
  195. Dim hfwnd As Long
  196.     ' keeps track of the active foreground window so we can set it back
  197.     ' if ctl+esc is hit while running in Windows NT
  198.     dwDWORDto2Integers wp, loword, hiword
  199.     hfwnd = GetForegroundWindow()
  200.     If hfwnd <> 0 Then foregroundwin = hfwnd
  201. End Sub
  202.