MFC树视图弹出菜单将不起作用 [英] MFC Tree-view pop-up menu won't work

查看:68
本文介绍了MFC树视图弹出菜单将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SDI MFC资源管理器样式的应用程序.它的左侧为树形视图,右侧为带分隔条的空白窗格.我试图使弹出菜单在用户单击树状视图中的项目时起作用.

我可以成功检测到用户何时单击了树形视图项目,并且显示了弹出菜单,并且该菜单没有变灰.这是我的代码:

I have an SDI MFC explorer style application. It has a tree view on the left and an empty pane on the right with a splitter bar. I am trying to get a pop up menu to work when the user clicks on an item in the tree view.

I can successfully detect when the user has clicked on a tree-view item and the pop up menu is displayed and is not greyed out. This is my code:

<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



I created the popup menu in the resource editor and the open menu item has ID = ID_OPEN_OPENFILE. In LeftView.cpp

<pre>ON_COMMAND(ID_OPEN_OPENFILE, &CLeftView::OnOpenOpenfile)


void CLeftView::OnOpenOpenfile()
{
	// TODO: Add your command handler code here
	int x=2;
}



但是,当我运行该应用程序并右键单击一个树视图项目时,弹出菜单就会出现,并且不会变灰,因此似乎知道它具有消息处理程序(因为在我添加该消息处理程序之前它是变灰的OnOpenOpenfile方法),但是当我在打开"菜单项上左击时,未调用事件处理程序(我在其中有一个断点,所以我知道).

请有人告诉我我所缺少的内容.



However, when I run the application, and right-click on a tree-view item, the pop-up menu appears and is not greyed out so it seems to know it has a message handler (as it was greyed out before I added the OnOpenOpenfile method), but when I left-clik on the ''open'' menu item the event handler is not called (I have a break-point in it so i know).

Please could somebody tell me what I am missing.

推荐答案

尝试在没有TPM_RETURNCMD的情况下进行TrackPopupMenu调用.还提供CTreeView窗口的句柄.喜欢:

Try doing the TrackPopupMenu call without the TPM_RETURNCMD. Also provide the handle to your CTreeView window. Like:

::TrackPopupMenu(subMenu,
    TPM_LEFTBUTTON,
    popupMenuXPos,
    popupMenuYPos,
    0,
    GetSafeHwnd()/*parentHandle*/, 0);


这篇关于MFC树视图弹出菜单将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆