Got Focus Event for Modeless forms using AcFocusCtrl

Discussion in 'AutoCAD' started by Terry W. Dotson, Mar 31, 2005.

  1. UserForm_Activate fires when the form is activated, but when you click
    over into the editor, then back on the form, how do you know when the
    form is active again? The AcFocusCtrl has some options but none fire
    when the form gets focus.

    Thanks in advance,

    Terry
     
    Terry W. Dotson, Mar 31, 2005
    #1
  2. Terry W. Dotson

    GTVic Guest

    Modeless forms seem to only get focus when you click on them and immediately lose focus when the mouse button is released.

    Since modeless forms never have ongoing focus then even if you found some code that worked, your code would fire on every single click of the mouse.

    If that is what you would want then the easiest (but not very elegant) way to do this would be to put your code in the click event of the form and the click events of everything on the form.
     
    GTVic, Mar 31, 2005
    #2
  3. Terry W. Dotson

    bcoward Guest

    Terry,

    I hope your not pulling your hair out but maybe I can at least give some assistance that will help.

    You know that your form is active again via the API. A couple costants that I think will resolve this are SW_SHOWNOACTIVATE and/or WS_OVERLAPPEDWINDOW


    To show a window without activating it you could....

    When you use ShowWindow API. This API when called will show the window and the activation depends on the second parameter. This is where I'd use SW_SHOWNOACTIVATE.

    Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Private Const SW_SHOWNOACTIVATE = 4

    Also....giver this some thought.....I believe your answer is in an api that uses the SW_SHOWNOACTIVATE constant.

    Private Sub Form_Load()
    ShowWindow Me.hWnd, SW_SHOWNOACTIVATE
    End Sub


    Sorry for not being specific but I hope I stirred something.

    Regards,

    Bob Coward
    CADS, Inc

    800-366-0946
     
    bcoward, Apr 1, 2005
    #3
  4. Terry W. Dotson

    GTVic Guest

    When the load event exits the window is already going to be shown non-active. There is no other way to show it with the way Autodesk has configured VBA.

    Aren't we talking VBA? Only VB has a hWnd property?
     
    GTVic, Apr 1, 2005
    #4
  5. Terry W. Dotson

    bcoward Guest

    hWnd property

    Search the archives...it's there
     
    bcoward, Apr 1, 2005
    #5
  6. Terry W. Dotson

    bcoward Guest

    hWnd property

    Search the archives...it's there

    Take form_load as an example not a directive
     
    bcoward, Apr 1, 2005
    #6
  7. Terry W. Dotson

    bcoward Guest

    bcoward, Apr 1, 2005
    #7
  8. Terry W. Dotson

    bcoward Guest

    Terry,

    I had a complete brain waste. I tried to make this too complicated I believe.

    Why can't you use the GetActiveWindow API to not only determine if the form is active or not.

    Or you could use the GetForeground window API.

    GEtForeground API

    The GetForegroundWindow function returns the handle of the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.

    GetWindow API

    The GetActiveWindow function retrieves the window handle to the active window associated with the thread that calls the function.

    Hope this is mud that is easier to look through.

    Good luck,

    Bob Coward
    CADS, Inc

    800-366-0946
     
    bcoward, Apr 1, 2005
    #8
  9. Here, it's simple enough and I know it in my sleep :)

    Private Declare Function GetActiveWindow Lib "user32" () As Long

    Public Property Get hwnd() As Long
    '+--get forms handle
    hwnd = GetActiveWindow()
    End Property

    GT - there is very little not possible with the Windows API. Go download a
    copy of the API Guide [do a google for it]

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Apr 1, 2005
    #9
  10. Terry W. Dotson

    GTVic Guest

    MT - I use that particular API call quite a bit. You are preaching to the choir.

    I was pointing out to the other fellow that he was confusing VB with VBA. That maybe he was assuming the original question was for a VB form. In that case his answer might not be relevant.

    The poster wants to know when his window has the focus. The problem is that a modeless VBA form in AutoCAD 2005 never has a sustained focus. It gets focus when the mouse button is clicked down and releases focus when the mouse button is released. So no need for complicated API calls. Just use the click events.

    In response to the other post (below) where would you put the GetForegroundWindow call - you would have to put it in some event that fires when the window becomes active - problem is that is what the poster is looking for (an event that fires when the window becomes active) - if you found that magic event handler then you would solve his problem without any API calls.
     
    GTVic, Apr 1, 2005
    #10
  11. Unless you use the Autodesk supplied AcFocusCtrl or WinAPI calls. For
    example if I have an textbox in my form and you click it, the cursor
    will remain there blinking and my form has focus, until the user clicks
    in the drawing editor. I don't have a problem with that.

    I (and others I'm sure) would like an event when the modeless form gets
    focus again so that the form could check for changes in the status of
    the drawing. I would have expected one of the userform_ events to fire,
    but the only one that does is mouseover. And that fires even if the
    user has not clicked back in the modeless form again, so its not really
    of use.

    Terry
     
    Terry W. Dotson, Apr 1, 2005
    #11
  12. I suppose I could use one of these in mousemove (which does fire). I
    just hate putting code in a mousemove event, it seems like a lot of cpu
    overhead.

    Terry
     
    Terry W. Dotson, Apr 1, 2005
    #12
  13. I've posted an example of this in autodesk.autocad.customer-files.

    Terry
     
    Terry W. Dotson, Apr 1, 2005
    #13
  14. Terry W. Dotson

    James Buzbee Guest

    No help but same problem in ObjectDCL (2.x). I just use a refresh button -
    suxs I know.

    jb
     
    James Buzbee, Apr 1, 2005
    #14
  15. There is a way around it but it might require a lot of time for you. Switch
    your app to a .net assembly. You can then use a custom tool palette in
    2k5-2k6 which has plenty of events.

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Apr 1, 2005
    #15
  16. Sorry if I was preaching to the choir. Your comment about vba not having a
    hwnd property suggested otherwise.

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Apr 1, 2005
    #16
  17. Thats on the to-do list, but was hoping to eak out one last project that
    would be 2002/2004 compatible also.

    Thanks, Terry

    Terry
     
    Terry W. Dotson, Apr 1, 2005
    #17
  18. Terry W. Dotson

    GTVic Guest

    An accurate statement - it doesn't have the property. You need a workaround API call to get the value.
     
    GTVic, Apr 1, 2005
    #18
  19. Terry W. Dotson

    GTVic Guest

    I have a solution that is fairly simple, uses a few API calls and a callback function to intercept windows messages.

    The only problem is that it hangs occasionally. I'm not sure why. When it hangs I usually hit Ctrl-Break and go to debug mode and then F5 to continue.

    Maybe this solution is not too compatible with VBA or maybe someone can suggest an improvement.

    The idea came from here:

    http://www.developerfusion.com/show/49/2/
     
    GTVic, Apr 3, 2005
    #19
  20. Looks dangerous. I'd rather use a refresh button than introduce
    instability.

    Thanks, Terry
     
    Terry W. Dotson, Apr 3, 2005
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.