InPowerS.Net

 找回密碼
 註冊
搜索
查看: 1734|回復: 0

vb激活窗口

[複製鏈接]
發表於 2008-12-25 22:55:13 | 顯示全部樓層 |閱讀模式
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
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

小黑屋|Archiver|手機版|InPowerS.Net

GMT+8, 2018-12-12 18:46

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表