VB6代码:关闭记事本程序

我每次要打开10个以内的记事本,其中一个记事本的名字是“摘要.txt”,另外的记事本名字任意,怎么用VB6代码实现按一个按钮,将当前除了“摘要.txt”所有打开的记事本都... 我每次要打开10个以内的记事本,其中一个记事本的名字是“摘要.txt”,另外的记事本名字任意,怎么用VB6代码
实现按一个按钮,将当前除了“摘要.txt”所有打开的记事本都关掉?如果这些记事本有改动要提示保存,没有改动的直接关
掉。(有的网友可能觉得关闭10个以内的记事本很快,但这只是一次,我每天要处理将近一千个记事本,就太耽误时间了。)
是WIN系统,不是MAC .已追加50分! 请自己任意建10个记事本验证通过。
展开
 我来答
zenper
2010-10-27 · TA获得超过153个赞
知道小有建树答主
回答量:101
采纳率:0%
帮助的人:60万
展开全部
窗体中添加一个Command控件

工程中新建一个模块:

Option Explicit
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const NILL = 0&
Private Const WM_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060&

Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Pid1 As Long
Dim wText As String * 255, tText As String
GetWindowThreadProcessId hwnd, Pid1
GetWindowText hwnd, wText, 100
tText = Left(wText, InStr(wText, Chr(0)) - 1)
If Right(tText, 3) = "记事本" Then
If tText <> "摘要.txt - 记事本" Then SendMessage hwnd, WM_SYSCOMMAND, SC_CLOSE, NILL
End If
EnumWindowsProc = True
End Function

Public Sub ClearWindows()
EnumWindows AddressOf EnumWindowsProc, 0
End Sub

窗体中添加如下代码:

Private Sub Command1_Click()
ClearWindows
End Sub

Private Sub Form_Load()
Command1.Caption = "关闭记事本窗口"
End Sub
ljl88900
2010-10-27 · TA获得超过2661个赞
知道大有可为答主
回答量:2197
采纳率:100%
帮助的人:2632万
展开全部
一、新建一个模块,复制下面代码:
Option Explicit

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, ByVal cch As Long) _
As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Public Function CloseWindowNames(Title As String)
Dim lngDeskTopHandle As Long
Dim lngHand As Long
Dim strName As String
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
Do While lngHand <> 0
strName = String(255, 0)
lngDeskTopHandle = GetWindowText(lngHand, strName, Len(strName))
strName = StrConv(LeftB(StrConv(strName, vbFromUnicode), lngDeskTopHandle), vbUnicode)
lngDeskTopHandle = InStr(1, strName, " - 记事本", vbTextCompare)
If lngDeskTopHandle > 0 Then
strName = Trim(Left(strName, lngDeskTopHandle - 1))
If strName <> Title Then
PostMessage lngHand, &H10, 0, 0 '关闭窗口
End If
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
Loop
End Function

二、调用举例:
比如:将当前除了“摘要.txt”所有打开的记事本都关掉.

Private Sub Command1_Click()
CloseWindowNames "摘要.txt"
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xuanxuan163com
2010-10-27 · TA获得超过2769个赞
知道大有可为答主
回答量:1709
采纳率:75%
帮助的人:1605万
展开全部
可以使用VB或VBA编程,调用API函数:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10

Sub b()
Dim hwnd1 As Long
Dim hwnd2 As Long
Dim wMsg As Long
Dim i As Long

i = 0
wMsg = WM_CLOSE
hwnd1 = FindWindow("Notepad", "摘要.txt - 记事本")
Do
hwnd2 = FindWindow("Notepad", vbNullString) '获得窗口句柄
If hwnd2 = hwnd1 Then
i = i + 1
If i > 1 Then Exit Sub
Else
SendMessage hwnd2, wMsg, 0, 0
End If
Loop Until hwnd2 = 0
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
fifaboy
2010-10-27 · TA获得超过3219个赞
知道大有可为答主
回答量:3046
采纳率:0%
帮助的人:1715万
展开全部
如果是自己用,可以考虑用sendkeys实现,虽然有很多问题,但是一般也能解决不少。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式