none
为何加载的工具栏图片的背景色是黑色的? RRS feed

  • 问题

  •         我基于MFC单文档程序写了一段测试代码,实现了动态创建菜单栏和工具栏,代码如下:

    static void DelRedundantMenu(HMENU hMenu)
    {
    	//  首先删除多余的菜单项
    	int Menucount = GetMenuItemCount(hMenu);
    	for (int i = Menucount - 1; i > -1; i--)
    	{
    		::DeleteMenu(hMenu, i, MF_BYPOSITION);
    	}
    }
    
    BOOL CMainFrame::CreateMenuBar()
    {
    	// 删除默认的菜单
    	CMenu* pMenu = GetMenu();
    	if (NULL != pMenu)
    	{
    		DelRedundantMenu(pMenu->GetSafeHmenu());
    	}
    
    	CMenu PopupMenu;
    	PopupMenu.CreatePopupMenu();
    	PopupMenu.AppendMenu(MF_STRING, 1024, "&New");
    	PopupMenu.AppendMenu(MF_STRING, 1025, "&Open");
    	PopupMenu.AppendMenu(MF_STRING, 1026, "&Close");
    	PopupMenu.AppendMenu(MF_STRING, 1027, "E&xit");
    	pMenu->AppendMenu(MF_POPUP, (UINT)PopupMenu.m_hMenu, "&File"); //关键!  
    	PopupMenu.Detach();	
    
    
    	return TRUE;
    }
    
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    		return -1;
    
    
    	CreateMenuBar();
    	
    	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
    	{
    		TRACE0("未能创建工具栏\n");
    		return -1;      // 未能创建
    	}
    
    	TCHAR dir[MAX_PATH], fname[MAX_PATH];
    	GetModuleFileName((HMODULE)theApp.m_hInstance, dir, MAX_PATH);
    	TCHAR *p1 = _tcsrchr(dir, _T('\\'));
    	*p1 = 0;
    
    	
    	CImage img;
    	_stprintf_s(fname, MAX_PATH, _T("%s\\apple_64px.png"), dir);
    	img.Load(fname);
    
    	CImageList imglist;
    	imglist.Create(img.GetWidth(), img.GetHeight(), ILC_COLOR32, 16, 16);
    
    	ImageList_Add(imglist.GetSafeHandle(), (HBITMAP)img, NULL);
    
    	_stprintf_s(fname, MAX_PATH, _T("%s\\heart_64px.png"), dir);
    	img.Destroy();
    	img.Load(fname);
    	ImageList_Add(imglist.GetSafeHandle(), (HBITMAP)img, NULL);
    
    	_stprintf_s(fname, MAX_PATH, _T("%s\\windows_xp_64px.png"), dir);
    	img.Destroy();
    	img.Load(fname);
    	ImageList_Add(imglist.GetSafeHandle(), (HBITMAP)img, NULL);
    
    	HIMAGELIST hImgs = imglist.Detach();
    
    	SIZE sizeButton, sizeImage;
    	sizeButton.cx = img.GetWidth() + 7;
    	sizeButton.cy = img.GetHeight() + 6;
    	sizeImage.cx = img.GetWidth();
    	sizeImage.cy = img.GetHeight();
    	m_wndToolBar.SetSizes(sizeButton, sizeImage);
    	m_wndToolBar.SendMessage(TB_SETIMAGELIST, 0, (LPARAM)hImgs);
    
    	int i = 0;
    	TBBUTTON buttons[3];
    	
    	buttons[i].iBitmap = 0;
    	buttons[i].idCommand = ID_APP_ABOUT;
    	buttons[i].fsState = TBSTATE_ENABLED;
    	buttons[i].fsStyle = BTNS_BUTTON;
    	buttons[i].dwData = 0;
    	buttons[i].iString = 0;
    	i++;
    
    	buttons[i].iBitmap = 1;
    	buttons[i].idCommand = ID_FILE_OPEN;
    	buttons[i].fsState = TBSTATE_ENABLED;
    	buttons[i].fsStyle = BTNS_BUTTON;
    	buttons[i].dwData = 0;
    	buttons[i].iString = 0;
    	i++;
    
    	buttons[i].iBitmap = 2;
    	buttons[i].idCommand = ID_FILE_SAVE;
    	buttons[i].fsState = TBSTATE_ENABLED;
    	buttons[i].fsStyle = BTNS_BUTTON;
    	buttons[i].dwData = 0;
    	buttons[i].iString = 0;
    	i++;
    
    	m_wndToolBar.SendMessage(TB_ADDBUTTONS, i, (LPARAM)buttons);
    	
    	if (!m_wndStatusBar.Create(this) ||
    		!m_wndStatusBar.SetIndicators(indicators,
    		  sizeof(indicators)/sizeof(UINT)))
    	{
    		TRACE0("未能创建状态栏\n");
    		return -1;      // 未能创建
    	}
    
    	// TODO: 如果不需要工具栏可停靠,则删除这三行
    	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
    	EnableDocking(CBRS_ALIGN_ANY);
    	DockControlBar(&m_wndToolBar);
    
    	return 0;
    }

          效果图下图:

         可以看出工具栏图片成功加载了,但是为何背景色是黑色的呢?


    前无古人,后无来者

    2021年10月6日 9:22

全部回复

  • 你好,

    感谢你在MSDN发帖。

    我建议你尝试使用尝试使用ILC_MASK | ILC_COLOR。

    Best Regards,

    Jeanine

    "Visual c++" forum will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A !
    For more information, please refer to the sticky post.

    2021年10月12日 8:18
    版主
  • 你好,

    感谢你在MSDN发帖。

    我建议你尝试使用尝试使用ILC_MASK | ILC_COLOR。

    Best Regards,

    Jeanine

    "Visual c++" forum will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A !
    For more information, please refer to the sticky post.

    已经找到正确答案了,问题的根源在于链接到了很古老的版本的 commctrl.dll,还没有支持 32 bpp 的 ImageList。解决这个问题的版本是在stdafx.h中加入下面代码:

    #if defined _M_IX86
    #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #elif defined _M_IA64
    #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #elif defined _M_X64
    #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #else
    #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
    #endif

    前无古人,后无来者

    2021年11月1日 6:44