Setting focus to acadapplication

Setting focus to acadapplication

Anonymous
Not applicable
283 Views
2 Replies
Message 1 of 3

Setting focus to acadapplication

Anonymous
Not applicable
I have a stand alone vb app that prompt the user to select some objects
after a button is clicked. This happens in a few steps. The first time the
button is clicked and AutoCAD is waiting for the user to select the objects,
the form immediately hides, however AutoCAD does not necessarily get focus.
I have to click on the AutoCAD window once in order for the focus to be set
to the AutoCAD.Application. Once there, I can select my objects and
everything works as intended. Once the focus has been set the first time,
any subsequent clicks on the button to select objects returns to the AutoCAD
window already with focus.

My question is how can I make sure that the AutoCAD application has the
focus once my form is hidden after the command button is clicked?

Thank you for your input.
0 Likes
284 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Try this out Tim.

Joe
--

Put this in your form module somewhere.


'make sure AutoCAD is in focus
If PutApplicationOnTop("AutoCAD 2002") = False Then
MsgBox "Could not set focus to AutoCAD", vbCritical, "Cannot process request"
Exit Sub
End If


Then in a standard .bas module

Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function PutFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal aint As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Public Const HWND_TOPMOST = -1
Public Const HWND_TOP = 0
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWWINDOW = &H40
Public Const GW_HWNDNEXT = 2

Public Function PutApplicationOnTop(AppName As String) As Boolean
Dim AppHwnd As Long
On Error Resume Next
'find the active application window
AppHwnd = FindWindowPartial(AppName)
'put application on top
SetForegroundWindow AppHwnd
'set focus to application window
If Not PutFocus(AppHwnd) Then PutApplicationOnTop = True
End Function

Public Function FindWindowPartial(Title As String) As Long
Dim hWndTmp As Long
Dim nRet As Integer
Dim TitleTmp As String
Dim hWndOver As Integer

'First find all the open windows so we can
'loop through them and find the right one.
hWndTmp = FindWindow(0&, 0&)

Do Until hWndTmp = 0
TitleTmp = Space$(256)
nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))

If nRet Then
'Let's prepare to compare
TitleTmp = UCase$(VBA.Left$(TitleTmp, nRet))

'Now we see if the window we chased down actually
'has the caption we want.
If InStr(TitleTmp, UCase(Title)) Then
FindWindowPartial = hWndTmp
Exit Do
End If
End If

hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
Loop
End Function
0 Likes
Message 3 of 3

Anonymous
Not applicable
That was awesome Joe!!!

 

Thank you very much for your help.  That
worked perfectly.


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Try
this out Tim.

Joe
--

Put this in your form module somewhere.

 
  'make sure AutoCAD is in focus
  If PutApplicationOnTop("AutoCAD 2002") = False Then
    MsgBox "Could not set focus to AutoCAD", vbCritical, "Cannot process request"
    Exit Sub
  End If

Then in a standard .bas module

 
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function PutFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal aint As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Public Const HWND_TOPMOST = -1
Public Const HWND_TOP = 0
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWWINDOW = &H40
Public Const GW_HWNDNEXT = 2

Public Function PutApplicationOnTop(AppName As String) As Boolean
Dim AppHwnd As Long
  On Error Resume Next
  'find the active application window
  AppHwnd = FindWindowPartial(AppName)
  'put application on top
  SetForegroundWindow AppHwnd
  'set focus to application window
  If Not PutFocus(AppHwnd) Then PutApplicationOnTop = True
End Function

Public Function FindWindowPartial(Title As String) As Long
Dim hWndTmp As Long
Dim nRet As Integer
Dim TitleTmp As String
Dim hWndOver As Integer

'First find all the open windows so we can
  'loop through them and find the right one.
  hWndTmp = FindWindow(0&, 0&)

Do Until hWndTmp = 0
    TitleTmp = Space$(256)
    nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))

If nRet Then
      'Let's prepare to compare
      TitleTmp = UCase$(VBA.Left$(TitleTmp, nRet))

'Now we see if the window we chased down actually
      'has the caption we want.
      If InStr(TitleTmp, UCase(Title)) Then
        FindWindowPartial = hWndTmp
        Exit Do
      End If
    End If

hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
  Loop
End Function

0 Likes