#include "CMyListBox.h"

IMPLEMENT_DYNAMIC(CMyListBox, CListBox)

BEGIN_MESSAGE_MAP(CMyListBox, CListBox)
	ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()

void CMyListBox::OnRButtonDown(UINT nFlags, CPoint point)
{
	CPoint pt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);
	CRect rc;

	int nCount = GetCount();
	for (int i = 0; i < nCount; i++)
	{
		GetItemRect(i, &rc);
		if (rc.PtInRect(pt))
		{
			// 将当前右击选项选中
			SetCurSel(i);
			if (MyOnRButtonDown)
			{
				CString str;
				GetText(i, str);
				MyOnRButtonDown(str, GetParent());
			}
			// 加载弹出菜单
			CMenu m, *pm;
			m.LoadMenu(selectedMenu);
			pm = m.GetSubMenu(0);
			for (auto& p : disableMenuList)
				pm->EnableMenuItem(p, MF_DISABLED);
			ClientToScreen(&pt);
			pm->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
				pt.x, pt.y, GetParent());
			goto CMyListBox_OnRButtonDown;
		}
	}
	if(notSelectedMenu)// 加载弹出菜单
	{
		CMenu m, * pm;
		m.LoadMenu(notSelectedMenu);
		pm = m.GetSubMenu(0);
		for (auto& p : disableMenuList)
			pm->EnableMenuItem(p, MF_DISABLED);
		ClientToScreen(&pt);
		pm->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
			pt.x, pt.y, GetParent());
	}
CMyListBox_OnRButtonDown:
	CListBox::OnRButtonDown(nFlags, point);
}

void CMyListBox::SetMenu(int selectedMenu, int notSelectedMenu)
{
	this->selectedMenu = selectedMenu;
	this->notSelectedMenu = notSelectedMenu;
}

void CMyListBox::SetDisableMenu(std::vector<UINT>* pMenuList)
{
	this->disableMenuList = *pMenuList;
}

void CMyListBox::AddDisableMenu(UINT menu)
{
	disableMenuList.push_back(menu);
}

void CMyListBox::ClearDisableMenu()
{
	disableMenuList.clear();
}

