In some of our projects we used the code below for handling context menus. This code handles the mouse up event and WM_CONTEXTMENU
message. It changes the cell focus but not selection.
Header:
virtual void OnShowContextMenu(
LONG nColNo,
LONG nRowNo,
CPoint ptTrack
);
virtual bool OnGbwAnalyzeCellMouseClickEvent(
UINT nChar, // VK_LBUTTON, VK_RBUTTON or VK_MBUTTON only
UINT nRepCnt, // 0 - button up, 1 - single click, 2 - double click, 3 - post single click & begin editing
UINT nFlags, // mouse event flags
CPoint point // mouse pointer in client coordinates
);
...
...
...
//{{AFX_MSG(CBaseGridWnd)
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
//}}AFX_MSG
Source:
BEGIN_MESSAGE_MAP(CBaseGridWnd,CExtGridWnd)
//{{AFX_MSG_MAP(CBaseGridWnd)
ON_WM_CONTEXTMENU()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
...
...
...
void CBaseGridWnd::OnShowContextMenu(
LONG nColNo,
LONG nRowNo,
CPoint ptTrack
)
{
nColNo;
nRowNo;
ptTrack;
// show you context menu here
}
bool CBaseGridWnd::OnGbwAnalyzeCellMouseClickEvent(
UINT nChar,
UINT nRepCnt,
UINT nFlags,
CPoint point
)
{
ASSERT_VALID( this );
ASSERT( 0 <= nRepCnt && nRepCnt <= 3 );
if( CExtGridWnd::OnGbwAnalyzeCellMouseClickEvent(
nChar,
nRepCnt,
nFlags,
point
)
)
return true;
if( nChar == VK_RBUTTON
&& nRepCnt == 0 // 0 - button up
)
{
CExtGridHitTestInfo htInfo( point );
HitTest( htInfo, false, true );
if( (!htInfo.IsHoverEmpty()) && htInfo.IsValidRect()
&& htInfo.m_nRowNo >= 0
&& (htInfo.m_dwAreaFlags&__EGBWA_INNER_CELLS) != 0
)
{
CPoint ptFocus( htInfo.m_nColNo, htInfo.m_nRowNo );
SetFocus();
FocusSet( ptFocus, true, true, false );
CPoint ptTrack( point );
ClientToScreen( &ptTrack );
OnShowContextMenu(
htInfo.m_nColNo,
htInfo.m_nRowNo,
ptTrack
);
}
return true;
}
return false;
}
void CBaseGridWnd::OnContextMenu( CWnd * pWnd, CPoint point )
{
LONG nRowNo = SelectionGetLastRowInColumn( 0L );
if( nRowNo < 0 )
nRowNo = FocusGet().y;
if( nRowNo >= 0 )
{
CRect rcCell;
if( GridCellRectsGet(
0L,
nRowNo,
0,
0,
&rcCell
)
)
{
CPoint ptTrack( rcCell.left, rcCell.bottom );
ClientToScreen( &ptTrack );
OnShowContextMenu(
0L,
nRowNo,
ptTrack
);
}
}
}