AppActivate wenn Handle bekannt
[Windows 95/98/NT/2000]

30.09.2001


  Mit dem Befehl AppActivate kann man Fenster "aktivieren",
     also als aktives Fenster setzen.
     Was aber, wenn nicht der Titel, 
     sondern nur ein Handle des Fensters bekannt ist ?
     Man könnte zwar den Umweg gehen, den Titel des Fensters zu ermitteln,
     um anschließend per AppActivate das Fenster zu aktivieren...

     Diese Routine zeigt Ihnen,
     wie man es auch ohne Fenstertitel erledigen kann:
ERFORDERLICHE OBJEKTE
   1 CommandButton (Command1)
  FORM-CODE   
   Private Declare Function BringWindowToTop Lib "user32" _
      (ByVal hwnd As Long) As Long
   Private Declare Function FindWindow Lib "user32" _
      Alias "FindWindowA" (ByVal lpClassName As String, _
      ByVal lpWindowName As String) As Long   
   Private Declare Function GetWindowPlacement Lib "user32" _
      (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
   Private Declare Function SetForegroundWindow Lib "user32" _
      (ByVal hwnd As Long) As Long
   Private Declare Function SetWindowPlacement Lib "user32" _
      (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
   Private Type RECT
      Left As Long
      Top As Long
      Right As Long
      Bottom As Long
   End Type
   Private Type POINTAPI
      x As Long
      y As Long
   End Type
   Private Type WINDOWPLACEMENT
      Length As Long
      flags As Long
      showCmd As Long
      ptMinPosition As POINTAPI
      ptMaxPosition As POINTAPI
      rcNormalPosition As RECT
   End Type
   Private Const SW_SHOWNORMAL = 1
   Private Const SW_SHOWMINIMIZED = 2
   Private Const SW_SHOWMAXIMIZED = 3
   Private Const SW_SHOWNOACTIVATE = 4
   Public Sub RestoreWindow(sWindowTitle As String)
      Dim wHandle As Long
      Dim currWinP As WINDOWPLACEMENT
      wHandle = FindWindow(vbNullString, sWindowTitle)
      If wHandle Then
         currWinP.Length = Len(currWinP)
         If GetWindowPlacement(wHandle, currWinP) > 0 Then
            If currWinP.showCmd = SW_SHOWMINIMIZED Then
               currWinP.Length = Len(currWinP)
               currWinP.flags = 0&
               currWinP.showCmd = SW_SHOWNORMAL
               Call SetWindowPlacement(wHandle, currWinP)
            Else
               Call SetForegroundWindow(wHandle)
               Call BringWindowToTop(wHandle)
           End If
        End If
      End If
   End Sub
   Private Sub Command1_Click()
      RestoreWindow ("Rechner")
   End Sub