ATL ActiveX control loses focus when VB form displayed 
Author Message
 ATL ActiveX control loses focus when VB form displayed

Here's my problem:

I have a number of ATL activex controls that are windowed and built
using Win GDI controls (edit boxes, combo boxes, buttons, etc.). When
I place them on a VB form and display a second VB form from within one
of the control's events that have been exposed to VB (ie., Change or
ButtonClick) I find that when the second form is displayed modelessly
focus bounces onto the next control in the tab order (on the original
form) just as the second form is displayed.

I can then use the second form as per usual, but on closing it, the
focus stays with the next control (the one focus "bounced" to earlier)
on the original form, not the ATL control that had focus when the
second VB form was displayed.

This does not occur if the second form is displayed as a modal form
(using Form.Show vbModal).

The problem also doesn't occur if I use an MFC activex control. I have
witnessed the problem across a series of ATL controls, some containing
just an edit box (where I used a WM_CHAR-inspired Change event to
display the second form), others containing a combo and a button
(where the click action on the button raised a click event to VB which
the displayed the second form). It has also occurred using a minimal
control created via the wizard (as opposed to my more extended
controls).

There is a related "focus bouncing" problem in a VB app that displays
a form modally as well. However, this VB app uses arrays of controls
extensively, form variables, MDI parent/child relationships. So I
don't know if the two instances of the same problem are related?

(PS. I have found this group to be very useful over the past few
months for answers to ATL questions. Most questions that I have had
I've been able to find somewhere in the archives of this group. So,
well done.)



Sun, 27 Feb 2005 15:21:34 GMT  
 ATL ActiveX control loses focus when VB form displayed
As a follow up to my earlier message I have done some further
investigation that simplifies the situation.

Using a simple ATL-wizard button-based control the problem of VB focus
jumping occurring on display of a second form (done on the button's
exposed click event) ONLY occurs if the control is on a form that is
an MDI child and the second form is displayed modelessly. If neither
of these criteria occurs focus remains on the ATL control as the
second form is displayed, and when that form is closed and the
original form regains focus.

I hope someone can offer some insight.

Further implementation details follow:

The control is windowed and based on a WTL CButton object in a
contained window:

    CContainedWindowT<WTL::CButton> m_ctlButton;

All it does is handle the BN_CLICKED message and fires a click event
(code generated by the wizard and located in SimpleButtonCP.h as per
usual) :

    BEGIN_MSG_MAP(CSmplButton)
        MESSAGE_HANDLER(WM_CREATE, OnCreate)
        MESSAGE_HANDLER(WM_SETFOCUS, OnSetFocus)
        COMMAND_CODE_HANDLER(BN_CLICKED, OnButtonClick)
        CHAIN_MSG_MAP(CComControl<CSmplButton>)
    ALT_MSG_MAP(1)
        // Replace this with message map entries for
        // superclassed Button
    END_MSG_MAP()

In the OnCreate handler the control is created as follows:

    HWND hWnd = m_ctlButton.Create(
                   this, 1, m_hWnd, &rc, NULL,
                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON);
    m_ctlButton.SetWindowText(_T("Caption"));

The OnSetFocus handler was wizard-generated and unchanged by me:

    LRESULT OnSetFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&
bHandled)
    {
        LRESULT lRes = CComControl<CSmplButton>::OnSetFocus(
                           uMsg, wParam, lParam, bHandled);
        if (m_bInPlaceActive)
        {
            DoVerbUIActivate(&m_rcPos,  NULL);
            if(!IsChild(::GetFocus()))
                m_ctlButton.SetFocus();
        }
        return lRes;
    }

I have added the following routine to handle default highlighting of
the button:

    STDMETHOD(OnAmbientPropertyChange)(DISPID dispid)
    {
        if (dispid == DISPID_AMBIENT_DISPLAYASDEFAULT)
        {
            BOOL DisplayAsDefault;
            GetAmbientDisplayAsDefault(DisplayAsDefault);
            UINT Style = m_ctlButton.GetButtonStyle();

            if (DisplayAsDefault)
                Style |= BS_DEFPUSHBUTTON;
            else
                Style &= ~BS_DEFPUSHBUTTON;

            m_ctlButton.SetButtonStyle(Style, true);

        } // if

        return S_OK;
    }

Quote:

> Here's my problem:

> I have a number of ATL activex controls that are windowed and built
> using Win GDI controls (edit boxes, combo boxes, buttons, etc.). When
> I place them on a VB form and display a second VB form from within one
> of the control's events that have been exposed to VB (ie., Change or
> ButtonClick) I find that when the second form is displayed modelessly
> focus bounces onto the next control in the tab order (on the original
> form) just as the second form is displayed.

> I can then use the second form as per usual, but on closing it, the
> focus stays with the next control (the one focus "bounced" to earlier)
> on the original form, not the ATL control that had focus when the
> second VB form was displayed.

> This does not occur if the second form is displayed as a modal form
> (using Form.Show vbModal).

> The problem also doesn't occur if I use an MFC activex control. I have
> witnessed the problem across a series of ATL controls, some containing
> just an edit box (where I used a WM_CHAR-inspired Change event to
> display the second form), others containing a combo and a button
> (where the click action on the button raised a click event to VB which
> the displayed the second form). It has also occurred using a minimal
> control created via the wizard (as opposed to my more extended
> controls).

> There is a related "focus bouncing" problem in a VB app that displays
> a form modally as well. However, this VB app uses arrays of controls
> extensively, form variables, MDI parent/child relationships. So I
> don't know if the two instances of the same problem are related?

> (PS. I have found this group to be very useful over the past few
> months for answers to ATL questions. Most questions that I have had
> I've been able to find somewhere in the archives of this group. So,
> well done.)



Mon, 28 Feb 2005 12:41:08 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. ATL ActiveX control loses focus when VB form displayed - Once more....

2. ATL ActiveX control loses focus when VB form displayed - Again....

3. ATL Control Doesn't Lose Focus?

4. ActiveX control losing focus

5. ActiveX Control Stops Reponding When Loses Focus

6. Default button in a VB Form with an ActiveX edit control

7. Problem painting icon for ActiveX control in VB form

8. ATL Control and VB Form.FormPrint method

9. CListCtrl: The List Control de-highlights when the control loses focus

10. Help: MDI ActiveX Document (Excel) does not hide its toolbars when it loses focus

11. preventing an ActiveX from losing focus

12. Help (Again): MDI ActiveX Document (Excel) does not hide its toolbars when it loses focus

 

 
Powered by phpBB® Forum Software