Hi all,
I have a small program I have designed to be able to sit in the background on my desktop, and whenever a new form is given focus (foreground) it's title and thread are recorded.
However, I would like the program to also report to me the X and Y coordinates of the foreground form. Could be any form NOT one that is attached to my program.
Here's what I have thus far:
Currently only set to give the coordinates of the form I have created, I need it to show the coordinates of any form on the foreground.
Hope this makes sense. Any ideas!
I have a small program I have designed to be able to sit in the background on my desktop, and whenever a new form is given focus (foreground) it's title and thread are recorded.
However, I would like the program to also report to me the X and Y coordinates of the foreground form. Could be any form NOT one that is attached to my program.
Here's what I have thus far:
VB.NET:
Imports System.Text
Imports System.Runtime.InteropServices
Public Class Form1
'gets x and y coordinates
Dim xloc, yloc As Integer
Private Declare Auto Function GetForegroundWindow Lib "user32" () As IntPtr
Private Const WM_GETTEST As Integer = &HD
<DllImport("User32")> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, <Out()> ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
End Function
<DllImport("User32", SetlastError:=True)> _
Private Shared Sub SendMessage(ByVal hwnd As IntPtr, ByVal messageId As Integer, ByVal wparam As Integer, <Out()> ByVal lpString As System.Text.StringBuilder)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'used for making the form appear at the lower left hand corner
'Me.Top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - Me.Height
'aligns form at the top right hand side
Me.Left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - Me.Width
End Sub
Private Sub txtGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGo.Click
If txtGo.Text = "Stop" Then
brain.Enabled = False
txtGo.Text = "Go"
Exit Sub
Else
brain.Enabled = True
txtGo.Text = "Stop"
End If
End Sub
Private Sub brain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brain.Tick
'get x, y coordinates
xloc = Me.Location.X
txtX.Text = xloc
yloc = Me.Location.Y
txtY.Text = yloc
'get thread pointer
Dim formPointer As IntPtr = GetForegroundWindow
TextBox1.Text = formPointer
Dim valueToRetrive As New StringBuilder(100)
Dim remoteHandle As IntPtr = New IntPtr(Convert.ToInt32(TextBox1.Text))
'GetWindowText(remoteHandle, valueToRetrive, 100)
SendMessage(remoteHandle, WM_GETTEST, 100, valueToRetrive)
TextBox2.Text = valueToRetrive.ToString()
'MessageBox.Show(valueToRetrive.ToString())
End Sub
End Class
Currently only set to give the coordinates of the form I have created, I need it to show the coordinates of any form on the foreground.
Hope this makes sense. Any ideas!