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

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds