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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read