Resize window using window handle

Does anyone know how to resize a window if I know that window handle?  (example: get the window handle of the Calculator program and change the size of the window)

THanks!
CaitlinRobertsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PockyMasterCommented:
I believe this will work (not on calculator, because it's not sizable )
But anyway, it's SetWindowPos you are looking for

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wnd As IntPtr = CWin32.FindWindow(Nothing, "SomeAppYouLookFor")
        CWin32.SetWindowPos(wnd, Nothing, 0, 0, 500, 500, 0)
    End Sub
End Class

Public Class CWin32
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Public Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer

End Class
CaitlinRobertsAuthor Commented:
For some reason this  code is seems to hide my window.  I've seen SetWindowPos before, but always have trouble getting it to work the way I need too.  Any suggestions?
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Can you show us what you've got?...
CaitlinRobertsAuthor Commented:
Sure thing:

    Private Declare Auto Sub SetWindowPos Lib "user32" ( _
        ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, _
        ByVal X As Long, ByVal Y As Long, _
        ByVal Cx As Long, ByVal cy As Long, ByVal wFlags As Long)

 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim IE() As Process
        Dim n As Integer
        IE = Process.GetProcessesByName("IEXPLORE")
        If IE.Length > 0 Then
            For n = 0 To IE.Length - 1
                If IE(n).MainWindowTitle.StartsWith("Window Title") Then
                    SetWindowPos(IE(n).MainWindowHandle.ToInt32, Nothing, 0, -25, 500, 500, 0)
                End If
            Next
        End If

    End Sub
CaitlinRobertsAuthor Commented:
Woops, copied from the wrong file, the SetWindowPos line should read:
                    SetWindowPos(IE(n).MainWindowHandle, Nothing, 0, -25, 500, 500, 0)
CaitlinRobertsAuthor Commented:
Do I have to pass anything in to the hWndInsertAfter or wFlag parameters?  
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You're using an old VB6 API declaration with Long data types in it  (and IntPtr stuffed into it)....they should be Integer as in both my linked example and PockyMasters posted example!

    Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
        (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As Integer, _
        ByVal x As Integer, ByVal y As Integer, _
        ByVal cx As Integer, ByVal cy As Integer, _
        ByVal wFlags As Integer) As Integer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CaitlinRobertsAuthor Commented:
Ahh, silly me!  That was a dumb mistake on my part.  Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.