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 / ch06 / simkeys.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  7.9 KB  |  265 lines

  1. VERSION 5.00
  2. Begin VB.Form frmSimKeys 
  3.    Caption         =   "Send Keys and Mouse Events"
  4.    ClientHeight    =   4725
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1515
  7.    ClientWidth     =   6870
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   4725
  11.    ScaleWidth      =   6870
  12.    Begin VB.CommandButton cmdClickMe 
  13.       Caption         =   "Click Me"
  14.       Height          =   435
  15.       Left            =   5520
  16.       TabIndex        =   9
  17.       Top             =   4140
  18.       Width           =   1275
  19.    End
  20.    Begin VB.Timer Timer1 
  21.       Left            =   5880
  22.       Top             =   3480
  23.    End
  24.    Begin VB.TextBox txtDelay 
  25.       Height          =   315
  26.       Left            =   5520
  27.       TabIndex        =   7
  28.       Text            =   "2"
  29.       Top             =   2880
  30.       Width           =   1215
  31.    End
  32.    Begin VB.TextBox txtTarget 
  33.       Height          =   315
  34.       Left            =   120
  35.       TabIndex        =   6
  36.       Top             =   420
  37.       Width           =   5235
  38.    End
  39.    Begin VB.CommandButton cmdCaptureActive 
  40.       Caption         =   "Capture Active"
  41.       Height          =   435
  42.       Left            =   5460
  43.       TabIndex        =   5
  44.       Top             =   1860
  45.       Width           =   1335
  46.    End
  47.    Begin VB.CommandButton cmdCaptureAll 
  48.       Caption         =   "Capture Screen"
  49.       Height          =   435
  50.       Left            =   5460
  51.       TabIndex        =   4
  52.       Top             =   1380
  53.       Width           =   1335
  54.    End
  55.    Begin VB.CommandButton cmdMouseMove 
  56.       Caption         =   "MyMouseMove"
  57.       Height          =   435
  58.       Left            =   5460
  59.       TabIndex        =   3
  60.       Top             =   900
  61.       Width           =   1335
  62.    End
  63.    Begin VB.CommandButton cmdSendKeys 
  64.       Caption         =   "MySendKeys"
  65.       Height          =   435
  66.       Left            =   5460
  67.       TabIndex        =   2
  68.       Top             =   420
  69.       Width           =   1335
  70.    End
  71.    Begin VB.TextBox txtSource 
  72.       Height          =   315
  73.       Left            =   120
  74.       TabIndex        =   1
  75.       Text            =   "Text to be entered by sendkeys"
  76.       Top             =   60
  77.       Width           =   6675
  78.    End
  79.    Begin VB.PictureBox Picture1 
  80.       Height          =   3735
  81.       Left            =   120
  82.       ScaleHeight     =   3705
  83.       ScaleWidth      =   5205
  84.       TabIndex        =   0
  85.       Top             =   840
  86.       Width           =   5235
  87.    End
  88.    Begin VB.Label Label1 
  89.       Caption         =   "Delay (seconds)"
  90.       Height          =   255
  91.       Left            =   5520
  92.       TabIndex        =   8
  93.       Top             =   2580
  94.       Width           =   1155
  95.    End
  96. Attribute VB_Name = "frmSimKeys"
  97. Attribute VB_GlobalNameSpace = False
  98. Attribute VB_Creatable = False
  99. Attribute VB_PredeclaredId = True
  100. Attribute VB_Exposed = False
  101. Option Explicit
  102. ' Copyright 
  103.  1997 by Desaware Inc. All Rights Reserved
  104. ' 0 if no operation is in progress
  105. ' 1 if sending keys
  106. ' 2 if sending mouse commands
  107. ' 3 if capturing the whole screen
  108. ' 4 if capturing the active window
  109. Dim OperationInProgress%
  110. Dim IsWindows95%
  111. Private Sub cmdCaptureActive_Click()
  112.     Dim delay&
  113.     If OperationInProgress Then
  114.         MsgBox "Wait for prior operation to finish"
  115.         Exit Sub
  116.     End If
  117.     delay& = Val(txtDelay)
  118.     If delay& = 0 Then
  119.         MyCapture 1
  120.     Else
  121.         OperationInProgress = 4
  122.         Timer1.Interval = delay& * 1000
  123.         Timer1.Enabled = True
  124.     End If
  125. End Sub
  126. Private Sub cmdCaptureAll_Click()
  127.     Dim delay&
  128.     If OperationInProgress Then
  129.         MsgBox "Wait for prior operation to finish"
  130.         Exit Sub
  131.     End If
  132.     delay& = Val(txtDelay)
  133.     If delay& = 0 Then
  134.         MyCapture 0
  135.     Else
  136.         OperationInProgress = 3
  137.         Timer1.Interval = delay& * 1000
  138.         Timer1.Enabled = True
  139.     End If
  140. End Sub
  141. Private Sub cmdClickMe_Click()
  142.     MsgBox "Button has been clicked"
  143. End Sub
  144. Private Sub cmdMouseMove_Click()
  145.     Dim delay&
  146.     If OperationInProgress Then
  147.         MsgBox "Wait for prior operation to finish"
  148.         Exit Sub
  149.     End If
  150.     delay& = Val(txtDelay)
  151.     If delay& = 0 Then
  152.         MyMouseMove
  153.     Else
  154.         OperationInProgress = 2
  155.         Timer1.Interval = delay& * 1000
  156.         Timer1.Enabled = True
  157.     End If
  158. End Sub
  159. Private Sub cmdSendKeys_Click()
  160.     Dim delay&
  161.     If OperationInProgress Then
  162.         MsgBox "Wait for prior operation to finish"
  163.         Exit Sub
  164.     End If
  165.     delay& = Val(txtDelay)
  166.     If delay& = 0 Then
  167.         MySendKeys txtSource.Text
  168.     Else
  169.         OperationInProgress = 1
  170.         Timer1.Interval = delay& * 1000
  171.         Timer1.Enabled = True
  172.     End If
  173. End Sub
  174. Private Sub Form_Load()
  175.     Dim dl&
  176.     Dim osinfo As OSVERSIONINFO
  177.     osinfo.dwOSVersionInfoSize = Len(osinfo)
  178.     dl& = GetVersionEx(osinfo)
  179.     If osinfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then IsWindows95 = True
  180. End Sub
  181. Private Sub timer1_Timer()
  182.     Timer1.Enabled = False
  183.     Select Case OperationInProgress
  184.         Case 1
  185.                 MySendKeys txtSource.Text
  186.         Case 2
  187.                 MyMouseMove
  188.         Case 3
  189.                 MyCapture 0
  190.         Case 4
  191.                 MyCapture 1
  192.     End Select
  193.     OperationInProgress = 0
  194. End Sub
  195. ' Setting mode to 1 causes capture of the active window only
  196. Public Sub MyCapture(ByVal mode%)
  197.     Dim altscan%
  198.     Dim dl&
  199.     Dim snapparam%
  200.     altscan% = MapVirtualKey(VK_MENU, 0)
  201.     cmdCaptureAll.Enabled = False
  202.     cmdCaptureActive.Enabled = False
  203.     Screen.MousePointer = vbHourglass
  204.     If mode Then
  205.         keybd_event VK_MENU, altscan, 0, 0
  206.         ' It seems necessary to let this key get processed before
  207.         ' taking the snapshot.
  208.     End If
  209.     ' Why does this work?  Who knows!
  210.     If mode = 0 And IsWindows95 Then snapparam = 1
  211.     DoEvents    ' These seem necessary to make it reliable
  212.     ' Take the snapshot
  213.     keybd_event VK_SNAPSHOT, snapparam, 0, 0
  214.     DoEvents
  215.     Picture1.Picture = Clipboard.GetData(vbCFBitmap)
  216.     If mode Then keybd_event VK_MENU, altscan, KEYEVENTF_KEYUP, 0
  217.     cmdCaptureAll.Enabled = True
  218.     cmdCaptureActive.Enabled = True
  219.     Screen.MousePointer = vbDefault
  220. End Sub
  221. ' Try to move the mouse to click the "click me" button
  222. Public Sub MyMouseMove()
  223.     Dim pt As POINTAPI
  224.     Dim dl&
  225.     Dim destx&, desty&, curx&, cury&
  226.     Dim distx&, disty&
  227.     Dim screenx&, screeny&
  228.     Dim finished%
  229.     Dim ptsperx&, ptspery&
  230.     ' Get screen coordinates first
  231.     ' 10 by 10 pixels into the button
  232.     pt.x = 10
  233.     pt.y = 10
  234.     dl& = ClientToScreen(cmdClickMe.hwnd, pt)
  235.     screenx& = GetSystemMetrics(SM_CXSCREEN)
  236.     screeny& = GetSystemMetrics(SM_CYSCREEN)
  237.     destx& = pt.x * &HFFFF& / screenx&
  238.     desty& = pt.y * &HFFFF& / screeny&
  239.     ' About how many mouse points per pixel
  240.     ptsperx& = &HFFFF& / screenx&
  241.     ptspery& = &HFFFF& / screeny&
  242.     ' Now move it
  243.     Do
  244.         dl& = GetCursorPos(pt)
  245.         curx& = pt.x * &HFFFF& / screenx&
  246.         cury& = pt.y * &HFFFF& / screeny&
  247.         distx& = destx& - curx&
  248.         disty& = desty& - cury&
  249.         If (Abs(distx&) < 2 * ptsperx& And Abs(disty&) < 2 * ptspery) Then
  250.             ' Close enough, go the rest of the way
  251.             curx& = destx&
  252.             cury& = desty&
  253.             finished% = True
  254.         Else
  255.             ' Move closer
  256.             curx& = curx& + Sgn(distx&) * ptsperx * 2
  257.             cury& = cury& + Sgn(disty&) * ptspery * 2
  258.         End If
  259.         mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, curx, cury, 0, 0
  260.     Loop While Not finished
  261.     ' We got there, click the button
  262.     mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTDOWN, curx, cury, 0, 0
  263.     mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTUP, curx, cury, 0, 0
  264. End Sub
  265.