Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I override my PreTranslateMessage function in my dialog, but when i right click the CTreeCtrl control the first time, the Menu loaded as i expected, but when i right click the second time, the Menu disappeared. and the third time the menu loaded again, and fourth it disappeared.
i want to understand the mechmanism of the message routing.
the problem code is here.
C++
if(WM_RBUTTONDOWN==pMsg->message)
	{	
		DWORD dwPos = GetMessagePos();
		CPoint point(LOWORD(dwPos), HIWORD(dwPos));

		CMenu menu;
		VERIFY(menu.LoadMenu(IDR_RCLKMENU));
		CMenu *popup=menu.GetSubMenu(0);
		ASSERT(popup!=NULL);
		popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
	}
	return CDialogEx::PreTranslateMessage(pMsg);

When i add one line code the problem disappear and works as i expceted.
C++
    if(WM_RBUTTONDOWN==pMsg->message)
{
    DWORD dwPos = GetMessagePos();
    CPoint point(LOWORD(dwPos), HIWORD(dwPos));

    CMenu menu;
    VERIFY(menu.LoadMenu(IDR_RCLKMENU));
    CMenu *popup=menu.GetSubMenu(0);
    ASSERT(popup!=NULL);
    popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
            return true; // this line make all message routing normal.
}
return CDialogEx::PreTranslateMessage(pMsg);


Thank you all.
Posted

1 solution

If you return TRUE from PreTranslateMessage() the message is marked as handled and no further handling is executed. This is the behaviour you probably want here. Because you have handled the message.

Windows provides the message WM_CONTEXTMENU when right-clicking. So a better solution would be to use this by adding a handler for this message to your CTreeCtrl derived class. To do this, use the properties window for the tree control class. It will create a ON_WM_CONTEXTMENU() entry and the function body OnContextMenu().
 
Share this answer
 
Comments
maturn 18-Jul-12 23:50pm    
Thank U.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900