窗口 - 按代码单击"确定"按钮

art*_*k21 4 vb.net winapi button

我正在将一个SDE要素类添加到ArcMap中,在添加之前,我必须单击"连接详细信息"窗口中的" 确定"按钮.有没有办法按代码点击确定按钮?我想也许可以通过使用Window通知代码(例如下面的代码)来完成,但是我没有看到按钮单击" 确定"或"取消"的任何选项.也许它可以通过"Windows.Forms.DialogResult.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&
Run Code Online (Sandbox Code Playgroud)

far*_*jad 5

我可以想到两种方法:

  1. 您可以找到OK按钮位置(使用FindWindowExGetWindowRect),将光标放在按钮上(SetCursorPosition)并模拟鼠标单击(mouse_event.)或者您可以设置焦点按钮并模拟按Enter键(带keyb_event).

  2. 发送BM_CLICK消息到OK按钮的句柄.

我个人更喜欢第二种方法:

<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
Run Code Online (Sandbox Code Playgroud)