PDA

View Full Version : Right Clicking Toolbars



jdingler
01-16-2009, 01:17 PM
In the course of upgrading one of our applications, I needed to be able to find the ID of a button on a toolbar, when the user right clicks it. With Stingray's dynamic toolbars, conventional MFC techniques didn't work, as at runtime, there can be any number of dynamic toolbars.

So after delving into the samples and source code a bit, I cobbled together, this solution.

You'll first need to create an event handler for the OnContextMenu message. Then fill in the included code Snippets.

Note:
You'll need to make a source cod change to the StingRay Library to compile this. Without the change, you'll see an error like this.
c:\TestProject\mainfrm.cpp(850) : error C2248: 'SECCustomToolBar::ItemFromPoint' : cannot access protected member declared in class 'SECCustomToolBar'

All you need to do, is change the function scope for 'SECCustomToolBar::ItemFromPoint' from protected to public.

Disclaimer: Now this seems to work fine for me. I make no claims that it will work for you. Use at your own risk.


void CMainFrame::DoContextMenu(CWnd* pWnd, CPoint point, int ControlId)
{
// ToDo: Fill in code to respond to the right click for controls.
}

void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point)
{
SECToolBarManager* pMgr = (SECToolBarManager*)m_pControlBarManager;

if (!pCurrentDocument)
{
DoContextMenu(pWnd, point, -1);
return;
}

SECCustomToolBar * pSECCustomToolBar = FindSECCustomToolBar(point);
if (!pSECCustomToolBar)
{
DoContextMenu(pWnd, point, -1);
return;
}

int ControlId = FindControlID(pSECCustomToolBar, point);
if (ControlId < 0)
{
DoContextMenu(pWnd, point, -1);
return;
}

DoContextMenu(pWnd, point, ControlId);
}


int CMainFrame::FindControlID(const SECCustomToolBar * pSECCustomToolBar, const CPoint & ScreenPoint)
{
CPoint ClientPoint = ScreenPoint;
pSECCustomToolBar->ScreenToClient(&ClientPoint);

CRect ButtonRect;
int ButtonIndex = pSECCustomToolBar->ItemFromPoint(ClientPoint, ButtonRect);
if (ButtonIndex < 0)
return -1;

SECStdBtn * pStdBtn = pSECCustomToolBar->GetBtnObject(ButtonIndex);
if (!pStdBtn)
return -1;

return pStdBtn->m_nID;
}

SECCustomToolBar * CMainFrame::FindSECCustomToolBar(const CPoint & ScreenPoint)
{
CPoint ClientPoint = ScreenPoint;
ScreenToClient(&ClientPoint);

SECDockBar * pDockBar = NULL;

CRect BarRect;

for (int i = 0; i < 4; i++)
{
pDockBar = (SECDockBar*)GetControlBar(dwSECDockBarMap[i][0]);
ASSERT(pDockBar != NULL);

pDockBar->GetWindowRect(BarRect);

if (!BarRect.PtInRect(ScreenPoint))
continue;

int NumberOfBars = pDockBar->m_arrBars.GetCount();
for (int Index = 0; Index < NumberOfBars; Index++)
{
SECCustomToolBar * pSECCustomToolBar = (SECCustomToolBar *) pDockBar->m_arrBars[Index];
if (!pSECCustomToolBar)
continue;

if (!pSECCustomToolBar->IsVisible())
continue;

pSECCustomToolBar->GetWindowRect(BarRect);

if (!BarRect.PtInRect(ScreenPoint))
continue;

return pSECCustomToolBar;
}
}

return NULL;
}