Microsoft Developer Network > Domovská stránka fór > Windows Presentation Foundation (WPF) > How to write into a WPF textbox in another WPF app form?
Odeslat dotazOdeslat dotaz
 

OdpovědětHow to write into a WPF textbox in another WPF app form?

  • 7. listopadu 2009 23:09awlinq Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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? 

Odpovědi

  • 13. listopadu 2009 6:50LawBot Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    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
    
    ...

      ' 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
    
    There is a special class to expose TextBox types to UIAutomation:

    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:

      Private Sub blob()
    
        Dim _handle As IntPtr = New WindowInteropHelper(Application.Current.MainWindow).Handle
    
        Dim windowElt As AutomationElement = AutomationElement.FromHandle(_handle)
    
        ' ...
    
      End Sub
    
    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.

    Best regards,
    M.

  • 16. listopadu 2009 6:09awlinq Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    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!

    • Označen jako odpověďawlinq 16. listopadu 2009 6:10
    •  

Všechny reakce

  • 13. listopadu 2009 6:50LawBot Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    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
    
    ...

      ' 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
    
    There is a special class to expose TextBox types to UIAutomation:

    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:

      Private Sub blob()
    
        Dim _handle As IntPtr = New WindowInteropHelper(Application.Current.MainWindow).Handle
    
        Dim windowElt As AutomationElement = AutomationElement.FromHandle(_handle)
    
        ' ...
    
      End Sub
    
    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.

    Best regards,
    M.

  • 16. listopadu 2009 6:09awlinq Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    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!

    • Označen jako odpověďawlinq 16. listopadu 2009 6:10
    •