Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRestore a window to a known position if you can't find it in Visual Basic 2005
DescriptionThis example shows how to restore a window to a known position if you can't find it in Visual Basic 2005.
Keywordsrestore window, window size, window position, FindWindow, SetWindowPos, Visual Basic, Visual Basic 2005, VB .NET
CategoriesControls, Software Engineering
 
Recently Microsoft Excel decided to take a little vacation. I could maximize it and minimize it but when I tried to restore it, it moved somewhere off the screen so I couldn't find it. I needed to be able to display Excel at less than full screen so I wrote this program to find it and put it back on the desktop.

Enter the title of the target application and the position where you want it, and click Go. The Go button's Click event handler uses FindWindow to find the window based on its title. It then uses SetWindowPos to position the target application.

 
Private Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, ByVal _
    lpWindowName As String) As Integer
Private Declare Function SetWindowPos Lib "user32" Alias _
    "SetWindowPos" (ByVal hwnd As Integer, 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

' Find the target window and restore it.
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    ' Find the target.
    Dim app_hwnd As Long = FindWindow(vbNullString, _
        txtTitle.Text)
    If app_hwnd = 0 Then
        MessageBox.Show("Cannot find application " & _
            txtTitle.Text, _
            "Application Not Found", MessageBoxButtons.OK, _
                MessageBoxIcon.Error)
        Exit Sub
    End If

    ' Position the window.
    SetWindowPos(app_hwnd, 0, _
        Val(txtLeft.Text), _
        Val(txtTop.Text), _
        Val(txtWidth.Text), _
        Val(txtHeight.Text), _
        0)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated