question

MaxPesenti-9771 avatar image
0 Votes"
MaxPesenti-9771 asked $$ANON_USER$$ commented

Find a window with partial text

Hy,
I've problems to organize different windows with running same program inside.

1) find a running window with a partial text name and get its position and size
2) find another window with partial text name (I use the same procedure written before) and set its new position and size

Someone can help me?

Thanks a lot
Max

windows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Are you trying to make one of the forms go to a different form's position, if they contain the same text, such as "Hello World"?

0 Votes 0 ·
$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ answered $$ANON_USER$$ edited

Hello,

You could try this:

First inside of the main form's load event add the forms you want to find the partial text for. For example:

     Me.AddOwnedForm(Form2)

This procedure loops through all of the forms checking to see if the title contains the SearchText variable's content. If it does then it moves the form to the new position.


 Private Sub FindFormWithPartialText()
             Dim SearchText As String = "Hello World"
             Dim HasFoundForm As Boolean = False
             Dim FormX As Integer
             Dim FormY As Integer
             Dim FormHeight As Integer
             Dim FormWidth As Integer
        
             For i = 0 To Me.OwnedForms.Length - 1
                 If Me.OwnedForms(i).Text.Contains(SearchText) Then
                     If HasFoundForm = False Then
                         FormX = Me.OwnedForms(i).Left
                         FormY = Me.OwnedForms(i).Top
                         FormHeight = Me.OwnedForms(i).Height
                         FormWidth = Me.OwnedForms(i).Width
                         HasFoundForm = True
                     Else
                         Me.OwnedForms(i).Left = FormX
                         Me.OwnedForms(i).Top = FormY
                         Me.OwnedForms(i).Height = FormHeight
                         Me.OwnedForms(i).Width = FormWidth
        
                         Exit For
                     End If
                 End If
             Next
 End Sub
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ answered $$ANON_USER$$ commented

Hello,

I modified the previous code, now it will move the form to the application's location, and change the form's height and width to the application's height and width. This does work with partial window title's. Also the form that I am moving is call, Form2. When you want to move the form just call the 'MoveForm' sub.

 <System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="FindWindowW")> _
     Private Shared Function FindWindowW(<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal lpClassName As String, <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal lpWindowName As String) As IntPtr
     End Function
    
     <System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="GetWindowRect")> _
     Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
     End Function
    
     <System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)> _
     Private Structure RECT
         Public Left As Integer
         Public Top As Integer
         Public Right As Integer
         Public Bottom As Integer
     End Structure
    
     Private Sub MoveForm()
         Dim PartialWindowTitle As String = "Untitled - Notepad"
         Dim Processes As Process() = Process.GetProcesses
    
         For i = 0 To Processes.Length - 1
             If Processes(i).MainWindowTitle.Contains(PartialWindowTitle) Then
                 Dim hWnd As IntPtr = FindWindowW(Nothing, Processes(i).MainWindowTitle)
    
                 If hWnd <> IntPtr.Zero Then
                     Dim WindowRectangle As New RECT
                     GetWindowRect(hWnd, WindowRectangle)
    
                     Form2.Top = WindowRectangle.Top
                     Form2.Left = WindowRectangle.Left
                     Form2.Height = WindowRectangle.Bottom - WindowRectangle.Top
                     Form2.Width = WindowRectangle.Right - WindowRectangle.Left
                 End If
             End If
         Next
     End Sub
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank a lot!!! It's great!!!!

0 Votes 0 ·

You are welcome!

1 Vote 1 ·
MaxPesenti-9771 avatar image
0 Votes"
MaxPesenti-9771 answered

Thanks Darren,
but the form it's opened by another program written from an external programmer. I've to find the form by name (partial text) and take information about it, position and size, I use to set new position and size depending the my configuration options...

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.