Professional User Interface Suite, Copyright FOSS Software Inc. Help Published with Permission.

This code demonstrates how to handle the WM_LBUTTONUP event and WM_CONTEXTMENU message. It changes the cell focus but not the selection.

// Method Headers
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(CYourGridWnd)
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
//}}AFX_MSG
// Method Implementation
BEGIN_MESSAGE_MAP(CYourGridWnd,CExtGridWnd)
//{{AFX_MSG_MAP(CYourGridWnd)
ON_WM_CONTEXTMENU()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

...
...
...

void CYourGridWnd::OnShowContextMenu(
LONG nColNo,
LONG nRowNo,
CPoint ptTrack
)
{
nColNo;
nRowNo;
ptTrack;
// show your context menu here
}

bool CYourGridWnd::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 CYourGridWnd::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
);
}
}
}