返回首页

我有一个SDI MFC探险风格的应用程序。它有一个左侧的树视图和空窗格与分隔栏的右侧。我试图得到一个弹出菜单,工作时,用户点击一个项目,在树视图中。

我可以成功地检测到,当用户点击一个树视图项,显示弹出菜单,并没有灰色。这是我的代码:

<pre>void CLeftView::OnRClick(NMHDR* pNMHDR, LRESULT* pResult)

{

	try

	{

		//get the position of the cursor when the last message was retrieved using GetMessage

		CPoint point(GetMessagePos());

		//jl

		int popupMenuXPos = point.x;

		int popupMenuYPos = point.y;

		//converts the screen coords of a point on the display to client coords which are relative

		//to the upper left hand corner of the CWnd client area

		ScreenToClient(&point);

		   

		CTreeCtrl& tree = GetTreeCtrl();

	   

		//tests 'point' to see if there is a tree-ctrl item there. If not, it returns null. If there is, 

		//it returns a handle to it

		HTREEITEM hti = tree.HitTest(point);

		if(hti)

		{

			//selects the item

			tree.SelectItem(hti);

			//test - to see what is at that position

			CString strTreeItem = tree.GetItemText(hti);

		  

			//get at the data held with the tree-view item

			WorkspaceItemData* pWspItemData = new WorkspaceItemData;

			pWspItemData = (WorkspaceItemData*)(tree.GetItemData(hti));

					

			//get the tree item's path

			CString m_selectedTreeItemFilePath = pWspItemData->m_path;

			// load popup menu

			HMENU menu = ::LoadMenu(::GetModuleHandle(0), MAKEINTRESOURCE(IDR_POPUP_TREEVIEW/*IDR_MENU1*/));

			HMENU subMenu = ::GetSubMenu(menu, 0);

			int command = ::TrackPopupMenu(subMenu,

				TPM_RETURNCMD | TPM_LEFTBUTTON,

				popupMenuXPos,

				popupMenuYPos,

				0,

				AfxGetMainWnd()->GetSafeHwnd()/*parentHandle*/, 0);

 

		}

	}

	catch( ... )

	{

		AfxMessageBox(_T("CLeftView::OnRClick"));

	}

  

}

我在资源编辑器创建弹出菜单,打开"菜单项的ID = ID_OPEN_OPENFILE。在LeftView.cpp {C}
void CLeftView::OnOpenOpenfile()

{

	// TODO: Add your command handler code here

	int x=2;

}

然而,当我运行的应用程序,右键单击树视图项,在弹出的菜单中出现,是不是灰色的,所以它似乎知道它有一个消息处理程序(因为它是灰色之前,我加入了OnOpenOpenfile方法),但我离开的时候,短声的"开放式"的菜单项的事件处理程序不叫(我有一个突破点,所以我知道)。

有人告诉我,我错过了什么。

回答

评论会员:b 时间:2