“哎呀,我的记事本怎么打不开了?真是的!”
  怎么回事?原来,记事本被“窗口杀手”杀了!想做一个窗口杀手吗?跟我来。
  先介绍一下我们的武器:
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  从函数名称上你一定看出点什么了。GetWindowText可以得到窗口的标题;GetWindow可以穷举所有的窗口(不如EnumWindows好用!想使用EnumWindows可参阅前一期的《看看你的密码》);GetWindowTextLength可得到窗口标题的长度;SendMessage我们以前用过了。如果我们穷举所有窗口,发现标题在黑名单上,就Send一个WM_CLOSE消息,那么……
  还愣着干什么?打开VB,新建一个模块,把上面的声明写进去。还要再声明几个常数:
Public Const WM_CLOSE = &H10
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDFIRST = 0
  接下来在窗体的“通用 声明”部分声明一个变量:Dim ToKill As String,然后在Form_Load中写上这样几句:
Private Sub Form_Load()
    Me.Hide '把自身隐藏起来
    On Error Resume Next
    
    '读取“黑名单”
    Open App.Path & "\KillWin.ini" For Input As #1
    If Err <> 0 Then End
    
    Input #1, ToKill
    Close #1
End Sub
  好了,拖一个时钟出来,设Interval为100,并为它写这些代码:
Private Sub Timer1_Timer()
    Dim WinText As String * 256
    Dim RenHWND As Integer
    Dim RetValue As Long
    Dim WinTextLength As Long
    
    RenHWND = GetWindow(Me.hwnd, GW_HWNDFIRST)

    Do
        RetValue = GetWindowText(RenHWND, WinText, 256)
        If RetValue <> 0 Then
            WinTextLength = GetWindowTextLength(RenHWND)

            '这句还有故障。VB使用了讨厌的汉字整字处理功能
            '导致我无法准确地得到窗口标题。
            '好像有人说过用Str???函数可解决这个问题
            '如果你找到了的话,别忘了告诉我一声。
            WinText = Left$(WinText, WinTextLength)

            If InStr(WinText, ToKill) <> 0 Then
                SendMessage RenHWND, WM_CLOSE, 0, 0 '又有窗口被杀了!
            End If
        End If
        RenHWND = GetWindow(RenHWND, GW_HWNDNEXT)
    Loop Until RenHWND = 0 '反复穷举直到所有窗体都穷举完毕
End Sub
  完工了!编译一下,在程序所在的目录新建一个KillWin.ini,填上你要杀的窗口,运行杀手,怎么样?
  你不能保证你的朋友都装了VB,所以还是下载一个免安装版本吧!