Windows Fenster positionieren in einer Schleife
21.06.2017 07:40:19
Sebastian
ich versuche in einer Schleife verschiedene Windowsfenster zu verschieben und die Größe zu ändern. Das funktioniert auch, wenn ich es einzeln mache. Jetzt habe ich mir eine schleife gebaut und nun geht es nicht. Setzte ich nach der Variablendeklarierung eine MsgBox dann funktioniert es komischer weise. Hat jemand eine Idee was ich tun kann?
Vielen Dank und viele Grüße
Sebastian
Link zur datei
https://www.herber.de/bbs/user/114385.xlsm
Hier mein Makro
Option Explicit
Private Declare Function SetForegroundWindow _
Lib "user32" ( _
ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" (ByVal hwnd As Long) _
As Long
Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long)
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, ByVal wIndx As _
Long) As Long
Private Declare Function ShowWindow Lib "user32" ( _
ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public STitel As String
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Const GWL_STYLE& = (-16)
Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000
Const GW_HWNDNEXT& = 2
Const GW_CHILD& = 5
Const iNormal& = 1
Const iMinimized& = 2
Const iMaximized& = 3
Private Function GetWindowInfo(ByVal hwnd&, STitel$, Optional booVisible As Boolean = True) As _
_
Long
Dim Result&, Style&, Title$
'Darstellung des Fensters
Style = GetWindowLong(hwnd, GWL_STYLE)
Style = Style And (WS_VISIBLE Or WS_BORDER)
'Fensetrtitel ermitteln
Result = GetWindowTextLength(hwnd) + 1
Title = Space$(Result)
Result = GetWindowText(hwnd, Title, Result)
Title = Left$(Title, Len(Title) - 1)
'prüfen ob Fenster Sichtbar
If (Style = (WS_VISIBLE Or WS_BORDER)) Or booVisible = False Then
If Title Like "*" & STitel & "*" Then
GetWindowInfo = hwnd
Exit Function
End If
End If
GetWindowInfo = 0
End Function
'Fenstergrößen und Position setzen
Sub FensteraufrufAlle()
Dim STitel As String
Dim hwnd As Long
Dim Zeilen As Integer
Dim links As Integer, oben As Integer
Dim breite As Integer, hoehe As Integer
Dim zeile As Integer
zeile = 7
Do
'Positionsdaten
links = Sheets("Tabelle1").Range("D" & zeile)
oben = Sheets("Tabelle1").Range("E" & zeile)
breite = Sheets("Tabelle1").Range("F" & zeile)
hoehe = Sheets("Tabelle1").Range("G" & zeile)
STitel = Sheets("Tabelle1").Range("C" & zeile)
'MsgBox STitel
hwnd = GetDesktopWindow()
hwnd = GetWindow(hwnd, GW_CHILD)
GetWindowInfo hwnd, STitel, False
Do
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
If hwnd = 0 Then Exit Do
If GetWindowInfo(hwnd, STitel, True) = hwnd Then
'ShowWindow hwnd, iMinimized
ShowWindow hwnd, iNormal 'Fenstermodus
SetForegroundWindow hwnd 'aktivieren
SetWindowPos GetForegroundWindow, 0, links, oben, breite, hoehe, &H40
GoTo weitersprung
End If
Loop
weitersprung:
zeile = zeile + 1
Loop While Sheets("Tabelle1").Cells(zeile, 3) ""
End Sub