PRB: Focus and Tab Issues with ATL Subclassed Edit Control

Last reviewed: February 16, 1998
Article ID: Q179696
The information in this article applies to:
  • The Microsoft Active Template Library (ATL), versions 2.0, 2.1 included with:

        - Microsoft Visual C++, 32-bit Editions, version 5.0
    

SYMPTOMS

If you use the ATL Wizards to create an ActiveX control that subclasses the Edit common control, you may see one or both of the following problems depending on where you use the control:

  • You cannot use the TAB key to set focus to the ActiveX Edit control. You must use the mouse to set focus and activate the ActiveX Edit control.
  • Once the ActiveX Edit control has the focus, you will hear a beep if you try to use the TAB key to change the focus away from it.

CAUSE

When an ATL-based ActiveX control subclasses a Windows common control, it creates a contained window object to subclass the control. When you use the TAB key to move around an application, the ActiveX control window gets the focus but never shifts the focus to the Edit window within the ActiveX control. As a result, you must click the Edit control to activate it.

The second issue results from the way an ATL-based control and its ActiveX control container handle messages. Because of the way some control containers handle messages, they do not get the opportunity to process the TAB keystroke, even if the control itself does not process it.

RESOLUTION

To activate the ATL-based ActiveX Edit control with the TAB key, you can override the OnSetFocus method and set the focus to the contained Edit window before returning.

To use the TAB key to shift the focus away from the ATL-based control, you can override the TranslateAccelerator method. If the message is WM_KEYDOWN and the key is the TAB key, give the IOleControlSite TranslateAccelerator a chance to process the message first.

STATUS

Microsoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

To work around these issues, add the following code to your ATL-based control class:

Sample Code

   typedef enum tagKEYMODIFIERS
   {
      KEYMOD_NONE    = 0X00000000,
      KEYMOD_SHIFT   = 0x00000001,
      KEYMOD_CONTROL = 0x00000002,
      KEYMOD_ALT     = 0x00000004
   } KEYMODIFIERS;

   class ATL_NO_VTABLE Csc_ctrl2 :
      ...
   {
      ...

      LRESULT OnSetFocus(UINT   /* nMsg */,
                         WPARAM /* wParam */,
                         LPARAM /* lParam */,
                         BOOL&  /* bHandled */)
      {
         CComQIPtr <IOleControlSite, &IID_IOleControlSite>
            spSite(m_spClientSite);
         if (m_bInPlaceActive && spSite)
            spSite->OnFocus(TRUE);

         ::SetFocus(m_ctlEdit.m_hWnd);
         return 0;
      }

      STDMETHOD(TranslateAccelerator)(LPMSG lpmsg)
      {
      CComQIPtr<IOleControlSite,&IID_IOleControlSite>
            spCtrlSite(m_spClientSite);
      if (spCtrlSite)
      {
         if ((lpmsg->message == WM_KEYDOWN) &&
             (LOWORD(lpmsg->wParam) == VK_TAB))
         {
            DWORD keymods = 0;
            keymods += (GetKeyState(VK_SHIFT) < 0) ? KEYMOD_SHIFT
                                                   : KEYMOD_NONE;
            keymods +=
               (GetKeyState(VK_CONTROL) < 0) ? KEYMOD_CONTROL
                                             : KEYMOD_NONE;
            keymods += (GetKeyState(VK_MENU) < 0) ? KEYMOD_ALT
                                                  : KEYMOD_NONE;
            return spCtrlSite->TranslateAccelerator(lpmsg,keymods);
         }
      }
      return S_FALSE;
    }

   ...
   };

Steps to Reproduce Behavior

  1. Use the ATL Wizards to create an ActiveX control project that subclasses an Edit control.

  2. Build the control and place it in different containers with several other controls.

  3. Use the TAB key to move between the controls.

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q169434
   TITLE     : BETA-PRB: Tabbing Broken for ATL Controls in IE 4.0

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Shawn William Karr, Microsoft Corporation


Additional query words: 5.00 Visual Studio C++ Basic
Keywords : AtlControl kbcode
Version : win95:2.0,2.1;winnt:2.0,2.1
Platform : Win95 winnt
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 16, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.