Re: Make application sleep

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On 12 Oct 2006 03:11:54 -0700, teslar91@xxxxxxxxxxx wrote:


Thanks to all of you guys for your help and efforts. I've given
SendMessage a go, but it still won't do (entirely).

My test.txt contains:
{1221,12}
{50,980}
{70,867}

The idea is to move to each position and perform a click. In my case
the first position should minimize any foremost underlying
application.

Second click hits the Win-XP Start-button.

The third click should click somewhat above the the now unfolded list
above the the Start-button and hit Notepad.

In all three cases the mouse moves to the proper position.

Though, the first click doesn't minimize the underlying application
(that could be any maximized app).

The second click performs well and unfolds the Start-button.

The thirc click doesn't open the Notepad (even at the right position).

Here is my full code:

'--code begin
Imports System.Windows.Forms
Imports System.Threading
Imports System.IO
Public Class Form1

Public TextInput As String = ""
Public PosX As Integer, PosY As Integer

Dim thSleep As New Thread(AddressOf ReadFile)

#Region "Get window-name, Declares"
Dim myVal1 As Integer
Dim myVal2 As Integer

Dim intHandle As IntPtr = FindWindow("[Class Name Here]",
vbNullString)
Dim intHandle2 As IntPtr = FindWindow(vbNullString, "Lommeregner")

Private Declare Function GetForegroundWindow _
Lib "user32" () As IntPtr
Private Declare Function SetForegroundWindow _
Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function GetWindowThreadProcessId _
Lib "user32" (ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As Integer
Private Declare Function AttachThreadInput _
Lib "user32" (ByVal idAttach As Integer, _
ByVal idAttachTo As Integer, ByVal fAttach As Boolean _
) As Boolean
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,
_
ByVal lpWindowName As String) As IntPtr
#End Region

Declare Auto Function WindowFromPoint Lib "user32" (ByVal xPoint
As Integer, ByVal yPoint As Integer) As IntPtr
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Me.WindowState = FormWindowState.Minimized
ReadFile()
End Sub
Sub ReadFile()

Dim sr As StreamReader = New StreamReader("d:\test.txt")
Dim s As String = ""

Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLine(s)

If s.Substring(0, 1) = "{" Then
s = s.Substring(1, s.Length - 2)
Dim myArray() As String = Split(s, ",", ,
CompareMethod.Text)
PosX = myArray(0)
PosY = myArray(1)

ClickPosition()
Thread.Sleep(500)
Else
'TextInput = s
'WriteText()
'Thread.Sleep(500)
End If
Loop

sr.Close()
sr.Dispose()

End Sub
Sub ClickPosition()
Dim wnd As IntPtr
Dim pt As Point

pt.X = PosX
pt.Y = PosY
Windows.Forms.Cursor.Position = pt

Const WM_ACTIVATEAPP = &H1C
Const WM_LBUTTONUP = &H202 '//LButton up
Const WM_LBUTTONDOWN = &H201 '//LButton down

wnd = WindowFromPoint(PosX, PosY)
Call SendMessage(wnd, WM_ACTIVATEAPP, 0, 0)
Call SendMessage(wnd, WM_LBUTTONDOWN, 0, 0)
Call SendMessage(wnd, WM_LBUTTONUP, 0, 0)
End Sub

Sub WriteText()
SendKeys.SendWait(TextInput)
End Sub

End Class
'--code end

Regards /Snedker
.


Quantcast