发送消息问题,一个字:难
有一个应用程序,它有一个窗体:Form1,Form1上有一个按钮:Abort;
如果单击Abort,则弹出一个对话框,询问是否要退出?上面仅有两个按钮:Yes、No;
如果单击Yes,就退出该Form1,如果单击No,则不退出Form1。
我的问题:
我用VB API:finewindowEx、sendmessage等实现了单击Abort,
但是随之谈出的退化框,我想继续单击Yes,请问题如何实现?
问题点数:50、回复次数:4Top
1 楼jjkk168(老加班的人--好好学习,天天吃饭)回复于 2006-07-01 23:27:50 得分 5
那再使用FindWindow,然后再利用FindWindowEx找到Yes按钮,再SendMessageTop
2 楼province_(雍昊)回复于 2006-07-02 08:47:46 得分 5
或者简单点(反正你已经找到该目标窗体了),直接发WM_DESTROY消息好了。:)Top
3 楼verywzm(寒江雪)回复于 2006-07-02 10:55:05 得分 30
'==========================窗体代码==========================
Option Explicit
Private Sub Command1_Click()
SimulateClick "Form1", "About" '前为顶级窗口caption,后为按钮caption
DoEvents
SimulateClick "工程1", "Yes"
End Sub
'==========================模块代码==========================
'Option Explicit
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam 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
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Public 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
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long '发送消息
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Dim Title As String
'鼠标消息
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const MK_LBUTTON = &H1
Public FatherTitle As String
Public ChildTitle As String
Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean '遍查主窗口
Dim buf As String * 1024
Dim length As Long
length = GetWindowText(app_hwnd, buf, Len(buf))
Title = Left$(buf, length)
If InStr(Title, Chr(0)) <> 0 Then
Title = Left(Title, InStr(Title, Chr(0)) - 1) '去掉尾巴冗余
End If
If Title = FatherTitle Then '判断窗口的标题(区分大小写)
Call GetZiWin(app_hwnd)
End If
EnumProc = 1
End Function
Public Function GetZiWin(window_hwnd As Long) As String
Dim buflen As Long
Dim child_hwnd As Long
Dim children() As Long
Dim num_children As Integer
Dim i As Integer
Dim ButtonState As Long
GetZiWin = GetWinText(window_hwnd)
If GetZiWin = ChildTitle Then
SendMessage window_hwnd, WM_LBUTTONDOWN, 0, 0
SendMessage window_hwnd, WM_LBUTTONUP, 0, 0
PostMessage window_hwnd, WM_LBUTTONDOWN, 0, 0
PostMessage window_hwnd, WM_LBUTTONUP, 0, 0
Exit Function
End If
num_children = 0
child_hwnd = GetWindow(window_hwnd, GW_CHILD) '取得第 1 个子窗口的句柄
Do While child_hwnd <> 0 '如果有子窗口
num_children = num_children + 1
ReDim Preserve children(1 To num_children)
children(num_children) = child_hwnd
child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT) '取得下一个兄弟窗口的句柄
Loop
For i = 1 To num_children
Call GetZiWin(children(i))
Next i
End Function
Public Function GetWinText(window_hwnd As Long) As String '取得子窗口的值
Dim buf As String * 128
Dim length As Long
length = GetWindowText(window_hwnd, buf, Len(buf)) '该函数无法取得编辑框内容,如果要取得,使用以下被注释代码
GetWinText = Left$(buf, length)
If InStr(GetWinText, Chr(0)) <> 0 Then
GetWinText = Left(GetWinText, InStr(GetWinText, Chr(0)) - 1) '去掉尾巴冗余
End If
End Function
Public Sub SimulateClick(Title As String, Caption As String)
FatherTitle = Title
ChildTitle = Caption
EnumWindows AddressOf EnumProc, 0 '枚举窗口列表中的所有父窗口(顶级和被所有窗口)
End Sub
Top
4 楼zcsor(偶业余的虽然星星了,但是水平依然是非常业余的。)回复于 2006-07-02 12:48:16 得分 10
获取最顶窗体就是你的弹出窗体,用FINDWINDOWEX获取父窗口句柄为顶窗体句柄的控件,就是你弹出窗体里面的控件,识别他们,发送消息Top




