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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Not all enterprise applications are created equal. Sophisticated applications need developer support but other more basic apps do not. With the right tools, everyone is a potential app developer with ideas and a perspective to share. Trends such as low-code development and model driven development are fundamentally changing how and who creates applications. Is your organization ready? Read this report and learn: The seven personas of enterprise app delivery How application ownership is spreading to the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds