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 / vbpg32 / samples5 / ch05 / statevw.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  6.5 KB  |  215 lines

  1. VERSION 5.00
  2. Begin VB.Form frmInput 
  3.    Caption         =   "Input State Viewer"
  4.    ClientHeight    =   3000
  5.    ClientLeft      =   2340
  6.    ClientTop       =   2055
  7.    ClientWidth     =   3885
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   3000
  11.    ScaleWidth      =   3885
  12.    Begin VB.CommandButton cmdAttach 
  13.       Caption         =   "Detach"
  14.       Height          =   315
  15.       Index           =   1
  16.       Left            =   120
  17.       TabIndex        =   13
  18.       Top             =   2580
  19.       Width           =   1275
  20.    End
  21.    Begin VB.CommandButton cmdAttach 
  22.       Caption         =   "Attach"
  23.       Height          =   315
  24.       Index           =   0
  25.       Left            =   120
  26.       TabIndex        =   12
  27.       Top             =   2280
  28.       Width           =   1275
  29.    End
  30.    Begin VB.Timer tmrDelayOp 
  31.       Enabled         =   0   'False
  32.       Left            =   3300
  33.       Top             =   1440
  34.    End
  35.    Begin VB.TextBox txtDelay 
  36.       Height          =   285
  37.       Left            =   2940
  38.       TabIndex        =   10
  39.       Text            =   "0"
  40.       Top             =   2280
  41.       Width           =   855
  42.    End
  43.    Begin VB.CommandButton cmdSet 
  44.       Caption         =   "Set Foreground"
  45.       Height          =   315
  46.       Index           =   2
  47.       Left            =   1440
  48.       TabIndex        =   9
  49.       Top             =   2580
  50.       Width           =   1335
  51.    End
  52.    Begin VB.CommandButton cmdSet 
  53.       Caption         =   "Set Active"
  54.       Height          =   315
  55.       Index           =   1
  56.       Left            =   1440
  57.       TabIndex        =   8
  58.       Top             =   2280
  59.       Width           =   1335
  60.    End
  61.    Begin VB.CommandButton cmdSet 
  62.       Caption         =   "Set Focus"
  63.       Height          =   315
  64.       Index           =   0
  65.       Left            =   1440
  66.       TabIndex        =   7
  67.       Top             =   1980
  68.       Width           =   1335
  69.    End
  70.    Begin VB.TextBox txtSet 
  71.       Height          =   285
  72.       Left            =   120
  73.       TabIndex        =   6
  74.       Top             =   1980
  75.       Width           =   1275
  76.    End
  77.    Begin VB.Timer Timer1 
  78.       Interval        =   500
  79.       Left            =   3300
  80.       Top             =   780
  81.    End
  82.    Begin VB.TextBox Text2 
  83.       Height          =   315
  84.       Left            =   2100
  85.       TabIndex        =   1
  86.       Text            =   "Text2"
  87.       Top             =   420
  88.       Width           =   1455
  89.    End
  90.    Begin VB.TextBox Text1 
  91.       Height          =   315
  92.       Left            =   180
  93.       TabIndex        =   0
  94.       Text            =   "Text1"
  95.       Top             =   420
  96.       Width           =   1515
  97.    End
  98.    Begin VB.Label Label1 
  99.       Caption         =   "Delay:"
  100.       Height          =   195
  101.       Left            =   2940
  102.       TabIndex        =   11
  103.       Top             =   1980
  104.       Width           =   855
  105.    End
  106.    Begin VB.Label lblForm 
  107.       Caption         =   "Label1"
  108.       Height          =   255
  109.       Left            =   180
  110.       TabIndex        =   5
  111.       Top             =   120
  112.       Width           =   3375
  113.    End
  114.    Begin VB.Label lblForeground 
  115.       Height          =   255
  116.       Left            =   180
  117.       TabIndex        =   4
  118.       Top             =   1560
  119.       Width           =   3495
  120.    End
  121.    Begin VB.Label lblActive 
  122.       Height          =   255
  123.       Left            =   180
  124.       TabIndex        =   3
  125.       Top             =   1200
  126.       Width           =   3495
  127.    End
  128.    Begin VB.Label lblFocus 
  129.       Height          =   255
  130.       Left            =   180
  131.       TabIndex        =   2
  132.       Top             =   840
  133.       Width           =   3495
  134.    End
  135. Attribute VB_Name = "frmInput"
  136. Attribute VB_GlobalNameSpace = False
  137. Attribute VB_Creatable = False
  138. Attribute VB_PredeclaredId = True
  139. Attribute VB_Exposed = False
  140. ' Copyright 
  141.  1997 Desaware Inc. All Rights Reserved
  142. Option Explicit
  143. Dim OpToDo%
  144. Dim WndToDo&
  145. Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
  146. Private Declare Function GetActiveWindow Lib "user32" () As Long
  147. Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
  148. Private Declare Function GetFocus Lib "user32" () As Long
  149. Private Declare Function GetForegroundWindow Lib "user32" () As Long
  150. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
  151. Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
  152. Private Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
  153. Private Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long
  154. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  155. Private Sub cmdAttach_Click(Index As Integer)
  156.     Dim wnd&
  157.     Dim dl&
  158.     Dim pid&
  159.     Dim destid&
  160.     Dim fAttach%
  161.     wnd = Val(txtSet.Text)
  162.     destid& = GetWindowThreadProcessId(wnd, pid)
  163.     ' Make sure values are valid
  164.     ' Note the need to compare with zero!
  165.     If destid& = 0 Or IsWindow(wnd) = 0 Then
  166.         MsgBox "Invalid hWnd"
  167.         Exit Sub
  168.     End If
  169.     If Index = 0 Then fAttach = True
  170.     dl& = AttachThreadInput(destid&, GetCurrentThreadId(), fAttach)
  171.     If dl& Then
  172.         MsgBox "Operation succeeded"
  173.     Else
  174.         MsgBox "Operation failed"
  175.     End If
  176. End Sub
  177. Private Sub cmdSet_Click(Index As Integer)
  178.     Dim UseDelay&
  179.     WndToDo = Val(txtSet.Text)
  180.     OpToDo = Index
  181.     UseDelay = Val(txtDelay.Text)
  182.     If UseDelay <> 0 Then
  183.         tmrDelayOp.Interval = UseDelay * 1000
  184.         tmrDelayOp.Enabled = True
  185.     Else
  186.         DoOperation
  187.     End If
  188.         
  189. End Sub
  190. Private Sub Form_Load()
  191.     Text1.Text = Str$(Text1.hwnd)
  192.     Text2.Text = Str$(Text2.hwnd)
  193.     lblForm.Caption = Str$(hwnd)
  194. End Sub
  195. Private Sub Timer1_Timer()
  196.     lblActive = "Active Window: " & Str$(GetActiveWindow())
  197.     lblFocus = "Focus Window: " & Str$(GetFocus())
  198.     lblForeground = "Foreground Window: " & Str$(GetForegroundWindow())
  199. End Sub
  200. Public Sub DoOperation()
  201.     Dim dl&
  202.     Select Case OpToDo
  203.         Case 0
  204.             dl& = SetFocusAPI(WndToDo)
  205.         Case 1
  206.             dl& = SetActiveWindow(WndToDo)
  207.         Case 2
  208.             dl& = SetForegroundWindow(WndToDo)
  209.     End Select
  210.     tmrDelayOp.Enabled = False    ' Make sure timer is off
  211. End Sub
  212. Private Sub tmrDelayOp_Timer()
  213.     DoOperation
  214. End Sub
  215.