我正在将一个SDE要素类添加到ArcMap中,在添加之前,我必须单击“连接详细信息”窗口中的确定按钮。有没有办法按代码单击确定按钮?我想也许可以通过使用Window通知代码(例如下面的代码)来完成,但是我没有看到按钮单击确定或取消的任何选项。也许它可以通过“Windows.Forms.DialogResult.Ok”以某种方式或通过获得 ok 按钮的焦点来完成?
由于
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
'Close SDE connection details dialog
Dim WinWnd As Long, Ret As String
'Ask for a Window title
Ret = "Connection Details"
If Ret = "" Then Exit Sub
'Search the window
WinWnd = FindWindow(vbNullString, Ret)
'If WinWnd = 0 Then Messagebox.show "Couldn't find the window ...": Exit Sub
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
答案 0 :(得分:5)
我可以想到两种方法:
您可以找到确定按钮位置(使用FindWindowEx
和GetWindowRect
),将光标放在按钮(SetCursorPosition
)上并进行模拟鼠标单击(mouse_event
。)或者您可以将焦点设置在按钮上并模拟按 Enter 键(使用keyb_event
)。
向“确定”按钮的句柄发送BM_CLICK
消息。
我个人更喜欢第二种方法:
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Public BM_CLICK As Integer = &HF5
SendMessage(`OK BUTTON HANDLE`, BM_CLICK, 0, 0); // The button handle can be found with FindWindowEx