我只知可从Menu派生一个类,可如何使之工作呢?

解决方案 »

  1.   

    没做过.不过codeproject上有一个类,关于按钮的.CButtonST
    可以去查查
      

  2.   

    菜单加入背景图片?那就要看你实现些什么功能!----------------------------
    你看看下面这派生类的定义,好多哦!!
    class CMenuXP : public CMenu  
    {
    DECLARE_DYNAMIC(CMenuXP)public:
    CMenuXP();
    virtual ~CMenuXP(); //Menu style(Default: STYLE_OFFICE)
    typedef enum 
    {
    STYLE_OFFICE, //Draw a float button around the icon
    STYLE_STARTMENU, //show selected bar below the icon
    STYLE_XP //use different color for the icon area
    } MENUSTYLE; //Below is the functions to build the menu
    BOOL AddSideBar(CMenuXPSideBar *pItem);
    BOOL AppendODMenu(UINT nFlags, CMenuXPItem *pItem, ACCEL *pAccel=0);
    BOOL AppendSeparator(void);
    BOOL AppendODPopup(UINT nFlags, CMenuXP *pPopup, CMenuXPItem *pItem);
    void Break(void); //change a column(the next item added will be in a new column)
    void BreakBar(void); //change a column with a break line(same as Break, except that a break line is drawn between two columns)protected:
    CFont m_fontMenu;
    COLORREF m_clrBackGround; //Background color
    COLORREF m_clrSelectedBar; //selected bar color
    COLORREF m_clrText; //Text color
    COLORREF m_clrSelectedText; //selected text color
    COLORREF m_clrDisabledText; //disabled text color COLORREF m_clrSideBarStart; //Start color of the gradient sidebar
    COLORREF m_clrSideBarEnd; //end color of the gradient sidebar
    COLORREF m_clrIconArea; //Background color of the button(icon) area BOOL m_bBreak; //if true, next item inserted into the menu will be added with the sytle MF_MENUBREAK
    BOOL m_bBreakBar; //if true, next item inserted into the menu will be added with the sytle MF_MENUBARBREAK HBITMAP m_hBitmap; //Background bitmap
    CDC m_memDC; //Memory dc holding the background bitmap MENUSTYLE m_Style; //menu style(currently support office or startmenu style)public: //User these functions to change the default attribute of the menu
    void SetBackColor(COLORREF clr) { m_clrBackGround = clr; }
    void SetSelectedBarColor(COLORREF clr) { m_clrSelectedBar = clr; }
    void SetTextColor(COLORREF clr) { m_clrText = clr; }
    void SetSelectedTextColor(COLORREF clr) { m_clrSelectedText = clr; }
    void SetDisabledTextColor(COLORREF clr) { m_clrDisabledText = clr; }
    void SetSideBarStartColor(COLORREF clr) { m_clrSideBarStart = clr; }
    void SetSideBarEndColor(COLORREF clr) { m_clrSideBarEnd = clr; }
    void SetIconAreaColor(COLORREF clr) { m_clrIconArea = clr; }
    void SetBackBitmap(HBITMAP hBmp);

    void SetMenuStyle(MENUSTYLE style) { m_Style = style; } BOOL SetMenuFont(LOGFONT lgfont);
    //Find the popupmenu from a menuitem id, you may not need it
    CMenuXP *FindSubMenuFromID(DWORD dwID);public:
    virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
    virtual void MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct );
    static LRESULT OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu);protected:
    virtual void DrawBackGround(CDC *pDC, CRect rect, BOOL bSelected, BOOL bDisabled);
    virtual void DrawButton(CDC *pDC, CRect rect, BOOL bSelected, BOOL bDisabled, BOOL bChecked);
    virtual void DrawIcon(CDC *pDC, CRect rect, HICON hIcon, BOOL bSelected, BOOL bDisabled, BOOL bChecked);
    virtual void DrawSideBar(CDC *pDC, CRect rect, HICON hIcon, CString strText);
    virtual void DrawText(CDC *pDC, CRect rect, CString strText, BOOL bSelected, BOOL bDisabled, BOOL bBold);
    virtual void DrawCheckMark(CDC *pDC, CRect rect, BOOL bSelected);
    virtual void DrawMenuText(CDC& dc, CRect rc, CString text, COLORREF color);
    virtual void DrawIconArea(CDC *pDC, CRect rect, BOOL bSelected, BOOL bDisabled, BOOL bChecked); void Clear(void); //Clean all memory and handles //helpers 
    HBITMAP CreateGradientBMP(HDC hDC,COLORREF cl1,COLORREF cl2,int nWidth,int nHeight,int nDir,int nNumColors);
    void DrawEmbossed(CDC *pDC, HICON hIcon, CRect rect, BOOL bColor = FALSE, BOOL bShadow = FALSE);
    void FillRect(CDC *pDC, const CRect& rc, COLORREF color);
    };
    ----------------------------
    有了这个才能到你的
    XXXXView::OnContextMenu(CWnd* pWnd, CPoint point)
    函数里去使用。
    对于你的就是要:
    CBitmap bmp;
    bmp.LoadBitmap(IDB_BACK);
    pMenu->SetBackBitmap((HBITMAP)bmp.Detach());
    那么那个派生类怎么也要实现:
    SetBackBitmap()吧:)那个例子里面是这么实现的。
    void CMenuXP::SetBackBitmap(HBITMAP hBmp)
    {
    if (hBmp == NULL && m_hBitmap)
    {
    ::DeleteObject(m_hBitmap);
    m_hBitmap = NULL;
    m_memDC.DeleteDC();
    return;
    }
    m_hBitmap = hBmp;
    if (!m_memDC.m_hDC)
    {
    CWindowDC dc(NULL);
    m_memDC.CreateCompatibleDC(&dc);
    } ASSERT(m_memDC.m_hDC); ::SelectObject(m_memDC.m_hDC, m_hBitmap);
    }
    具体的你还是找个例子看吧。共同学习。:)
      

  3.   

    zhdleo(叮东) :你好你说的过程我大致了解,可是这是关于弹出菜单的。我是要给框架自动生成的菜单加背景,是不是只有不生成框架菜单,只有自己画了呢?
      

  4.   

    自画菜单
    在DrawItem里把背景画上去
      

  5.   

    stoneyrh() :你好那么如何使这个派生的CMENU类的DRAWITEM工作呢,框假生成时只管他自己的破菜单,难道要框架不生成菜单,然后自己找到位置自己CREATE菜单?
      

  6.   

    重载CMainFrame的OnDrawItem和OnMeasureItem
      

  7.   

    自画菜单CYourMenu类,然后设置到CMainFrame中去。
    CMainFrame::OnCreate(...)
    {
        CYourMenu* pMenu = new CYourMenu;
        pMenu ->CreateMen();
        SetMenu(pMenu);
    }
      

  8.   

    继承cmenu,在drawitem和ctrcolor(好象写错了,没怎么用vc了,不大记得了)里重画设置背景就可以了
      

  9.   

    http://www.codeproject.com/menu/NewMenuXPStyle.asp
    重载\CMenu可以改弹出菜单,但是对于主框架菜单是不行的吧