UI Automation-TransformPattern

本文介绍如何使用TransformPattern控件来移动、调整大小及旋转UI元素。通过具体实例展示了如何获取控件并调用其Move、Resize和Rotate方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

表示一个可以在二维空间中移动,调整大小或旋转的控件。TransformPattern控件的主要方法有Move(移动控件),resize(调整控件大小)和rotate(旋转控件)。

示例:

      public static void TestTransformPattern()

        {

            Process adressbook = Process.Start(@"C:/Program Files/Outlook Express/wab.exe");

            Thread.Sleep(2000);

 

            //Get main window   "Address Book - Main Identity"

 

            AutomationElement addresswindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Address Book - Main Identity"));

            Thread.Sleep(2000);

 

            TransformPattern windowpattern = Utility.UIA.ControlPattern.Transformpattern.GetTransformPattern(addresswindow);

            windowpattern.Move(10, 0);

            Thread.Sleep(1000);

            windowpattern.Resize(200, 200);

            Thread.Sleep(2000);

            if (windowpattern.Current.CanRotate.ToString() == "True")

                windowpattern.Rotate(20);

           

            Thread.Sleep(1000);

 

 

        }

 

public static TransformPattern GetTransformPattern(

            AutomationElement targetControl)

        {

            TransformPattern transformPattern = null;

 

            try

            {

                transformPattern =

                    targetControl.GetCurrentPattern(TransformPattern.Pattern)

                    as TransformPattern;

            }

            catch (InvalidOperationException)

            {

                // object doesn't support the TransformPattern control pattern

                return null;

            }

 

            return transformPattern;

        }

 

 

 

        ///--------------------------------------------------------------------

        /// <summary>

        /// Calls the TransformPattern.Move() method for an associated

        /// automation element.

        /// </summary>

        /// <param name="transformPattern">

        /// The TransformPattern control pattern obtained from

        /// an automation element.

        /// </param>

        /// <param name="x">

        /// The number of screen pixels to move the automation element

        /// horizontally.

        /// </param>

        /// <param name="y">

        /// The number of screen pixels to move the automation element

        /// vertically.

        /// </param>

        ///--------------------------------------------------------------------

        public static void MoveElement(

            TransformPattern transformPattern, double x, double y)

        {

            try

            {

                if (transformPattern.Current.CanMove)

                {

                    transformPattern.Move(x, y);

                }

            }

            catch (InvalidOperationException)

            {

                // object is not able to perform the requested action

                return;

            }

        }

        /// <param name="height">

        /// The requested height of the automation element.

        /// </param>

        ///--------------------------------------------------------------------

        public static void ResizeElement(

            TransformPattern transformPattern, double width, double height)

        {

            try

            {

                if (transformPattern.Current.CanResize)

                {

                    transformPattern.Resize(width, height);

                }

            }

            catch (InvalidOperationException)

            {

                // object is not able to perform the requested action

                return;

            }

        }

        public static void RotateElement(

        TransformPattern transformPattern, double degrees)

        {

            try

            {

                if (transformPattern.Current.CanRotate)

                {

                    transformPattern.Rotate(degrees);

                }

            }

            catch (InvalidOperationException)

            {

                // object is not able to perform the requested action

                return;

            }

        }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值