肥龍 發表於 2008-12-25 22:55:13

vb激活窗口

Option Explicit
Const SW_MINIMIZE = 6
Const SW_SHOWNORMAL = 1
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function IsZoomed Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim lngWindow As Long
Private Sub Form_Load()
    If App.PrevInstance Then
      lngWindow = FindWindow(vbNullString, "測試窗口")
      If lngWindow <> 0 Then
            If IsIconic(lngWindow) = 0 Then
                ShowWindow lngWindow, SW_MINIMIZE
            Else
                ShowWindow lngWindow, SW_SHOWNORMAL
            End If
      End If
      End
    End If
    Me.Caption = "測試窗口"
End Sub

Private Function GetOtherWindowState(ByVal lngHWND As Long) As String
    GetOtherWindowState = "普通"
    If IsWindow(lngHWND) <> 0 Then
      GetOtherWindowState = "沒有找到該表單"
    ElseIf IsIconic(lngHWND) <> 0 Then
      GetOtherWindowState = "表單最小化"
    ElseIf IsZoomed(lngHWND) <> 0 Then
      GetOtherWindowState = "表單最大化"
    End If
End Function

判斷別人的視窗的狀態 並且在重新運行的時候可以來回切換!
====================================================================

先打開一個記事本,讓他不是當前視窗(無焦點)。程式裡建一個Command1,按一下按鈕看看效果。
代碼如下。
=================
Private Declare Function SetForegroundWindow 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 Sub Command1_Click()
Dim h As Long
h = FindWindow("notepad", vbNullString)
SetForegroundWindow h
End Sub
頁: [1]
查看完整版本: vb激活窗口