Mark

Mark

  • NA
  • 11
  • 0

Find references to specific controls in a windows app

Aug 12 2009 11:26 AM

Hi - I want to write a small app which will pick up the text in a specific textbox, in a windows application (it's a unique helpdesk ticket number).
I've got some VB code (below) which I'll convert to C#.  However, it just gives me the window name and nothing else.
Is it possible for user32.dll (or some other library) to be utilised, which will allow me to see the controls on the screen - so I can read the text associated with specific controls?
Thanks for any suggestions,
Mark
 
 
Imports
System
Imports
System.Collections.Generic
Imports
System.ComponentModel
Imports
System.Data
Imports
System.Drawing
Imports
System.Text
Imports
System.Windows.Forms
Imports
System.Runtime.InteropServices
Imports
System.Security
Imports
System.Security.Principal.WindowsIdentity
Public
Class Form1
<DllImport(
"user32.dll")> _
Private Shared Function GetForegroundWindow() As Integer
End Function
<DllImport(
"user32.dll")> _
Private Shared Function GetWindowText(ByVal hWnd As Integer, ByVal text As StringBuilder, ByVal count As Integer) As Integer
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
GetActiveWindow()
End Sub
Dim curtime As DateTime
Private Sub GetActiveWindow()
Const nChars As Integer = 256
Dim handle As Integer = 0
Dim Buff As New StringBuilder(nChars)
handle = GetForegroundWindow()
Dim winname As String = ""
If GetWindowText(handle, Buff, nChars) > 0 Then
Me.richtextbox1.Text = Buff.ToString()
Me.RichTextBox2.Text = handle.ToString()
End If
End Sub
Public Sub New()
InitializeComponent()
curtime = DateTime.Now
End Sub
End
Class

Answers (6)