Right mouse button click in CTreeView?
You might try inserting the on context menu component and using it.
However, I also have an SDI CTreeView splitter windows and here is what i
did to get solve what sounds like a similar problem.
You may want to trap the PreTranslateMessage to check for the right click
message.
If you aren't getting it in this override, check your message map macros.
Try this for right mouse clicks in the CTreeView to display a context menu
as a test:
HEADER:
protected:
afx_msg void OnContextMenu(CWnd*, CPoint point);
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnRclick(NMHDR* pNMHDR, LRESULT* pResult);
CODE:
BEGIN_MESSAGE_MAP(CDbTreeView, CTreeView)
//{{AFX_MSG_MAP(CDbTreeView)
ON_WM_CONTEXTMENU()
ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CDbTreeView::PreTranslateMessage(MSG* pMsg)
{
{
// Shift+F10: show pop-up menu.
if ((((pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN) &&
// If we hit a key and
(pMsg->wParam == VK_F10) && (GetKeyState(VK_SHIFT) & ~1)) != 0) || //
it's Shift+F10 OR
(pMsg->message == WM_CONTEXTMENU)) // A Right Click
{
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
CPoint point = rect.TopLeft();
point.Offset(5, 5);
OnContextMenu(NULL, point);
return TRUE;
}
}
return CTreeView::PreTranslateMessage(pMsg);
Quote:
}
void CDbTreeView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM hRClickSel = NULL;
UINT htFlags = TVHT_ONITEM;
CPoint htPoint;
// get mouse point
GetCursorPos(&htPoint);
ScreenToClient(&htPoint);
// is there a tree item there?
hRClickSel = GetTreeCtrl().HitTest(htPoint, &htFlags);
if(hRClickSel != NULL)
{ // yes a tree item is present
// select it
GetTreeCtrl().SelectItem( hRClickSel );
// pop up a context menu
ClientToScreen(&htPoint);
htPoint.Offset(5, 5);
OnContextMenu(NULL, htPoint);
}
*pResult = 0;
Quote:
}
void CDbTreeView::OnContextMenu(CWnd*, CPoint point)
{
UINT htFlags = TVHT_ONITEM;
HTREEITEM hRClickSel = GetTreeCtrl().GetSelectedItem();
CMenu menu;
VERIFY(menu.LoadMenu(CG_IDR_POPUP_DB_TREE_VIEW));
CMenu* pPopup = menu.GetSubMenu(IMG_GROUPS);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
Quote:
}
Quote:
> > I have splitter window and two CTreeView. In each of view I want to
> > cath right mouse button click. I tried to implement handler on
> > WM_RBUTTONDOWN but function is not called in my view although I see
> > message using Spy.