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

📄 owner_drawn_menu2.shtml

📁 mfc资源大全包含MFC编程各个方面的源码
💻 SHTML
📖 第 1 页 / 共 2 页
字号:
            pDC->FillRect (rect,&m_brBackground);
        else
            pDC->FillRect (rect,&m_brSelect);

        pDC->SelectObject (pOldPen);
        //pDC->Draw3dRect (rect,GetSysColor (COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));

		// This version provides a black-border:


		pDC->Draw3dRect (rect,GetSysColor (COLOR_3DHILIGHT),RGB(0,0,0));        

		//lf.lfWeight = FW_BOLD;
        if ((HFONT)dispFont != NULL)
                dispFont.DeleteObject ();
        dispFont.CreateFontIndirect (&lf);
        crText = m_clrHilightText;
    }
    else
    {
        CPen *pOldPen = pDC->SelectObject (&m_penBack);
        pDC->FillRect (rect,&m_brBackground);
        pDC->SelectObject (pOldPen);


        // draw the up edges


        pDC->Draw3dRect (rect,m_clrBack,m_clrBack);
        if ((HFONT)dispFont != NULL)
                dispFont.DeleteObject ();
        dispFont.CreateFontIndirect (&lf); //Normal

    }


    // 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,hIcon,
					m_iconX,m_iconY,0,NULL,DI_NORMAL);
	};

    //This is needed always so that we can have the space for check marks


    rect.left = rect.left +((m_iconX)?m_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 TCMenu::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 TCMenu::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 = (((TCMenuData*)(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
    AfxGetApp()->m_pMainWnd->ReleaseDC (pDC);			// Release the DC

	// Set width and height:

    lpMIS->itemWidth = m_iconX + t.cx + 10;
	lpMIS->itemHeight = m_iconY;
}

void TCMenu::SetIconSize (int width, int height)
{
    m_iconX = width;
    m_iconY = height;
}

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

void TCMenu::SetBackColor (COLORREF clrBack)
{
    m_clrBack = clrBack;
    if ((HBRUSH)m_brBackground != NULL)
		m_brBackground.DeleteObject ();

    m_brBackground.CreateSolidBrush (clrBack);
}

void TCMenu::SetHighlightColor (COLORREF clrHilight)
{
    m_clrHilight = clrHilight;
    if ((HBRUSH)m_brSelect != NULL)
                    m_brSelect.DeleteObject ();

    m_brSelect.CreateSolidBrush (clrHilight);
}

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


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


/*
	=================================================================================
	BOOL TCMenu::AppendODMenu(LPCTSTR, UINT, UINT, UINT, UINT)
	BOOL TCMenu::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 TCMenu::AppendODMenu(LPCTSTR lpstrText,
						  UINT  nFlags,
						  UINT	nID,
						  UINT	nIconNormal,
						  UINT	nIconSelected,
						  UINT	nIconDisabled)
{
	// Add the MF_OWNERDRAW flag if not specified:

	if(!(nFlags & MF_OWNERDRAW))
		nFlags |= MF_OWNERDRAW;

	TCMenuData *mdata = new TCMenuData;
	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 TCMenu::ModifyODMenu(LPCTSTR lpstrText,
						  UINT	  nID,
						  UINT	  nIconNormal,
						  UINT	  nIconSelected,
						  UINT	  nIconDisabled)
{
	// Delete the existing TCMenuData structure associated with this item (if any)


	int numMenuItems = m_MenuList.GetUpperBound();
	TCMenuData *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 TCMenuData structure:


	mdata = new TCMenuData;
	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 TCMenu::LoadMenu(LPCTSTR lpszResourceName)
{
	return(TCMenu::LoadMenu(MAKEINTRESOURCE(lpszResourceName)));
};

BOOL TCMenu::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("TCMenu::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("TCMenu::LoadMenu() - Failed to load Menu Resource\n");
		ASSERT(FALSE);
	};


	// Attempt to create us as a menu...


	if(!CMenu::CreateMenu())
	{
		TRACE0("TCMenu::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:


	TCMenuData*	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, TCMenu*>	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;

			TCMenu* pSubMenu = new TCMenu;
			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></pre>















<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 + -