|
更加奇怪的是,在WORD中,添加一个按钮“隐藏”,添加第二个按钮“显示”,打开文件后,第一次点击隐藏按钮竟然不能隐藏,点击第二次又可以了,而且两次获得的窗口句柄竟然不同,第一次获得的窗口文本为空,第二次可以获得正确的窗口标题文本。这是为什么呢?请版主不吝赐教,谢谢!!
Doc1.rar
(11.55 KB, 下载次数: 44)
'模块中代码
'//查找需要的窗口
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'//取得窗口样式位
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
'//设置窗口样式位
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'//重绘窗体标题栏
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'//以下定义常数及变量
Private Const GWL_STYLE = (-16) '窗口样式
Private Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
'//****************************************************************************************************************************************
Sub BtResume_Click()
Dim Istype As Long
Dim hWnd As Long
Dim MyStr As String
'Create a buffer
MyStr = String(100, Chr$(0))
'Get the windowtext
'//取得窗口样式位
hWnd = FindWindow("OpusApp", vbNullString)
Istype = GetWindowLong(hWnd, GWL_STYLE)
'//窗体样式位: 原样式和标题栏
Istype = Istype Or WS_CAPTION
'//重设窗体样式位
SetWindowLong hWnd, GWL_STYLE, Istype
GetWindowText hWnd, MyStr, 100
'//重绘窗体标题栏
DrawMenuBar hWnd
MsgBox hWnd & ",," & MyStr
End Sub
'//****************************************************************************************************************************************
Sub click1()
Dim Istype As Long
Dim hWnd As Long
Dim MyStr As String
'Create a buffer
MyStr = String(100, Chr$(0))
'Get the windowtext
hWnd = FindWindow("OpusApp", vbNullString)
'Excel改成 hWnd = FindWindow("XLMAIN", vbNullString)
'//取得窗口样式位
Istype = GetWindowLong(hWnd, GWL_STYLE)
'//窗体样式位: 原样式无标题栏
Istype = Istype And Not WS_CAPTION
'//重设窗体样式位
SetWindowLong hWnd, GWL_STYLE, Istype
GetWindowText hWnd, MyStr, 100
'//重绘窗体标题栏
DrawMenuBar hWnd
MsgBox hWnd & ",," & MyStr
End Sub
'ThisDocument中代码
Private Sub CommandButton1_Click()
Call click1
End Sub
Private Sub CommandButton2_Click()
Call BtResume_Click
End Sub
[ 本帖最后由 headbegger 于 2009-8-23 10:16 编辑 ] |
|