Reputation: 330
I'm adding a SDE feature class into ArcMap and before it adds in I have to click the ok button on the "Connection Details" Window. Is there a way to click the ok button by code? I was thinking maybe it could be done by using Window notification code (e.g. code below), however I'm not seeing any option for button click Ok or Cancel. Maybe it could be done by "Windows.Forms.DialogResult.Ok" somehow or by getting the focus of the ok button?
Thanks
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&
Upvotes: 4
Views: 10013
Reputation: 20424
I can think of two ways to do so:
You can find the OK button position (using FindWindowEx
and GetWindowRect
), place the cursor on the button (SetCursorPosition
) and simulate a mouse click (mouse_event
.) Or you can set focus on the button and simulate pressing Enter key (with keyb_event
).
Send BM_CLICK
message to OK button's handle.
I personally like the second approach better:
<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
Upvotes: 5