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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Companies must routinely transfer files and share data to run their business, work with partners, and speed operations. However, many find the traditional approach to file transfer lacks necessary security, is too complex and difficult to manage, does not support the levels of automation needed, and breaks down when addressing the file transfer requirements of new areas like Big Data analytics and mobile applications. This QuinStreet SmartSelect discusses how the changing business environment is making the use …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds