I have a window in which I only know the title (for example, Notepad), which I need to activate, resize and place in the upper left corner of the screen. So after some research on MSDN and forums, I found some features that should have accomplished this. I use FindWindow to get the descriptor for the title, then I use GetWindowPlacement to find out if the notepad is minimized or not (if not, then I just use AppActivate, I just need to activate it if it is not minimized). However, if the window is minimized, I then try to use SetWindowPlacement to activate, resize and move it in one command. Here is my code:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Private Shared Function FindWindow( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As IntPtr End Function <DllImport("user32.dll")> _ Private Shared Function GetWindowPlacement(ByVal hWnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Boolean End Function <DllImport("user32.dll")> _ Private Shared Function SetWindowPlacement(ByVal hWnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Boolean End Function Private Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer Public Sub New(ByVal X As Integer, ByVal Y As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) Me.Left = X Me.Top = Y Me.Right = X2 Me.Bottom = Y2 End Sub End Structure Private Structure WINDOWPLACEMENT Public Length As Integer Public flags As Integer Public showCmd As ShowWindowCommands Public ptMinPosition As POINTAPI Public ptMaxPosition As POINTAPI Public rcNormalPosition As RECT End Structure Enum ShowWindowCommands As Integer Hide = 0 Normal = 1 ShowMinimized = 2 Maximize = 3 ShowMaximized = 3 ShowNoActivate = 4 Show = 5 Minimize = 6 ShowMinNoActive = 7 ShowNA = 8 Restore = 9 ShowDefault = 10 ForceMinimize = 11 End Enum Public Structure POINTAPI Public X As Integer Public Y As Integer Public Sub New(ByVal X As Integer, ByVal Y As Integer) Me.X = X Me.Y = Y End Sub End Structure
With actual execution here:
Dim wp As WINDOWPLACEMENT wp.Length = Marshal.SizeOf(wp) GetWindowPlacement(FindWindow(Nothing, "Notepad"), wp) If wp.showCmd = ShowWindowCommands.ShowMinimized Then Dim wp2 As WINDOWPLACEMENT wp2.showCmd = ShowWindowCommands.ShowMaximized wp2.ptMinPosition = wp.ptMinPosition wp2.ptMaxPosition = New POINTAPI(0, 0) wp2.rcNormalPosition = New RECT(0, 0, 816, 639) 'this is the size I want wp2.flags = wp.flags wp2.Length = Marshal.SizeOf(wp2) SetWindowPlacement(FindWindow(Nothing, "Notepad"), wp2) Else AppActivate("Notepad")
So, I'm trying to run this, but it just activates the window, while the rectangle should also resize it. So what am I doing wrong? Is there an easier way to achieve all this? Sorry for the long post.