Ho una finestra di cui conosco solo il titolo (ad esempio Blocco note) che devo attivare, ridimensionare e posizionare nell'angolo in alto a sinistra dello schermo. Quindi dopo alcune ricerche su MSDN e forum ho trovato alcune funzioni che dovrebbero raggiungere questo obiettivo. Io uso FindWindow per ottenere l'handle per titolo, quindi utilizzo GetWindowPlacement per verificare se il blocco note è ridotto a icona o meno (in caso contrario, quindi utilizzo AppActivate, è necessario attivarlo solo se non è ridotto a icona). Tuttavia, se la finestra viene ridotta a icona, cerco di utilizzare SetWindowPlacement per attivarlo, ridimensionarlo e spostarlo in un unico comando. Ecco il mio codice:Come attivare, spostare e ridimensionare una finestra in VB.NET
<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
con l'esecuzione effettiva di essere qui:
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")
così cerco di eseguire questo, ma solo attiva la finestra, mentre il rettangolo si suppone per ridimensionare anche. Quindi cosa sto sbagliando? C'è un modo più semplice per ottenere tutto questo? Ci scusiamo per il lungo post
Con il tuo codice, gli effetti del rettangolo avranno luogo dopo il ripristino della finestra da "ingrandito" a "normale". Cambia 'wp2.showCmd = ShowWindowCommands.ShowMaximized' a' wp2.showCmd = ShowWindowCommands.Normal'. Non vuoi che la finestra sia ingrandita. –
Si trascura il controllo degli errori –