PRB: Pop-Up Menu Items Disabled for an OLE Control

Last reviewed: July 24, 1997
Article ID: Q141199
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Edition, version 4.0, 4.1, 4.2, 5.0

SYMPTOMS

The menu items are disabled even though menu handlers were added with Class Wizard when you create an OLE Control and then use the Pop-up Menu component in the Component Gallery to add a context menu to the COleControl derived class. The problem occurs after you modify the context menu, and then add handlers for the menu items, build the Control, place it in Tstcon32.exe, and bring up the context menu.

CAUSE

Within the OnContextMenu function added by the Pop-up Menu Component, the code makes the top-most parent window the parent of the context menu. The menu sends the WM_COMMAND message to its parent first, which can then route the command appropriately through the application. This method works for windows in an MFC application that are part of the command-routing scheme. However, because OLE Controls are not part of the routing mechanism, the WM_COMMAND message never gets back to them and therefore the context menu doesn't see the handlers, so the menu items are disabled.

RESOLUTION

When you add a context menu to an OLE control, you need to make the COleControl derived class the parent of the context menu. The Pop-up Menu Component creates the following code within the CWnd derived class. If the pop-up menu is being added to a COleControl-derived class, you need to delete the following lines of code:

   while (pWndPopupOwner->GetStyle() & WS_CHILD)
      pWndPopupOwner = pWndPopupOwner->GetParent();

Code Created by Pop-up Menu Component

   void CSPLITCtrl::OnContextMenu(CWnd*, CPoint point)
   {
      // CG: This function was added by the Pop-up Menu component

      CMenu menu;
      VERIFY(menu.LoadMenu(CG_IDR_POPUP_SPLITCTRL));

      CMenu* pPopup = menu.GetSubMenu(0);
      ASSERT(pPopup != NULL);

      CWnd* pWndPopupOwner = this;
      while (pWndPopupOwner->GetStyle() & WS_CHILD)
         pWndPopupOwner = pWndPopupOwner->GetParent();

      pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x,
                             point.y, pWndPopupOwner);
   }

By removing the two lines of code, you are leaving the COleControl-derived class as the parent of the menu. As a result, the control receives the WM_COMMAND messages sent by the pop-up menu, so the normal ON_COMMAND functionality added by Class Wizard worked as designed.


Keywords : CodeGen kbprg MfcOLE
Technology : kbMfc kbole
Version : 4.0 4.1 4.2 5.0
Platform : NT WINDOWS
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: July 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.