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?
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
0
 
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?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you show us what you've got?...
0
 
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
0
 
CaitlinRobertsAuthor Commented:
Woops, copied from the wrong file, the SetWindowPos line should read:
                    SetWindowPos(IE(n).MainWindowHandle, Nothing, 0, -25, 500, 500, 0)
0
 
CaitlinRobertsAuthor Commented:
Do I have to pass anything in to the hWndInsertAfter or wFlag parameters?  
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0

Experts Exchange Solution brought to you by ConnectWise

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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.