VB6代码:关闭记事本程序
我每次要打开10个以内的记事本,其中一个记事本的名字是“摘要.txt”,另外的记事本名字任意,怎么用VB6代码实现按一个按钮,将当前除了“摘要.txt”所有打开的记事本都...
我每次要打开10个以内的记事本,其中一个记事本的名字是“摘要.txt”,另外的记事本名字任意,怎么用VB6代码
实现按一个按钮,将当前除了“摘要.txt”所有打开的记事本都关掉?如果这些记事本有改动要提示保存,没有改动的直接关
掉。(有的网友可能觉得关闭10个以内的记事本很快,但这只是一次,我每天要处理将近一千个记事本,就太耽误时间了。)
是WIN系统,不是MAC .已追加50分! 请自己任意建10个记事本验证通过。 展开
实现按一个按钮,将当前除了“摘要.txt”所有打开的记事本都关掉?如果这些记事本有改动要提示保存,没有改动的直接关
掉。(有的网友可能觉得关闭10个以内的记事本很快,但这只是一次,我每天要处理将近一千个记事本,就太耽误时间了。)
是WIN系统,不是MAC .已追加50分! 请自己任意建10个记事本验证通过。 展开
4个回答
展开全部
窗体中添加一个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
工程中新建一个模块:
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
展开全部
一、新建一个模块,复制下面代码:
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以使用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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是自己用,可以考虑用sendkeys实现,虽然有很多问题,但是一般也能解决不少。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询