Move MDI windows

Ask general questions here.
this_is_ridiculous
Posts: 27
Joined: Tue Apr 27, 2010 10:33 pm

Move MDI windows

Post by this_is_ridiculous » Thu Jan 06, 2011 2:20 pm

Hi there!

I've got an issue with moving MDI forms. Ranorex Spy detects them as forms but Move method doesn't work for those windows. Even more, I've also got a custom class Window derived from Form (Element Adapter). In this class i've implemented my own move and size logic using Windows Api (my methods do not hide base class' methods for moving and sizing). So the resize works OK, but not the Move logics nether with the base method, nor with my custom.

So i'de like to ask if there's any way for solving this in a some kind of native way instead of emulating user input to the MDI childs?

Thanks in advance.
Artem.

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Move MDI windows

Post by Ciege » Thu Jan 06, 2011 4:07 pm

If, as you say, the Move method does not work, then an easy workaround is to move the window via a mouse drag.

Use code to get the current coordinates of the window.
Determine the coordinates you want to move to.
Mouse click down on the window title bar.
Move the mouse to your destination.
Mouse click up to release the window.

All of the above is quite easily do-able in code.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

this_is_ridiculous
Posts: 27
Joined: Tue Apr 27, 2010 10:33 pm

Re: Move MDI windows

Post by this_is_ridiculous » Thu Jan 06, 2011 5:36 pm

Ciege wrote:If, as you say, the Move method does not work, then an easy workaround is to move the window via a mouse drag.

All of the above is quite easily do-able in code.
Thanks for reply

I i've done exactly that way, it does work OK. But I thought I could do it using the class method spefically designed for such things (MDI child windows) in a single call. In my case it's no good to add user input emulation method to custom class for window due to I have different windows (the title location differs on some of them: left or right side of window and they flip their title according to screen location and i need a single solution for all types of windows in product. That's not my will, it's because I try to adopt Ranorex for the whole team to use. They are currently using SilkTest and from some time it doesn't suit our needs). So it will be only in a few UI scenarios i develop for them But not in classes.

But still thanks anyway :)

Artem

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Move MDI windows

Post by Ciege » Thu Jan 06, 2011 5:42 pm

But can't you just create a generic method that does this? I.e. create a framework method called MyMove (or whatever) that takes in parameters such as window name and where you want to move it. You could overload that with either coordinates of where to move to or relative screen location (like lower left, etc ...). Then your users only make a call to MyMove(WindowTitle, location) and be done with it... Write it once, use it often.

I've created a very large framework of generic methods such as this to use in our projects. Just created a dll from the framework code and made a reference to that framework dll in each of my different automation projects...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Move MDI windows

Post by Support Team » Fri Jan 07, 2011 1:46 pm

I'm not sure why the Form.Move should not work with MDI windows. I just tested it on a sample MDI application and the MDI window was correctly moved according to the coordinates (relative within the MDI container) specified for the Move method.

Could you provide us a sample application containing the MDI windows that Move does not seem to work on? If we could reproduce that problem, then we would (most probably) also be able to fix it :)

Regards,
Alex
Ranorex Team

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Move MDI windows

Post by sdaly » Tue Jan 18, 2011 10:56 am

You could use the code below to do it....

Use like this - ChangeWindowPosition("Some of the TitleBar text", 500, 500)

'###################################################################

Private Declare Function GetForegroundWindow Lib "user32" () As Integer
Private Declare Function GetWindow Lib "user32" (ByVal ByValhWnd As Integer, ByVal uCmd As Integer) As Integer

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
<DllImport("user32.dll")> _
Private Function SetWindowPos(ByVal ByValhWnd As IntPtr, ByVal ByValhWndInsertAfter As IntPtr, ByVal ByValX As Integer, ByVal ByValY As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
End Function

Public Sub ChangeWindowPosition(ByVal AppTitle As String, ByVal x As Integer, ByVal y As Integer)
'get starting handle from the window in the foreground
Dim hWnd As Integer = GetForegroundWindow
'loop to find handle
While hWnd <> 0
'create string builder with the length of the window text for current handle
Dim sb As New System.Text.StringBuilder("", GetWindowTextLength(hWnd) + 1)
GetWindowText(hWnd, sb, sb.Capacity)
'check if the current window text contains the app window text required
If sb.ToString().Contains(AppTitle) Then
'set the new window position
SetWindowPos(hWnd, IntPtr.Zero, x, y, 0, 0, 1)
Exit While
End If
hWnd = GetWindow(hWnd, 2)
End While

End Sub