home *** CD-ROM | disk | FTP | other *** search
- VERSION 4.00
- Begin VB.Form frmStubborn
- Caption = "Handling Stubborn Apps"
- ClientHeight = 3825
- ClientLeft = 2865
- ClientTop = 5265
- ClientWidth = 7200
- Height = 4230
- Icon = "FRMSTUB.frx":0000
- Left = 2805
- LinkTopic = "Form1"
- MaxButton = 0 'False
- ScaleHeight = 255
- ScaleMode = 3 'Pixel
- ScaleWidth = 480
- Top = 4920
- Width = 7320
- Begin VB.Frame Frame1
- Caption = "Exchange Data"
- Height = 915
- Left = 120
- TabIndex = 0
- Top = 2340
- Width = 3495
- Begin VB.OptionButton opt
- Caption = "Send Text to a Window"
- Height = 255
- Index = 1
- Left = 120
- TabIndex = 2
- Top = 540
- Width = 3255
- End
- Begin VB.OptionButton opt
- Caption = "Get Text From a Window"
- Height = 255
- Index = 0
- Left = 120
- TabIndex = 1
- Top = 240
- Width = 3255
- End
- End
- Begin VB.CommandButton cmd
- Caption = "Communicate with Notepad using SendKeys and the API"
- Height = 420
- Index = 2
- Left = 90
- TabIndex = 6
- Top = 3330
- Width = 7035
- End
- Begin VB.CommandButton cmd
- Caption = "Send Text to a Window"
- Enabled = 0 'False
- Height = 420
- Index = 1
- Left = 90
- TabIndex = 5
- Top = 2835
- Width = 7035
- End
- Begin VB.CommandButton cmd
- Caption = "Get Text from a Window"
- Height = 420
- Index = 0
- Left = 90
- TabIndex = 4
- Top = 2340
- Width = 7035
- End
- Begin VB.TextBox Text1
- BeginProperty Font
- name = "Courier New"
- charset = 1
- weight = 400
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 2175
- Left = 90
- MultiLine = -1 'True
- ScrollBars = 3 'Both
- TabIndex = 3
- Top = 90
- Width = 7035
- End
- Attribute VB_Name = "frmStubborn"
- Attribute VB_Creatable = False
- Attribute VB_Exposed = False
- '*********************************************************************
- ' FRMSTUB.FRM: This program demonstrates how to communicate with apps
- ' that don't respond via DDE or OLE.
- '*********************************************************************
- Option Explicit
- '*********************************************************************
- ' Form-level 16 & 32-bit API declarations.
- '*********************************************************************
- #If Win32 Then
- Private Const PROCESS_QUERY_INFORMATION = &H400
- Private Const STILL_ACTIVE = &H103
- Private Declare Function OpenProcess& Lib "kernel32" (ByVal _
- dwDesiredAccess&, ByVal bInheritHandle&, ByVal dwProcessId&)
- Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal _
- hProcess As Long, lpExitCode As Long) As Long
- Private Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" _
- (ByVal hInstance&, ByVal lpCursor&) As Long
- Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, _
- ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
- Private Const IDC_UPARROW = 32516&
- #Else
- Private Declare Function GetModuleUsage% Lib "Kernel" (ByVal hModule%)
- #End If
- '*********************************************************************
- ' Form-level variables.
- '*********************************************************************
- Private SendIt As Boolean, PointMode As Boolean
- '*********************************************************************
- ' Process command button clicks.
- '*********************************************************************
- Private Sub cmd_Click(Index As Integer)
- Dim hNotepad&, Source%, FileName$, msg$, Handle&, ExitCode&
- Select Case Index
- Case 0 ' Get
- SendIt = False
- PointMode = True
- CaptureWindows "Start", Me, 0, 0, ""
- Case 1 ' Send
- SendIt = True
- PointMode = True
- CaptureWindows "Start", Me, 0, 0, ""
- Case 2 ' Use SendKeys
- '*********************************************************
- ' Build a temporary filename. Kill it if it already exists.
- '*********************************************************
- FileName = App.Path & "\~test~.txt"
- If FileExists(FileName) Then Kill FileName
- '*********************************************************
- ' Run Notepad maximized with the new file, and store its
- ' task handle into a variable for later use.
- '*********************************************************
- hNotepad = Shell("notepad.exe " & FileName, vbNormalFocus)
- '*********************************************************
- ' This statement hits enter to create the new file.
- '*********************************************************
- SendKeys "~", True
- '*********************************************************
- ' Build an instruction screen, and insert it into Notepad.
- '*********************************************************
- msg = "Enter your text in here." & vbCrLf
- msg = msg & "When you are done, quit "
- msg = msg & "Notepad and save your changes."
- SendKeys msg, True
- '*********************************************************
- ' Finally, highlight the instructions so the user can
- ' easily delete them.
- '*********************************************************
- SendKeys "^+{Home}", True
- '*********************************************************
- ' Wait while Notepad is still open.
- '*********************************************************
- #If Win32 Then
- Handle = OpenProcess(PROCESS_QUERY_INFORMATION, False, _
- hNotepad)
- Do
- GetExitCodeProcess Handle, ExitCode
- DoEvents
- Loop While ExitCode = STILL_ACTIVE
- #Else
- Do While GetModuleUsage(hNotepad)
- DoEvents
- Loop
- #End If
- '*********************************************************
- ' Once Notepad is unloaded, open the file and insert it
- ' into Text1.
- '*********************************************************
- Source = FreeFile
- Open FileName For Input As Source
- Text1 = Input(LOF(Source), Source)
- Close Source
- '*********************************************************
- ' Kill the temporary file.
- '*********************************************************
- Kill FileName
- End Select
- End Sub
- '*********************************************************************
- ' Form Inialization.
- '*********************************************************************
- Private Sub Form_Load()
- '*********************************************************************
- ' Exchanging data is totally different in Win32, so re-adjust the
- ' visual appearance of the form for it. Win16 uses the default
- ' configuration set at design time.
- '*********************************************************************
- #If Win32 Then
- Dim iX%, iY%, iDrawX%
- '*****************************************************************
- ' Change the ScaleMode to pixels and turn on AutoRedraw
- '*****************************************************************
- ScaleMode = vbPixels
- AutoRedraw = True
- '*****************************************************************
- ' Hide controls for use with Win16, and display Win32 controls.
- '*****************************************************************
- cmd(0).Visible = False
- cmd(1).Visible = False
- Frame1.Visible = True
- '*****************************************************************
- ' Build positioning variables and set CurrentX & CurrentY
- '*****************************************************************
- iX = Frame1.Left + Frame1.Width
- CurrentX = iX
- iDrawX = iX + ((ScaleWidth - iX) / 2) + 10
- iY = Text1.Top + Text1.Height
- iY = iY + ((ScaleHeight - (Frame1.Top + Frame1.Height)) / 2) + 5
- CurrentY = iY
- '*****************************************************************
- ' Draw MousePointer vbUpArrow into the form's persistant bitmap.
- '*****************************************************************
- DrawIcon hdc, iDrawX, iY, LoadCursor(0, IDC_UPARROW)
- '*****************************************************************
- ' Give the user some instructions about why the arrow is painted
- ' on the form.
- '*****************************************************************
- Print " Click & drag this arrow ->"
- CurrentX = iX
- Print " to exhange data."
- #End If
- '*****************************************************************
- ' Hide controls for use with Win32, and display Win16 controls.
- '*****************************************************************
- cmd(0).Visible = True
- cmd(1).Visible = True
- Frame1.Visible = False
- '*****************************************************************
- ' Centers the form to the screen
- '*****************************************************************
- Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 2
- End Sub
- '*********************************************************************
- ' Win32 version of SetCapture requires the left mouse button to be
- ' depressed in order to capture data from other processes and the
- ' MouseUp event automatically does a ReleaseCapture. This difference
- ' requires totally different code (and a different UI).
- '*********************************************************************
- #If Win32 Then
- Private Sub Form_MouseDown(Button As Integer, Shift%, x!, y!)
- '*************************************************************
- ' If opt(0) is checked, then send the text from Text1 using
- ' the code already in cmd(0)'s click event. Otherwise, get
- ' text from another window using cmd(1)'s code.
- '*************************************************************
- If opt(0) Then
- cmd_Click 0
- Else
- cmd_Click 1
- End If
- End Sub
- '*****************************************************************
- ' Stop capturing and exchange the text on the Form_MouseUp event.
- '*****************************************************************
- Private Sub Form_MouseUp(Button As Integer, Shift%, x!, y!)
- HandleMouse Button, Shift, x, y
- End Sub
- '*********************************************************************
- ' 16-bit version of MouseDown. MouseUp is ignored.
- '*********************************************************************
- #Else
- '*****************************************************************
- ' Stop capturing and exchange the text on the Form_MouseDown event
- '*****************************************************************
- Private Sub Form_MouseDown(Button As Integer, Shift%, x!, y!)
- HandleMouse Button, Shift, x, y
- End Sub
- #End If
- '*********************************************************************
- ' During the PointMode, this window receives a mouse move for the
- ' entire desktop. This function causes a new highlight to be drawn.
- '*********************************************************************
- Private Sub Form_MouseMove(Button As Integer, Shift%, x!, y!)
- If GetCapture() Then CaptureWindows "Move", Me, x, y, ""
- End Sub
- '*********************************************************************
- ' Only enable cmd(1) when it contains text.
- '*********************************************************************
- Private Sub Text1_Change()
- cmd(1).Enabled = IIf(Text1 = "", False, True)
- End Sub
- '*********************************************************************
- ' This function checks to see if a file exists.
- '*********************************************************************
- Private Function FileExists(FileName$) As Boolean
- On Error Resume Next
- FileExists = IIf(Dir(FileName) <> "", True, False)
- End Function
- '*********************************************************************
- ' Mouse code is the same for Win16 & Win32, but its called in
- ' different locations on the two platforms. Rather than repeating
- ' code, this code is entered once and called from the appropriate
- ' event.
- '*********************************************************************
- Private Sub HandleMouse(Button As Integer, Shift%, x!, y!)
- Dim errStr$, retStr$
- '*****************************************************************
- ' Build a string that matchings the return error value.
- '*****************************************************************
- errStr = "Error:" & String(10, "~")
- '*****************************************************************
- ' Get text from a window.
- '*****************************************************************
- If PointMode And SendIt = False Then
- retStr = CaptureWindows("End", Me, x, y, "")
- If retStr = errStr Then
- MsgBox "Sorry, but that control didn't respond!", 48
- Else
- Text1 = retStr
- End If
- PointMode = False
- '*****************************************************************
- ' Send text to a window.
- '*****************************************************************
- ElseIf PointMode And SendIt Then
- retStr = CaptureWindows("End", Me, x, y, Text1)
- If retStr = "0" Or retStr = errStr Then
- MsgBox "Sorry, but that control will not accept text!", 48
- End If
- PointMode = False
- End If
- End Sub
-