Building Translucent Menus

Environment: C++

Object

This article will show you how to build a translucent menu, as shown in the example. The two interessing files are CImageTarget.h and CImageTarget.cpp, which provide the functionalities of the translucent menu.

Step 1

First, you have to build a Document View application. Then, insert #include “CImageTarget.h” into the header of the CView-derived class. Then, don’t forget to include in your settings the msimg32.lib library; it will be necessary for the AlphaBlend(…) function.

You have also to declare as member variables one or more “CImageTarget *” variables inside the “protected” section. For the example above, I’ve two variables:

CImageTarget * m_itMenuAffichage , * m_itMenuCentral ;

Step 2

Now, you must construct and create your instance of the CImageTarget class. I do it in the “CXXXView::OnInitialUpdate” function:

CRect rect ;
AfxGetMainWnd()->GetClientRect( &rect );
int bottom = rect.bottom ;

m_itMenuAffichage = new CImageTarget( );

//first, create the master item.
bool res = m_itMenuAffichage->Create( this , CRect( 200 ,
                                      bottom - 150 , 300 ,
                                      bottom - 50 ) ,
                                      std::string("affichage.bmp") ,
                                      true );

//if the master item has been correctly created , you can have some
//subItems

if( res )
{
  int item = -1;

  item = m_itMenuAffichage->AddItem( CRect( 200 , bottom - 510 ,
                                     300 , bottom - 410 ) ,
                                     std::string("symbole.bmp") ,
                                     false );

/* if the subordinate item has been correctly added, you can
 * associate to it a message and the ID of a classic item menu.
 * This will have the effect of sending a WM_LBUTTONDOWN message
 * to the view that contains the corresponding function. */

  if( item != -1 ) res = m_itMenuAffichage->AddToMap( item ,
                                            WM_LBUTTONDOWN ,
                                            ID_SYMBOLE );

  ...
}

/* Note that you can never handle the sub-items by their address*/

Step 3

/*Call the master items CImageTarget::MouseMove method into the
 *CXXXView::OnMouseMove(UINT nFlags, CPoint point)*/

void CProjet1View::OnMouseMove(UINT nFlags, CPoint point)
{

  m_itMenuAffichage->MouseMove( point ) ;
  m_itMenuCentral->MouseMove( point ) ;
  .
}

/*Call the master items CImageTarget:: LButtonDown method into the
 *CXXXView:: OnLButtonDown (UINT nFlags, CPoint point)*/

void CProjet1View::OnLButtonDown(UINT nFlags, CPoint point)
{
  if(  m_itMenuAffichage->LButtonDown( point ) ) return ;
  /*you immediately return if you have clicked an item*/
  .
}

/*Call the master items CImageTarget::Draw method into the
 *CXXXView:: OnDraw ( CDC * pDC )*/

void CProjet1View::OnDraw(CDC* pDC)
{
  m_itMenuAffichage->Draw(  pDC  );
  ...
}

/*Note that this way will cause a little flicking. To avoid it,
 * you better work on a memory DC.*/

Remarks

  1. The master item controls all the sub-items (as you will see in the code). It controls the drawing, the repainting, and the events.
  2. I did not try to insert a master item as a sub-item of another master item, but it’s simple to do. Just make the AddItem function return the address instead of the ID and re-do an AddItem on it.

Downloads


Download demo project with sources – 134 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read