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


Comments

  • those french

    Posted by Legacy on 02/04/2004 12:00am

    Originally posted by: Yalcin

    just kidding.

    this might help if for some reason you have hard time compiling

    1.Add imageTarget.cpp to your project
    2.link to msimg32.lib
    3.top of imageTarget.cpp add #include <wingdi.h>

    looks interesting.

    Here is what I had done in the past and I like it better.

    Menu is either semitransparent or invisible. mouse comes over it appears in a "un-dim fashion" it lights up slowly. like the lights in my Benz. Than as you move away from them they dim out.

    However My linking to messages and maps were not elegant at all like it is in the example.

    Overall I think functionality is good beauty needs more work.

    Thanks for the code....

    Yalcin
    Proper Gypsy

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds