for the window skined with no-regular region, the menu assigned to window is difficult to be placed on it's normal location, so it does not work. I will introduce to you a way that use popup-menu to simulate menu assigned to window.
first, draw a menu’ effect in any region on the window. the region we call it menu-region, we will save every menu-item's rect.
CRect m_rcMenu[3];
we handle tree three messages mostly: WM_LBUTTONDOWN, WM_MOUSEMOVE, WM_ENTERIDLE.
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_LBUTTONDOWN( )
ON_WM_MOUSEMOVE()
ON_WM_ENTERIDLE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
when the mouse left button is pushed in the menu-region, we start to use popup-menu to simulate menu assigned to window.
void CMyDlg::OnLButtonDown(UINT nFlags, CPoint ptStart)
{
for (i=0 ;i<3; i++)
{
if (PtInRect(m_rcMenu[i], point))
{
if (!PtInRect(m_rcMenu[m_iMenuIndex], point))
{
InvalidateRect(&m_rcMenu[m_iMenuIndex], TRUE);
UpdateWindow();
}
//this is the popup menu index be shown.
m_iMenuIndex = i;
m_bShowMenu = TRUE;
show the menu, if m_bMenuContinue is TRUE, show new popup menu repeated, and older is cleared on WM_ENTERIDLE, because when menu assigned to window is shown and the mouse move in menu-region(no left button down), the menu item changes.
do
{
//draw the effect that menu is pushed.
CDC* pDC = GetDC();
pDC->Draw3dRect(&m_rcMenu[m_iMenuIndex], RGB(100, 100, 100), RGB(160, 160, 160));
ReleaseDC(pDC);
m_bMenuContinue = FALSE;
ShowMenu();
} while(m_bMenuContinue);
InvalidateRect(&m_rcMenu[m_iMenuIndex], TRUE);
UpdateWindow();
m_bShowMenu = FALSE;
CWnd::OnLButtonDown(nFlags, point);
return;
}
}
if ( i== 3)
m_iMenuIndex = -1;
CDialog::OnLButtonDown(nFlags, point);
}
then change the menu index in OnMouseMove
void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//undo the effect that menu be pushed.
if (m_iMenuIndex != -1)
{
if (!PtInRect(m_rcMenu[m_iMenuIndex], point))
InvalidateRect(&m_rcMenu[m_iMenuIndex], TRUE);
}
UpdateWindow();
m_iMenuIndex = -1;
for (i=0 ;i<3; i++)
{
if (PtInRect(m_rcMenu[i], point))
{
//change the index
m_iMenuIndex = i;
//draw the effect that mouse stop on it without left-button down.
CDC* pDC = GetDC();
pDC->Draw3dRect(&m_rcMenu[i], RGB(160, 160, 160), RGB(100, 100, 100));
ReleaseDC(pDC);
break;
}
}
CDialog::OnMouseMove(nFlags, point);
}
cancel the popup menu in OnEnterIdle and re-show it in OnLButtonDown's do-while loop.
void CMyDlg::OnEnterIdle(UINT nWhy, CWnd* pWho)
{
CWnd::OnEnterIdle(nWhy, pWho);
// TODO: Add your message handler code here
if (nWhy == MSGF_MENU)
{
if (!m_bShowMenu)
return;
CPoint point;
GetCursorPos(&point);
ScreenToClient(&point);
for (int i=0 ;i<3; i++)
{
if (PtInRect(m_rcMenu[i], point))
{
if (m_iMenuIndex != i)
{
if (m_iMenuIndex != -1)
{
if (!PtInRect(m_rcMenu[m_iMenuIndex], point))
{
InvalidateRect(&m_rcMenu[m_iMenuIndex], TRUE);
UpdateWindow();
}
}
m_iMenuIndex = i;
m_bMenuContinue = TRUE;
SendMessage(WM_CANCELMODE);
}
break;
}
}
}
}
last, this is the code for show menu.
void CMyDlg::ShowMenu()
{
if (m_PopupMenu.GetSafeHmenu())
m_PopupMenu.DestroyMenu();
m_PopupMenu.LoadMenu(IDR_POPUP);
CMenu* pSub = m_PopupMenu.GetSubMenu(m_iMenuIndex);
CPoint pt(m_rcMenu[m_iMenuIndex].left, m_rcMenu[m_iMenuIndex].bottom+2);
ClientToScreen(&pt);
TrackPopupMenu(pSub->GetSafeHmenu(), TPM_LEFTALIGN |
TPM_LEFTBUTTON, pt.x, pt.y, NULL, m_hWnd, NULL);
}
so we can use popup menu to simulate menu assigned to window with this code. you can place your menu on any location in the window skined with no-regular region.