⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owner_drawn_menu_2b.shtml

📁 mfc资源大全包含MFC编程各个方面的源码
💻 SHTML
📖 第 1 页 / 共 2 页
字号:

	// draw the text if there is any
	//We have to paint the text only if the image is nonexistant

	if (hIcon != NULL)
	{
		DrawIconEx(pDC->GetSafeHdc(), rect.left,
					rect.top + (rect.Height() - iconY) / 2, hIcon,
					iconX, iconY, 0, NULL, DI_NORMAL);
	}

	//This is needed always so that we can have the space for check marks
	rect.left = rect.left + ((iconX) ? iconX : 32) + 2;

	if( !strText.IsEmpty() )
	{
//		pFont->GetLogFont(&lf);
		
		int iOldMode = pDC->GetBkMode();
		
		pDC->SetBkMode(TRANSPARENT);

		// Draw the text in the correct colour:
		UINT nFormat = DT_LEFT|DT_SINGLELINE|DT_EXPANDTABS|DT_VCENTER;

		if( !(lpDIS->itemState & ODS_GRAYED) )
		{
			pDC->SetTextColor(crText);
			pDC->DrawText (strText,rect,nFormat);
		}
		else
		{
			// Draw the slightly lighter greyed text at an offset:
			
			RECT offset = *rect;

			offset.left += 1;
			offset.right += 1;
			offset.top += 1;
			offset.bottom += 1;

			pDC->SetTextColor(GetSysColor(COLOR_BTNHILIGHT));
			pDC->DrawText(strText,&offset, nFormat);
			// And the standard Grey text:
			pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
			pDC->DrawText(strText,rect, nFormat);
		}

		pFont = pDC->SelectObject(&dispFont);
		pDC->SetBkMode(iOldMode);
		pDC->SelectObject(pFont); //set it to the old font
	}
	dispFont.DeleteObject();
}

/*
	=================================================================================
	void CBitmapMenu::MeasureItem(LPMEASUREITEMSTRUCT)
	---------------------------------------------
	Called by the framework when it wants to know what the width and height of our
	item will be.  To accomplish this we provide the width of the icon plus the
	width of the menu text, and then the height of the icon.
	=================================================================================
*/
void CBitmapMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
	// Obtain the width of the text:
	CWnd *pWnd = AfxGetMainWnd();						// Get main window
	CDC *pDC = pWnd->GetDC();							// Get device context
	CFont* pFont = pDC->SelectObject(&m_fontMenu);		// Select menu font in...
	LPCTSTR lpstrText = (((BMenuData*)(lpMIS->itemData))->menuText);	// Get pointer to text

	SIZE t;

	GetTextExtentPoint32(pDC->GetSafeHdc(), lpstrText, strlen(lpstrText), &t); // Width of caption

	pDC->SelectObject(pFont);							// Select old font in
	
	pWnd->ReleaseDC(pDC);			// Release the DC

	// Set width and height:
	IMAGEINFO ii;

	m_pList->GetImageInfo(((BMenuData*)(lpMIS->itemData))->menuIconNormal, &ii);
	lpMIS->itemWidth = ii.rcImage.right - ii.rcImage.left + 10;
	lpMIS->itemHeight = max(ii.rcImage.bottom - ii.rcImage.top, GetSystemMetrics(SM_CYMENU));
}

void CBitmapMenu::SetTextColor(COLORREF clrText)
{
	m_crText = clrText;
}

void CBitmapMenu::SetBackColor(COLORREF clrBack)
{
	m_clrBack = clrBack;

	if( (HBRUSH)m_brBackground != NULL )
		m_brBackground.DeleteObject();
	m_brBackground.CreateSolidBrush (clrBack);
}

void CBitmapMenu::SetHighlightColor(COLORREF clrHilight)
{
	m_clrHilight = clrHilight;

	if( (HBRUSH)m_brSelect != NULL )
		m_brSelect.DeleteObject();
	m_brSelect.CreateSolidBrush(clrHilight);
}

void CBitmapMenu::SetHighlightTextColor(COLORREF clrHilightText)
{
	m_clrHilightText = clrHilightText;
}

void CBitmapMenu::SetHighlightStyle(HIGHLIGHTSTYLE hilightStyle)
{
	m_hilightStyle = hilightStyle;
}

void CBitmapMenu::SetImageList(CImageList* pList)
{
	m_pList = pList;
}

/*
	=================================================================================
	BOOL CBitmapMenu::AppendODMenu(LPCTSTR, UINT, UINT, UINT, UINT)
	BOOL CBitmapMenu::ModifyODMenu(LPCTSTR, UINT, UINT, UINT, UINT)
	----------------------------------------------------------
	These 2 functions effectively replace the CMenu::AppendMenu() and CMenu::ModifyMenu()
	classes, with support for 3 icon states.  When using Owner-drawn menu items,
	use these functions instead of the usual ones.
	=================================================================================
*/
BOOL CBitmapMenu::AppendODMenu(LPCTSTR lpstrText,
							  UINT nFlags,
							  UINT nID,
							  int nIconNormal,
							  int nIconSelected,
							  int nIconDisabled)
{
	// Add the MF_OWNERDRAW flag if not specified:
	if( !(nFlags & MF_OWNERDRAW) )
		nFlags |= MF_OWNERDRAW;

	BMenuData *mdata = new BMenuData;
	m_MenuList.Add(mdata);

	lstrcpy(mdata->menuText, lpstrText);
	mdata->menuIconNormal = nIconNormal;
	mdata->menuIconSelected = nIconSelected;
	mdata->menuIconDisabled = nIconDisabled;
	return CMenu::AppendMenu(nFlags, nID, (LPCTSTR)mdata);
}

BOOL CBitmapMenu::ModifyODMenu(LPCTSTR lpstrText,
							  UINT nID,
							  int nIconNormal,
							  int nIconSelected,
							  int nIconDisabled)
{
	// Delete the existing BMenuData structure associated with this item (if any)
	int numMenuItems = m_MenuList.GetUpperBound();
	BMenuData *mdata;

	for( int m = 0; m <= numMenuItems; m++ )
	{
		if( (mdata = m_MenuList[m]) != NULL )
		{
			if( mdata->nID == nID )
			{
				delete(mdata);
				m_MenuList.RemoveAt(m);
				break;
			}
		}
	}
	
	// Create a new BMenuData structure:
	mdata = new BMenuData;
	m_MenuList.Add(mdata);

	lstrcpy(mdata->menuText, lpstrText);
	mdata->menuIconNormal = nIconNormal;
	mdata->menuIconSelected = nIconSelected;
	mdata->menuIconDisabled = nIconDisabled;

	return CMenu::ModifyMenu(nID, MF_BYCOMMAND | MF_OWNERDRAW, nID, (LPCTSTR)mdata);
}

BOOL CBitmapMenu::LoadMenu(LPCTSTR lpszResourceName)
{
	return CBitmapMenu::LoadMenu(MAKEINTRESOURCE(lpszResourceName));
}

BOOL CBitmapMenu::LoadMenu(int nResource)
{
	// Find the Menu Resource:
	DWORD dwSize;
	WORD wrd = MAKEWORD(nResource, 0);

	HRSRC hRsrc = FindResource(NULL, (LPCTSTR)wrd, RT_MENU);

	// Find the resource
	if( hRsrc == NULL )
	{
		TRACE0("CBitmapMenu::LoadMenu() - Failed to find Menu Resource\n");
		ASSERT(FALSE);
	}

	// Get size of resource:
	dwSize = SizeofResource(NULL, hRsrc);

	// Load the Menu Resource:
	HGLOBAL hGlobal = LoadResource(NULL, hRsrc);
	if( hGlobal == NULL )
	{
		TRACE0("CBitmapMenu::LoadMenu() - Failed to load Menu Resource\n");
		ASSERT(FALSE);
	}
	// Attempt to create us as a menu...
	if( !CMenu::CreateMenu() )
	{
		TRACE0("CBitmapMenu::LoadMenu() - Failed to create Menu\n");
		ASSERT(FALSE);
	}
	// Get Item template Header, and calculate offset of MENUITEMTEMPLATES
	MENUITEMTEMPLATEHEADER* pTpHdr = (MENUITEMTEMPLATEHEADER*)LockResource(hGlobal);
	BYTE*					pTp   = (BYTE*)pTpHdr + 
									(sizeof(MENUITEMTEMPLATEHEADER) + pTpHdr->offset);

	// Variables needed during processing of Menu Item Templates:
	BMenuData*	pData = NULL;							// New OD Menu Item Data
	WORD		dwFlags = 0;							// Flags of the Menu Item
	WORD		dwID	= 0;							// ID of the Menu Item
	BOOL		bLastPopup = 0; 						// Last popup?
	UINT		uFlags; 								// Actual Flags.
	char		caption[80];							// Allows up to 80 chars for caption
	int 		nLen   = 0; 							// Length of caption
	CTypedPtrArray<CPtrArray, CBitmapMenu*> m_Stack;		// Popup menu stack

	m_Stack.Add(this);									// Add it to this...
	
	do
	{
		// Obtain Flags and (if necessary), the ID...
		memcpy(&dwFlags, pTp, sizeof(WORD));
		pTp+=sizeof(WORD);
		
		// Obtain Flags
		if( !(dwFlags & MF_POPUP) )
		{
			memcpy(&dwID, pTp, sizeof(WORD));
			// Obtain ID
			pTp += sizeof(WORD);
		}
		else
			dwID = 0;

		uFlags = (UINT)dwFlags; 		// Remove MF_END from the flags that will
		if( uFlags & MF_END )			// be passed to the Append(OD)Menu functions.
			uFlags -= MF_END;
		// Obtain Caption (and length)
		nLen = 0;

		char *ch = (char*)pTp;
		while( *ch != 0 )
		{
			caption[nLen] = ch[0];
			nLen++; 					// Increment length
			ch+=2;						// Accounts for UNICODE '0's...
		}

		caption[nLen] = 0;				// Zero-terminate the string...
		pTp += (nLen+1) * 2;				// Increase Pointer...
		
		// Handle popup menus first....
		if( dwFlags & MF_POPUP )
		{
			if( dwFlags & MF_END )
				bLastPopup = TRUE;

			CBitmapMenu* pSubMenu = new CBitmapMenu;
			
			pSubMenu->CreatePopupMenu();
			
			// Append it to the top of the stack:
			m_Stack[m_Stack.GetUpperBound()]->AppendMenu(uFlags, (UINT)pSubMenu->m_hMenu, caption);
			m_Stack.Add(pSubMenu);					// Add to PopupStack
			m_SubMenus.Add(pSubMenu);				// For deletion...
		}
		else
		{
			m_Stack[m_Stack.GetUpperBound()]->AppendODMenu(caption, uFlags, dwID, -1, -1, -1);
			if( dwFlags & MF_END )
				m_Stack.RemoveAt(m_Stack.GetUpperBound());	// Remove top of stack
			if(bLastPopup)
			{
				bLastPopup = FALSE;
				m_Stack.RemoveAt(m_Stack.GetUpperBound());	// And again...
			}
		}
	}
	while( m_Stack.GetUpperBound() != -1 );
	return TRUE;
}

</FONT></TT></PRE>

<P>Posted on: May 27, 1998.
<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD>

<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>&copy; 1998 Zafir Anjum</FONT>&nbsp;</CENTER>
</TD>

<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A>&nbsp;</FONT></DIV>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -