可以列出所有窗口,但是无法列出所有程序(alt+ctrl+del那种)。

解决方案 »

  1.   

    Option Explicit
    'How to use
    '---------------------------------------------------------------------------------
    '    Dim i As Integer
    '    Dim objItem As ListItem
    '    Dim NumOfProcess As Long
    '    Dim objActiveProcess As SQLSysInfo
    '    Set objActiveProcess = New SQLSysInfo
    '    NumOfProcess = objActiveProcess.GetActiveProcess
    '    For i = 1 To NumOfProcess
    '        Set objItem = ListView2.ListItems.Add(, , objActiveProcess.szExeFile(i))
    '        With objItem
    '            .SubItems(1) = objActiveProcess.th32ProcessID(i)
    '            .SubItems(2) = objActiveProcess.th32DefaultHeapID(i)
    '            .SubItems(3) = objActiveProcess.thModuleID(i)
    '            .SubItems(4) = objActiveProcess.cntThreads(i)
    '            .SubItems(5) = objActiveProcess.th32ParentProcessID(i)
    '        End With
    '    Next
    '    Set objActiveProcess = NothingPrivate Const TH32CS_SNAPPROCESS As Long = 2&
    Private Const MAX_PATH As Integer = 260Private Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szExeFile As String * MAX_PATH
    End TypePrivate Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)Dim ListOfActiveProcess() As PROCESSENTRY32Public Function szExeFile(ByVal Index As Long) As String
        szExeFile = ListOfActiveProcess(Index).szExeFile
    End FunctionPublic Function dwFlags(ByVal Index As Long) As Long
        dwFlags = ListOfActiveProcess(Index).dwFlags
    End FunctionPublic Function pcPriClassBase(ByVal Index As Long) As Long
        pcPriClassBase = ListOfActiveProcess(Index).pcPriClassBase
    End FunctionPublic Function th32ParentProcessID(ByVal Index As Long) As Long
        th32ParentProcessID = ListOfActiveProcess(Index).th32ParentProcessID
    End FunctionPublic Function cntThreads(ByVal Index As Long) As Long
        cntThreads = ListOfActiveProcess(Index).cntThreads
    End FunctionPublic Function thModuleID(ByVal Index As Long) As Long
        thModuleID = ListOfActiveProcess(Index).th32ModuleID
    End FunctionPublic Function th32DefaultHeapID(ByVal Index As Long) As Long
        th32DefaultHeapID = ListOfActiveProcess(Index).th32DefaultHeapID
    End FunctionPublic Function th32ProcessID(ByVal Index As Long) As Long
        th32ProcessID = ListOfActiveProcess(Index).th32ProcessID
    End FunctionPublic Function cntUsage(ByVal Index As Long) As Long
        cntUsage = ListOfActiveProcess(Index).cntUsage
    End FunctionPublic Function dwSize(ByVal Index As Long) As Long
        dwSize = ListOfActiveProcess(Index).dwSize
    End FunctionPublic Function GetActiveProcess() As Long
        Dim hToolhelpSnapshot As Long
        Dim tProcess As PROCESSENTRY32
        Dim r As Long, i As Integer
        hToolhelpSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        If hToolhelpSnapshot = 0 Then
            GetActiveProcess = 0
            Exit Function
        End If
        With tProcess
            .dwSize = Len(tProcess)
            r = ProcessFirst(hToolhelpSnapshot, tProcess)
            ReDim Preserve ListOfActiveProcess(20)
            Do While r
                i = i + 1
                If i Mod 20 = 0 Then ReDim Preserve ListOfActiveProcess(i + 20)
                ListOfActiveProcess(i) = tProcess
                r = ProcessNext(hToolhelpSnapshot, tProcess)
            Loop
        End With
        GetActiveProcess = i
        Call CloseHandle(hToolhelpSnapshot)
    End Function
    建立一个类模块,名称为:SQLSysInfo,然后copy上面代码,
    使用示例:
    Dim objActiveProcess As SQLSysInfo
        Set objActiveProcess = New SQLSysInfo
        NumOfProcess = objActiveProcess.GetActiveProcess
        strmsg = "**processmsg"
        
        List1.Clear
        For i = 1 To NumOfProcess
        List1.AddItem objActiveProcess.szExeFile(i)
        Next i
    记得给分哦!!
      

  2.   


    Public Declare Function GetForegroundWindow Lib "user32.dll" () As Long
    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
    Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    Public Declare Function SetWindowText Lib "user32.dll" Alias "SetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String) As Long
    Public Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal nCmdShow As Long) As LongPublic Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As LongPublic Declare Function SetWindowPos Lib "user32.dll" (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) As Long
    Public Const SWP_NOSIZE = &H1
    Public Const HWND_TOPMOST = -1
    Public Const HWND_NOTOPMOST = -2Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal msg As Long, wParam As Any, lParam As Any) As Long
    Public Const WM_CLOSE = &H10
    Public Const WM_DESTROY = &H2Public Declare Function SendMessageTimeout Lib "user32" _
        Alias "SendMessageTimeoutA" (ByVal hWnd As Long, _
        ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, _
        ByVal fuFlags As Long, ByVal uTimeout As Long, _
        pdwResult As Long) As Long
    Public Const SMTO_BLOCK = &H1
    Public Const SMTO_ABORTIFHUNG = &H2
    Public Const WM_NULL = &H0
    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongPublic Declare Function OpenProcess Lib "kernel32" _
        (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessId As Long) As Long
        Public Const PROCESS_ALL_ACCESS = &H1F0FFF
    Public Declare Function EnableWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal fEnable As Long) As Long
    Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
    Public Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As LongPublic Function getalltopwindows(ByVal hWnd As Long, ByVal lParam As Long) As LongDim foregroundwindow As Long
    Dim textlen As Long
    Dim windowtext As String
    Dim svar As Long
    Static lastwindowtext As Stringforegroundwindow = hWnd
    textlen = GetWindowTextLength(foregroundwindow) + 1windowtext = Space(textlen)
    svar = GetWindowText(foregroundwindow, windowtext, textlen)
    windowtext = Left(windowtext, Len(windowtext) - 1)If windowtext = "" Then GoTo slaskIf Form1.Check2.Value = 1 Then
    If IsWindowVisible(foregroundwindow) > 0 ThenIf windowtext = Form1.Caption Then GoTo slask
    Form1.List1.AddItem windowtext
    Form1.List1.ItemData(Form1.List1.NewIndex) = foregroundwindow
    lastwindowtext = windowtextEnd IfElse
    If windowtext = Form1.Caption Then GoTo slask
    Form1.List1.AddItem windowtext
    Form1.List1.ItemData(Form1.List1.NewIndex) = foregroundwindow
    lastwindowtext = windowtext
    End Ifslask:getalltopwindows = 1
    End Function