转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
一种简单的结束无法关闭的DOS窗口的方法         ★★★★

一种简单的结束无法关闭的DOS窗口的方法

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1459 更新时间:2009/4/23 18:59:45

关闭dos窗口



测试方法如下
先打开两个dos窗口
运行project1.exe
再打开两个dos窗口
注销,dos窗口后打开的直接被关闭,先打开的出现立即结束窗口后被关闭,速度取决于timer的设定

测试环境: xp, vb6

代码如下

''''窗体
Option Explicit

Private Sub Form_Load()
    Me.AutoRedraw = True
    oldwinproc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
    SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf OnMenu
End Sub


Private Sub Form_Unload(Cancel As Integer)
    SetWindowLong Me.hwnd, GWL_WNDPROC, oldwinproc
End Sub

Private Sub Timer1_Timer()
    CloseConsole1
End Sub

''''模块
Option Explicit

Public Const WM_SYSCOMMAND = &H112
Public Const WM_QUERYENDSESSION = &H11
Public Const PROCESS_TERMINATE = &H1
Public Const GWL_WNDPROC = (-4)
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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 FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public oldwinproc As Long
Public g__caption As String
Public g__hwnd As Long
''''查询是否关机并且关闭dos窗口
Public Function OnMenu(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Select Case wMsg
        Case WM_QUERYENDSESSION
            Form1.WindowState = 0
            Form1.Cls
            Form1.Print "系统要关机了"
            CloseConsole
        Case WM_SYSCOMMAND
        Case Else
    End Select
    OnMenu = CallWindowProc(oldwinproc, hwnd, wMsg, wParam, lParam)
End Function
''''枚举并且关闭
Public Function CloseConsole() As Long
    EnumWindows AddressOf EnumProc, 0
    CloseConsole = 1
End Function

''''枚举回调函数
Public Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim s As String * 255
    Dim s1 As String
    Dim i As Integer
    GetClassName hwnd, s, 255
    For i = 1 To 255
        If Mid(s, i, 1) <> Chr(0) Then
            s1 = s1 & Mid(s, i, 1)
        Else
            Exit For
        End If
    Next i
    If s1 = "ConsoleWindowClass" Then
        GetWindowText hwnd, s, 255
        TerminateProcessByHWND hwnd
        Form1.Print s
        Form1.Print hwnd
    End If
    EnumProc = 1
End Function

''''关闭任何普通应用程序的窗口
Public Function TerminateProcessByHWND(ByVal hCloseWnd As Long) As Boolean
    Dim hProcessID  As Long
    Dim hProcess    As Long
On Error GoTo PROC_EXIT
    If hCloseWnd = 0 Then GoTo PROC_EXIT
    If GetWindowThreadProcessId(hCloseWnd, hProcessID) = 0 Then GoTo PROC_EXIT
    hProcess = OpenProcess(PROCESS_TERMINATE, False, hProcessID)
    If hProcess = 0 Then GoTo PROC_EXIT
    If TerminateProcess(hProcess, 0&) = 0 Then GoTo PROC_EXIT
    TerminateProcessByHWND = True
PROC_EXIT:
    If Err.Number <> 0 Then
        Debug.Print Err.Description
        Err.Clear
    End If
End Function

''''定时查询是否有无法结束的dos窗口
Public Function CloseConsole1() As Long
    Dim console As Long
    console = FindWindow("ConsoleWindowClass", vbNullString)    ''''查找是否有dos窗口
    Dim s As String * 255
    g__caption = ""
    Dim i As Integer
    If console Then
        GetWindowText console, s, 255
        For i = 1 To 255
            If Mid(s, i, 1) <> Chr(0) Then
                g__caption = g__caption & Mid(s, i, 1)
            Else
                Exit For
            End If
        Next i
        ''''Debug.Print g__caption
        g__hwnd = console
        EnumWindows AddressOf EnumProc1, 0
    End If
    CloseConsole1 = 1
End Function
''''枚举是否出现立即结束的窗口
Public Function EnumProc1(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim s As String * 255
 &n

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台