Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1684to1688
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Fenstergröße mit VBA ermitteln

Fenstergröße mit VBA ermitteln
12.04.2019 06:51:31
Sebastian
Hallo Profis,
ich hole mir mit einem Code ein Windowsfenster in den Vordergrund. Jetzt Würde ich gerne die genau größe noch auslesen. Hat jemand eine Idee wie ich das noch machen kann?
Mein Code um z.B. VLC in den Vordergrund zu holen lautet.
Jetzt benötige ich nur noch Fenstergröße :-(
Vielen Dank für Eure Hilfe
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 Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
pnWidth As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, ByVal wIndx As _
Long) 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 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

Sub PostitinFinden()
Dim STitel As String
Dim hwnd As Long
Dim zeilen As Integer
Dim seite As String
Dim Rec As RECT
STitel = "VLC"
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
SetForegroundWindow hwnd 'aktivieren
GetWindowRect GetForegroundWindow, Rec
'FensterPostion ermitteln
MsgBox "Links:" & Rec.Left & vbCr & _
"Oben:" & Rec.Top & vbCr & _
"Rechts:" & Rec.Right & vbCr
Exit Sub
End If
Loop
End Sub

1
Beitrag zum Forumthread
Beitrag zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Fenstergröße mit VBA ermitteln
12.04.2019 08:22:04
Nepumuk
Hallo Sebastian,
einfach so:
Option Explicit

Private Declare PtrSafe Function FindWindowA Lib "user32.dll" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function GetWindowRect Lib "user32.dll" ( _
    ByVal hwnd As LongPtr, _
    ByRef lpRect As RECT) As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32.dll" ( _
    ByVal hwnd As LongPtr) As Long

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Const GC_CLASSNAMEVLC As String = "Qt5QWindowIcon"

Public Sub Test()
    Dim lngptrHwnd As LongPtr
    Dim udtRect As RECT
    lngptrHwnd = FindWindowA(GC_CLASSNAMEVLC, "VLC media player")
    If lngptrHwnd <> 0 Then
        Call GetWindowRect(lngptrHwnd, udtRect)
        With udtRect
            Call MsgBox("Links: " & CStr(.Left) & vbLf & _
                "Oben: " & CStr(.Top) & vbLf & _
                "Rechts: " & CStr(.Right) & vbLf & _
                "Unten: " & CStr(.Bottom))
        End With
        Call SetForegroundWindow(lngptrHwnd)
    End If
End Sub

Gruß
Nepumuk
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige