我使用findwindow 查找某个word文档是否已经打开,方法如下:
但是现在我发现我在某些机器上面使用时不会出现问题,但是在有些机器上面使用这个方法的时候
就不能使用了,我现在很纳闷,不知道是不是在使用前机器还需要什么配置吗?还是和机器有什么关系啊?
希望大家帮帮忙!!!!!
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Function FileOpen(ByVal strFileName As String) As Boolean
Dim lpCaption As String
Const lpClassName = "OpusApp" 'word的类名,固定的
lpCaption = strFileName
MsgBox FindWindow(0, lpCaption)
If FindWindow(lpClassName, lpCaption) <> "0" Then
FileOpen = True
Else
FileOpen = False
End If
End Function
但是现在我发现我在某些机器上面使用时不会出现问题,但是在有些机器上面使用这个方法的时候
就不能使用了,我现在很纳闷,不知道是不是在使用前机器还需要什么配置吗?还是和机器有什么关系啊?
希望大家帮帮忙!!!!!
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Function FileOpen(ByVal strFileName As String) As Boolean
Dim lpCaption As String
Const lpClassName = "OpusApp" 'word的类名,固定的
lpCaption = strFileName
MsgBox FindWindow(0, lpCaption)
If FindWindow(lpClassName, lpCaption) <> "0" Then
FileOpen = True
Else
FileOpen = False
End If
End Function
FindWindow(lpClassName, lpCaption)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (lpClassName As Any, ByVal lpWindowName As String) As LongMsgBox FindWindow(Byval 0&, lpCaption)
If FindWindow(Byval lpClassName$, lpCaption) <> 0 Then或Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongMsgBox FindWindow(vbNullString, lpCaption)
If FindWindow(lpClassName, lpCaption) <> 0 Then
我看了你的代码你的API声明是正确的,但是你应该理解这个函数在VB当中使用时,不应该传入0作为参数,而是vbnullstring,也就是说(你的代码运行时只有MSGBOX句返回不正确):
1、把你代码中MsgBox FindWindow(0, lpCaption)换为MsgBox FindWindow(vbnullstring, lpCaption)
2、我不知道你调用代码中FileOpen时是如何输入的,应该是类似“文档 1 - Microsoft Word”的形式
将你的代码修改了一下:(打开WORLD空文档后运行下面代码)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Function FileOpen(ByVal strFileName As String) As Boolean
Dim lpCaption As String
Const lpClassName = "OpusApp" 'word的类名,固定的
MsgBox FindWindow(vbNullString, strFileName)
FileOpen = FindWindow(lpClassName, strFileName)
End FunctionPrivate Sub Command1_Click()
Me.Caption = FileOpen("文档 1 - Microsoft Word")
End Sub
不知道你查找他是要做什么,我提供一个已经知道窗口类名,但只能确定该窗口名的一部分时,确定该窗口句柄的函数,希望对你有所启示,体会一下用FINDWINDOWEX的好处Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private 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
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long'参数意义:cname,窗口类名;ftext窗口名中包含的字符
'我是个业余的,所以命名规范不怎么样,将就看吧
Public Function mKillWindow(ByVal cName As String, ByRef fText As String) As Long
'初始化
Dim myStr As String * 255
Dim strLen As String
Dim bWnd As Long
'保存链表指针位置
Dim bWndback As Long
strLen = Len(myStr)
bWndback = 0
Do
DoEvents
'获所有类名为cname的取子窗口标志,这里呢,第一个参数用0,代表要查询桌面窗口的子窗口,所有顶级窗口都是桌面的子窗口,你打开的WORLD也不例外
bWnd = FindWindowEx(0, bWndback, cName, vbNullString)
If bWnd <> 0 Then
'获窗口名为cname的第一个子窗口的标题
GetWindowText bWnd, myStr, strLen
myStr = Trim(myStr)
If InStrRev(myStr, fText) Then '如果含有ftext,返回该窗口句柄
mGetWindow = bWnd
Exit Do
End If
Else
Exit Do
mGetWindow = 0
End If
bWndback = bWnd
Loop
End Function
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
并没有用到,是我从正在写的程序里复制下来的,我要关闭一个程序,这个API是用来支持下面代码的
GetWindowThreadProcessId hwnd, PID '获取窗口所在进程PID,第一个参数是上面函数返回的,第二个是获得的PID
'结束
lProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, PID)
TerminateProcess lProcess, 0&'关闭这个进程,呵呵,我卡在这里了,虽然提升了程序权限,但是还是无法关闭其他用户身份运行的标准进程.也希望高手指点,我对服务不是很了解.....
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Function FileOpen(ByVal strFileName As String) As Boolean
Dim lpCaption As String
Const lpClassName = "OpusApp" 'word的类名,固定的
lpCaption = strFileName & " - Microsoft Word"
MsgBox FindWindow(vbNullString, lpCaption)
If FindWindow(lpClassName, lpCaption) <> "0" Then
FileOpen = True
Else
FileOpen = False
End If
End Function