在CMFCToolbar的菜单中设置位图 [英] Setting Bitmap in menu from CMFCToolbar

查看:476
本文介绍了在CMFCToolbar的菜单中设置位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://www.codeproject.com/articles/1790/menu-bitmaps-from-minimal-source-code

https://www.codeproject.com/articles/1790/menu-bitmaps-from-minimal-source-code

此链接用于从CToolbar向菜单添加位图。

This link is for adding bitmap to menu from CToolbar.

我修改了从CMFCToolbar向菜单添加位图的代码。

I modified the code for adding bitmap to menu from CMFCToolbar.

下面是我的代码。

void CMainFrame::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
  // Call the base class function.
  if (lpMeasureItemStruct->CtlType != ODT_MENU)
  {
      CFrameWnd::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
      return;
  }
  // Find the size of a tool bar button.
  IMAGEINFO imageInfo;
  CMFCToolBar *toolBar = m_pToolbars[1];//starting from 1 leaving 123bar
  CSize size = toolBar->GetImageSize();
  
  // Add at least a one pixel border around the bitmap so there is room 
  // to draw a raised button. Also, put some extra horizontal space 
  // between the bitmap and the menu text.
  lpMeasureItemStruct->itemHeight = (int)( __max(size.cy+2, (int)lpMeasureItemStruct->itemHeight+3));
  double ratio = (size.cx+2) / (double) (size.cy+2);
  lpMeasureItemStruct->itemWidth = (UINT)ratio * lpMeasureItemStruct->itemHeight + MENU_TEXT_SPACE;
}

//
// Draw a tool bar bitmap on the menu. The bitmap will be drawn gray if the
// corresponding button is currently disabled.
//
void CMainFrame::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
   // Call the base class function.
  CFrameWnd::OnDrawItem(nIDCtl, lpDrawItemStruct);

  // The remainder of this function pertains to menus.
  if (lpDrawItemStruct->CtlType != ODT_MENU) return;
 

  // Get information about the menu item to be drawn.
  bool isDisabled = ((lpDrawItemStruct->itemState & ODS_GRAYED) != 0);
//  bool isSelected = ((lpDrawItemStruct->itemState & ODS_SELECTED) != 0);
  CDC* dc = CDC::FromHandle(lpDrawItemStruct->hDC);

  // Get the image that corresponds to this menu item.
  int imageID;
  CMFCToolBarImages *imageList;
  GetImage(lpDrawItemStruct->itemID, imageID, imageList,isDisabled);
  if (imageID == -1) return;

  // Get the size and position of the button and bitmap.
  CSize bitmapSize = imageList->GetImageSize();
  CSize buttonSize(lpDrawItemStruct->rcItem.right - MENU_TEXT_SPACE - 2,
                   lpDrawItemStruct->rcItem.bottom);
  CPoint bitmapPosition((buttonSize.cx - bitmapSize.cx) / 2 + 1,
                        (buttonSize.cy - bitmapSize.cy) / 2 + 1);
  ASSERT(bitmapPosition.x >= 0 && bitmapPosition.y >= 0);

  // The item is disabled, so draw a monochrome bitmap.
  
  CAfxDrawState ds;
  imageList->PrepareDrawImage(ds);
  if (isDisabled) 
	  imageList->Draw(dc, bitmapPosition.x,bitmapPosition.y,imageID,0,1);
  else
  // Draw the bitmap on the menu.
  imageList->Draw(dc, bitmapPosition.x,bitmapPosition.y,imageID);//, bitmapPosition, ILD_TRANSPARENT);
  imageList->EndDrawImage(ds);
  return;
}

//
// Given a menu ID, get the corresponding image. This routine returns
// an image list and an image ID which is the index of the image within 
// the image list. If there is no tool bar button corresponding to the
// given menu ID, then the returned image ID is -1.
//
void CMainFrame::GetImage(UINT menuID, int &imageID, CMFCToolBarImages *&imageList, bool bDisabled)
{
	// Find the tool bar and button that correspond to the menu ID.
	int buttonID = imageID = -1;

	int nSize = sizeof(m_pToolbars)/sizeof(CEzToolbar*);
	//starting from 1 leaving 123bar
	for(int i=1;i<nSize;i++)
	{
		buttonID = m_pToolbars[i]->CommandToIndex(menuID);
		if (buttonID != -1) break;
	}
	if (buttonID == -1) return;

	// Find the corresponding image ID.
	UINT commandID, style;
	m_pToolbars[i]->GetButtonInfo(buttonID, commandID, style, imageID);
	imageList =m_pToolbars[i]->GetLockedImages();
}

我的代码使用CMFCToolBarIMages绘制功能来绘制图像。

My code is using CMFCToolBarIMages draw function for drawing images.

在启用菜单中图像正常显示。禁用的菜单图像无法正常显示,而在工具栏中,禁用的图像显示正常。

In enabled menu images are coming properly.In disabled menu images are not coming properly whereas in toolbar the disabled images are displaying properly.

请帮助任何人解决此问题。

please help anyone to resolve this probelm.

推荐答案

您好sgrm123,

Hi sgrm123,

感谢您在此处发布。

>>启用菜单中的图片即将推出在禁用的菜单中图像不能正常显示,而在工具栏中,禁用的图像显示正确。

bDisable参数设置要以禁用的样式绘制的图像使它们变灰。

The bDisable parameter set the images to be drawn in the disabled style which makes them gray.

您可以尝试使用bInactive参数将图像设置为b e以非活动状态样式绘制。

You could try to use bInactive parameter to set the images to be drawn in the inactive state style.

例如:

if (isDisabled) imageList->Draw(dc, bitmapPosition.x,bitmapPosition.y,imageID,0,0,0,0,1); 

希望这可以帮助你。

最好的问候,

Sera Yu


这篇关于在CMFCToolbar的菜单中设置位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆