Hello,
Welcome to Microsoft Q&A!
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.