Q: How do yo change the size of one Winforms application from another in VB.NET?


        Dim p() As System.Diagnostics.Process

        p = System.Diagnostics.Process.GetProcessesByName("WindowsApplication2")


        If p.Length > 0 Then

            Dim res As Integer

            Dim rc As RECT

            GetWindowRect(p(0).MainWindowHandle().ToInt32, rc)


            res = MoveWindow(p(0).MainWindowHandle().ToInt32, rc.Left, rc.Top, 100, 100, True)


        End If



Where RECT, GetWindowRect, and MoveWindow are declared as:



    Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Integer


    Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer


    Public Structure RECT

        Dim Left As Integer

        Dim Top As Integer

        Dim Right As Integer

        Dim Bottom As Integer

    End Structure



You will see a slight delay between the call and the actual resize.  This is because the amount of time it takes to pass the message to the message pump for the other application but it works just fine unless the 2nd app is overriding the WndProc and handling WM_WINDOWPOSCHANGING message. 


You could also use SetWindowPos instead of MoveWindow if you wanted to get fancy and not have the window repaint or if you wanted to withhold the WM_WINDOWPOSCHANGING message, etc.  See SetWindowPos for more options.


Skip to main content