How to change color or remove pop menu border in MFC

Preetish Amballi 1 Reputation point
2021-12-02T16:40:57.69+00:00

I have a custom class inherited from CMenu for the owner draw. I have changed the background color of the pop up menu but i cant change the border color. Please help either to remove the border altogether or change border color. Refer to following image.

void CNewMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 100;
lpMeasureItemStruct->itemWidth = 150;
}

void CNewMenu::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect rectFull(lpDrawItemStruct->rcItem);
COLORREF TextRect = COLORREF(RGB(0, 0, 0));
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
pDC->FillSolidRect(&rectFull, TextRect);
pDC->TextOut(rectFull.left, rectFull.top, L"Pop up menu");
}

154526-popup-menu.png

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,657 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,765 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Junjie Zhu - MSFT 19,051 Reputation points Microsoft Vendor
    2021-12-03T05:27:20.883+00:00

    Hello,
    Welcome to Microsoft Q&A!

    pic

    I offer another approach, I recommend using self-painted controls to avoid borders, and you can refer to this code.

    void CNewMenu::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)  
    {  
      
      CString strText;     
      CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC); //Gets the device handle to the menu item     
      StruItemInfo *pStruItemInfo =(StruItemInfo*)lpDrawItemStruct->itemData;    
      if(pStruItemInfo == NULL)  
      {  
      return;  
      }  
       
      CRect rect(lpDrawItemStruct->rcItem);    
       
      if(pStruItemInfo->eItemType == ITEMTYPE_SEPARATOR) //Separator    
      {    
     // Draw background  
      pDC->FillSolidRect(rect, m_clrBk);    
      
     // Draw background  
      CRect rcSeparator(rect);    
      rcSeparator.top = rcSeparator.top + rcSeparator.Height() / 2;    
      rcSeparator.bottom = rcSeparator.top + 1;   
     rcSeparator.left += 5;  
      rcSeparator.right -= 5;    
      pDC->Draw3dRect(rcSeparator, m_clrSeparator, m_clrSeparator);  
       
      }  
     else  
     {  
     // Draw background  
     if(lpDrawItemStruct->itemState & ODS_GRAYED) // Invalid turn to black  
     {    
     pDC->FillSolidRect(rect, m_clrBk);    
     pDC->SetTextColor(m_clrTextDisable);    
     }        
     else if(lpDrawItemStruct->itemState & ODS_SELECTED )  
     {     
     //Paint the background color of the rectangular box on the menu item    
     pDC->FillSolidRect(rect, m_clrBkHover);    
     //Set menu text color    
     pDC->SetTextColor(m_clrText);    
     }     
     else    
     {    
     pDC->FillSolidRect(rect, m_clrBk);    
     pDC->SetTextColor(m_clrText);    
     }  
      
     pDC->SetBkMode(TRANSPARENT);    
      
     CRect rcIcon(rect);  
     rcIcon.left += (m_nLeftWidth - m_nIconWidth) / 2;  
     rcIcon.right = rcIcon.left + m_nIconWidth;  
     rcIcon.top += (rcIcon.Height() - m_nIconHeight) / 2;  
      
     if(pStruItemInfo->hIcon != NULL)    
     {    
     DrawIconEx(pDC->m_hDC, rcIcon.left, rcIcon.top, pStruItemInfo->hIcon, m_nIconWidth, m_nIconHeight, 0, NULL, DI_NORMAL);    
     }     
      
     // Text font and size Settings  
     LOGFONT fontInfo;    
     pDC->GetCurrentFont()->GetLogFont(&fontInfo);    
      
     fontInfo.lfHeight = m_nFontSize;  
     lstrcpy(fontInfo.lfFaceName, m_strFontFaceName.c_str());   
     CFont fontCh;    
     fontCh.CreateFontIndirect(&fontInfo);    
     pDC->SelectObject(&fontCh);    
      
     CRect rcText(rect);  
     if (m_bHasIcon)  
     {  
     rcText.left += m_nLeftWidth;  
     }  
     else  
     {  
     rcText.left += 10;  
     }  
     rcText.top += (rcText.Height() - m_nFontSize) / 2;  
     rcText.bottom = rcText.top + m_nFontSize;  
      
     //Sub menu item  
     if(pStruItemInfo->eItemType == ITEMTYPE_SUBMENU)  
     {    
     pDC->TextOut(rcText.left, rcText.top, pStruItemInfo->strText, pStruItemInfo->strText.GetLength());             
     }    
     else if (pStruItemInfo->eItemType == ITEMTYPE_STRING)  
     {    
     // String items  
     pDC->TextOut(rcText.left, rcText.top, pStruItemInfo->strText, pStruItemInfo->strText.GetLength());    
     pDC->TextOut(rcText.left, rcText.top, pStruItemInfo->strShortcut, pStruItemInfo->strShortcut.GetLength());    
     }      
      
     }  
    }  
      
    void CNewMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)  
    {  
     StruItemInfo *pStruItemInfo =(StruItemInfo*)lpMeasureItemStruct->itemData;    
     if (pStruItemInfo->eItemType == ITEMTYPE_SEPARATOR)  
     {  
     // Sets the height of the Separator   
     lpMeasureItemStruct->itemHeight = m_nSeparatorHeight;  
     }  
     else  
     {  
     lpMeasureItemStruct->itemHeight = m_nItemHeight;  
     }  
     // Setting item width  
     lpMeasureItemStruct->itemWidth = m_nMenuWidth;  
    }  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Castorix31 86,041 Reputation points
    2021-12-02T19:44:16.13+00:00

    The border is the WS_EX_DLGMODALFRAME extended style of the Menu window
    So you can remove it with SetWindowLong and GWL_EXSTYLE from the Menu window handle, that you can get in WM_INITMENUPOPUP with :

    HWND hWndPopupMenu = FindWindow(TEXT("#32768"), NULL);  
    

    But the size of the menu items will change (rcItem) and you must increase it

    A test :

    154567-od-menu.gif

    0 comments No comments

  3. SM 416 Reputation points
    2021-12-02T21:02:59.4+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.