How to move an UI automation element to a different location?
I have a C#.NET app where I am able to find a AutomationElement at a particular point using FromPoint function.
Now I want to manipulate that AutomationElement so that I can move it to a different location, resize it etc.
For moving and resizing , I thought that I can use SetWindowPos Window API function but that requires the handle(an IntPtr) of the target window. Using the AutomationElement's "Current" property, i can read the NativeWindowHandle(an int) of it. Since Control is also a window, I passed this int value as window handle to the SetWindowPost function hoping that it would do my job.
But the code is not working.
Kindly suggest me a way to do this.
What's the type of element you want to move - a top-level window, or an item within a window? Also, is this a type of item that a user could move around using mouse or keyboard? UIAutomation is only supposed to allow you to manipulate the UI consistent with what a regular user could do - you can't use it to move the controls around on a dialog, for example.
The official way to move things in UIAutomation is to see if the element supports the Transform Pattern/a, and then use the Move or Resize methods to move it appropriately. I think this is mostly implemented only on top-level windows, not sure about how widely it is implemented on other controls - you can use UISpy/Inspect to check your target UI.
If the element is a HWND, you could also get the NativeWindowHandle property - which is the underlying HWND, but you'll need to convert it to IntPtr first - and then use it in Win32 APIs. This only works for actual HWND items, though, not for say items in a listview.