Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
    'Ask for a Window title
    Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
    'Search the window
    WinWnd = FindWindow(vbNullString, Ret)
    If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
    'Show the window
    ShowWindow WinWnd, SW_SHOWNORMAL
    'Create a buffer
    lpClassName = Space(256)
    'retrieve the class name
    RetVal = GetClassName(WinWnd, lpClassName, 256)
    'Show the classname
    MsgBox "Classname: " + Left$(lpClassName, RetVal)
    'Post a message to the window to close itself
    PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解决方案 »

  1.   

    试试吧,是你要的!(关掉本身还用得着这么麻烦么?EDN就行!)
      

  2.   

    hehe,我是菜鸟。谢谢你,我试过了,有些可以有些不行。可以把其中关掉进程的语句指出来吗?
    还有点那里给你分?
      

  3.   

    1、通过FindWindow()函数找到你要关掉的程序的句柄;
    2、通过PostMessage()函数发送"WM_CLOSE"信息关闭该程序; 3、给分:点击你的帖子的右边的“管理”,在出现的页面中给分!你可以看一下CSDN的帮助。
      

  4.   

    很高兴这个时段还看见你的回复(有点感动呢  :~) )"WinWnd"这个值是不是一个进程的唯一标识?因为发现当打开两个相同的程序(比如打开两个"我的电脑")它们的"WinWnd"都不同的。还有"WM_COLSE"是不是一个固定的参数(用来关闭进程)还想深入一点了解如何得到"WinWnd"这个值的方法,因为我的程序是按照一个预先安排好的时间表来打开一个程序,然后再关闭它,有时可能会同时开两个或以上,所以如果可以在打开程序时就得知"WinWnd"号就好办多了。中途不需要人手输入窗口名称,该如何才可做到这点?先给90分吧,我不想打这贴关掉,剩下的110分解答了再给。 :)