Displaying a Company Logo on the Menu Bar



Click here for a larger image.

Environment: VC6 SP5, NT4

I have searched the entire Web and tried every resource at my disposal to display a company logo on the Menu Bar, but all my efforts went in vain. Finally, I did R&D and came out with a new and simple way to display a company logo on the Menu Bar without subclassing CControlBar.

It is always possible to find out the coordinate of the Menu Bar by calculating the position of placing the company logo on the Menu Bar. But, calculating coordinates and drawing a bitmap at that position needs to called every time a view is maximized or restored. And, also particularly, when the main windows width is reduced, the company logo needs to be moved to the next line in the Menu Bar if it doesn't fit in the first line.

Following is the procedure to overcome the preceding problems.

  1. Create a menu item in the view's menu resource and mainframe's menu resource with a unique ID (let's say ID_HARIKRISHNA). Uncheck the Popup menu check box.
  2. Now, open the resource file (yourproject.rc file), go to the menu resource block, and add following flags for the above menu item.
    Ex. For the menuitem named "harikrishna"
    MENUITEM "harikrishna", ID_HARIKRISHNA, MFT_STRING | MFT_OWNERDRAW | MFT_RIGHTJUSTIFY,MFS_ENABLED
  3. Now, override the OnMeasureItem CMainfrm class.
  4. Add the following code in OnMeasureItem:
  5.   void CMainFrame::OnMeasureItem(int nIDCtl,
                       LPMEASUREITEMSTRUCT lpMeasureItemStruct)
      {
      if (ID_HARIKRISHNA == lpMeasureItemStruct->itemID
                         && ODT_MENU
                         == lpMeasureItemStruct->CtlType )
      {
        CBitmap bitmap;
        bitmap.LoadBitmap(IDB_BITMAP1);
    // IDB_BITMAP1 is the bitmap resource that you want to
    // display on the Menu Bar
        CSize size = bitmap.GetBitmapDimension();
        BITMAP bm;
        GetObject(bitmap.GetSafeHandle(), sizeof(bm), &bm);
    
        lpMeasureItemStruct->itemWidth = bm.bmWidth;
        lpMeasureItemStruct->itemHeight = bm.bmHeight;
      }
      else
      {
      CMDIFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
      }
      }
    
  6. Add the following code in the OnDrawItem method.
  7.   void CMainFrame::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT
                                              lpDrawItemStruct)
    {
      if (ID_HARIKRISHNA == lpDrawItemStruct ->itemID
                         && ODT_MENU
                         == lpDrawItemStruct ->CtlType )
      {
        CBitmap bitmap;
        bitmap.LoadBitmap(IDB_BITMAP1);
    // IDB_BITMAP1 points to the BMP resource of your company
    // logo.
        HDC hTempDC = ::CreateCompatibleDC(lpDrawItemStruct->hDC);
        ::SelectObject(hTempDC,bitmap.GetSafeHandle());
        BitBlt(lpDrawItemStruct->hDC,
               lpDrawItemStruct->rcItem.left,
               lpDrawItemStruct->rcItem.top,
               lpDrawItemStruct->rcItem.right,
               lpDrawItemStruct->rcItem.bottom,hTempDC,
          0,0,SRCCOPY);
    
      }
      else
      CMDIFrameWnd::OnDrawItem(nIDCtl, lpDrawItemStruct);
    }
    
    

That's it. You are done.

There are some points that need to be taken care of:

  1. You need to add an extra menu item in all resources that you use in your project.
  2. The ID of the menu item resource needs to be unique, particularly if you are merging code with already existing projects. Make sure the ID of company logo menu item doesn't contradict already existing IDs.

And the environment in which the following code was tested is Window 2000 and WinNT 4.0. It should also work in on operating systems greater than Windows 95 because the Menu flags MFT_RIGHTJUSTIFY exist only those operating systems. And I have built the above code only on VC6.0.

Downloads

Download demo project - 30 Kb


Comments

  • Fix "not recognized" JUSTIFYRIGHT etc.

    Posted by Legacy on 11/19/2003 12:00am

    Originally posted by: Brian

    Open the .rc file in note pad. In an MDI app, only add to the MDI CHILD menu area. after the END of the last "regular" menu. but before the END of the menu area.

    END
    MENUITEM "", ID_MYMENUITEMID, OWNERDRAW
    END

    Just use OWNERDRAW nothing else required. You should probably add this as last thing you do with menu, if you edit your menu with the menu wizard, you will likely have to reopen the .rc and append , OWNERDRAW again and again.

    if you want the item to be right justified. go into the menu wizard, right click the blank area that represents this new menu item, click properties, in the menuitem properties, just choose true for right justified. thow agian likely have to go into note pad and append , OWNERDRAW as your final step.

    read my next entry for good tip on avoiding drawing issues.

    Reply
  • fonts in menu bar

    Posted by Legacy on 05/18/2003 12:00am

    Originally posted by: Bui Tan Duoc

    How can I change font in the main menu bar of my application only ? Please help me, thanks much !

    Reply
  • don't recognize MFT_STRING | MFT_OWNERDRAW | MFT_RIGHTJUSTIFY,MFS_ENABLED

    Posted by Legacy on 05/01/2003 12:00am

    Originally posted by: Dafna Zamir

    I use sdi application. After adding the types MFT_STRING | MFT_OWNERDRAW | MFT_RIGHTJUSTIFY,MFS_ENABLED closing the .rc file and the resource (in an automatic mannar) visual 6 prompt the message: unkown menu subtype.
    How can I resolve this?

    • MENUEX

      Posted by wolfgangjia on 09/13/2005 11:39pm

      Use MENUEX resource language instead of MENU

      Reply
    • ModifyMenu

      Posted by David Smulders on 07/06/2004 11:07am

      this->GetMenu()->ModifyMenu(ID_MENU_LOGO, MF_BYCOMMAND|MF_OWNERDRAW, ID_MENU_LOGO);

      Reply
    Reply
  • Does this work for an SDI

    Posted by Legacy on 12/18/2002 12:00am

    Originally posted by: JM

    Can't seem to get this to work for an SDI application. Any thoughts?

    Reply
  • company logo on the menubar

    Posted by Legacy on 12/16/2002 12:00am

    Originally posted by: rajasekar

    this is the nice article among all the others in the latest articles.  i have thaougt to add the company logo in my project in various places.but i didnt thought of the menu bar.this makes me to build my project more efficiently.
    
    thanks for the authour of the page and the article

    Reply
  • hello

    Posted by Legacy on 12/14/2002 12:00am

    Originally posted by: BARADHI

    hai
    

    Reply
  • Logo on Menubar.

    Posted by Legacy on 12/12/2002 12:00am

    Originally posted by: Srinivasa Raju Bhairraju

    This article is for displaying company logo on menubar not on toolbar. Good work.
    

    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 …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds