How to write into a WPF textbox in another WPF app form?
- With any non-WPF forms, we can use WindowFromPoint and FindWindow etc to get a HWND of any target and SendMessage to write or click. How to achieve the same with WPF form?
Answers
- Hi,
you could use UIAutomation:
http://msdn.microsoft.com/en-us/library/system.windows.automation.peers.aspx
Here is an example for UIAutomation I recently experimented with:
...Imports System.Windows.Automation Imports System.Windows.Automation.Peers Imports System.Windows.Automation.AutomationElement Imports System.Windows.Automation.TreeWalker Imports System.Windows.Automation.Provider
...' Open Windows Start using UIAutomation: Private Sub btWindowStart_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btWindowStart.Click Dim element As AutomationElement = _ AutomationElement.RootElement.FindFirst(TreeScope.Descendants, _ New AndCondition(New PropertyCondition(AutomationElement.ControlTypeProperty, _ ControlType.Button), _ New PropertyCondition(AutomationElement.NameProperty, "Start", _ PropertyConditionFlags.IgnoreCase))) Dim patty As InvokePattern = element.GetCurrentPattern(InvokePattern.Pattern) Try patty.Invoke() Catch ex As Exception MessageBox.Show(ex.Message.ToString) End Try End Sub
There is a special class to expose TextBox types to UIAutomation:' This Button will be invoked using UIAutomation whithin the same WPF Application Private Sub btAutomationTarget_Click(ByVal sender As Object, _ ByVal e As System.Windows.RoutedEventArgs) _ Handles btAutomationTarget.Click MessageBox.Show("I have been automated!") End Sub ' Here I invoke the above Button using UIAutomation Private Sub btAutomate_Click(ByVal sender As Object, _ ByVal e As System.Windows.RoutedEventArgs) _ Handles btAutomate.Click Dim peer As New ButtonAutomationPeer(btAutomationTarget) Dim invokeProv As IInvokeProvider = peer.GetPattern(PatternInterface.Invoke) invokeProv.Invoke() End Sub
http://msdn.microsoft.com/en-us/library/system.windows.automation.peers.textboxautomationpeer.aspx
Instead of using the RootElement property as the starting point for the TreeScope to locate the instance of the desired AutomationElement you could use the property FocusedElement, or the methods FromHandle or FromPoint.
Here is an example for the usage of the method FromHandle:
If you use UIAutomation you should take care not to block the UI thread. So you should code the automation calls in a seperate thread if you locate a AutomationElement from RootElement as starting point.Private Sub blob() Dim _handle As IntPtr = New WindowInteropHelper(Application.Current.MainWindow).Handle Dim windowElt As AutomationElement = AutomationElement.FromHandle(_handle) ' ... End Sub
Best regards,
M.- Marked As Answer byJim Zhou - MSFTModeratorFriday, November 13, 2009 10:00 AM
- hey, this is really cool and powerful! It can also write on web pages, wpf, and windows native. I tested the following code, and it writes on textbox anywhere.
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Load(
@"..\..\app_cross.bmp");
System.Windows.Point p = new System.Windows.Point();
p.X = MousePosition.X;
p.Y = MousePosition.Y;
AutomationElement element = AutomationElement.FromPoint(p);
ValuePattern textcontrol = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern);
textcontrol.SetValue(textBox1.Text);}
Thank you!- Marked As Answer byawlinq Monday, November 16, 2009 6:10 AM
All Replies
- Hi,
you could use UIAutomation:
http://msdn.microsoft.com/en-us/library/system.windows.automation.peers.aspx
Here is an example for UIAutomation I recently experimented with:
...Imports System.Windows.Automation Imports System.Windows.Automation.Peers Imports System.Windows.Automation.AutomationElement Imports System.Windows.Automation.TreeWalker Imports System.Windows.Automation.Provider
...' Open Windows Start using UIAutomation: Private Sub btWindowStart_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btWindowStart.Click Dim element As AutomationElement = _ AutomationElement.RootElement.FindFirst(TreeScope.Descendants, _ New AndCondition(New PropertyCondition(AutomationElement.ControlTypeProperty, _ ControlType.Button), _ New PropertyCondition(AutomationElement.NameProperty, "Start", _ PropertyConditionFlags.IgnoreCase))) Dim patty As InvokePattern = element.GetCurrentPattern(InvokePattern.Pattern) Try patty.Invoke() Catch ex As Exception MessageBox.Show(ex.Message.ToString) End Try End Sub
There is a special class to expose TextBox types to UIAutomation:' This Button will be invoked using UIAutomation whithin the same WPF Application Private Sub btAutomationTarget_Click(ByVal sender As Object, _ ByVal e As System.Windows.RoutedEventArgs) _ Handles btAutomationTarget.Click MessageBox.Show("I have been automated!") End Sub ' Here I invoke the above Button using UIAutomation Private Sub btAutomate_Click(ByVal sender As Object, _ ByVal e As System.Windows.RoutedEventArgs) _ Handles btAutomate.Click Dim peer As New ButtonAutomationPeer(btAutomationTarget) Dim invokeProv As IInvokeProvider = peer.GetPattern(PatternInterface.Invoke) invokeProv.Invoke() End Sub
http://msdn.microsoft.com/en-us/library/system.windows.automation.peers.textboxautomationpeer.aspx
Instead of using the RootElement property as the starting point for the TreeScope to locate the instance of the desired AutomationElement you could use the property FocusedElement, or the methods FromHandle or FromPoint.
Here is an example for the usage of the method FromHandle:
If you use UIAutomation you should take care not to block the UI thread. So you should code the automation calls in a seperate thread if you locate a AutomationElement from RootElement as starting point.Private Sub blob() Dim _handle As IntPtr = New WindowInteropHelper(Application.Current.MainWindow).Handle Dim windowElt As AutomationElement = AutomationElement.FromHandle(_handle) ' ... End Sub
Best regards,
M.- Marked As Answer byJim Zhou - MSFTModeratorFriday, November 13, 2009 10:00 AM
- hey, this is really cool and powerful! It can also write on web pages, wpf, and windows native. I tested the following code, and it writes on textbox anywhere.
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Load(
@"..\..\app_cross.bmp");
System.Windows.Point p = new System.Windows.Point();
p.X = MousePosition.X;
p.Y = MousePosition.Y;
AutomationElement element = AutomationElement.FromPoint(p);
ValuePattern textcontrol = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern);
textcontrol.SetValue(textBox1.Text);}
Thank you!- Marked As Answer byawlinq Monday, November 16, 2009 6:10 AM


