| 网站首页 | 文章中心 | 电子书下载 | 矢量图库 | 视频教程 | 素材下载 | 程序代码下载 | JS代码 | 论坛 | 
常用软件类:
|杀毒安全 |联络聊天 |网络软件 |多媒体类 |系统工具 |图形图像 |系统工具 |应用软件 |行业软件
开发设计类:
|动画制作 |图像处理 |3D设计 |操作系统 |站长学院 |网络相关 |WEB设计 |数据库类 |程序开发
VC++的菜单控制和自绘菜单
作者:佚名    文章来源:网络    点击数:    更新时间:2006-11-12
 


////////////////////////////////////////////////
//
// CCustomMenu message handlers


void CCustomMenu::DrawItem (LPDRAWITEMSTRUCT lpDIS)
{
    ASSERT(lpDIS != NULL);

    CDC* pDC = CDC::FromHandle(lpDIS->hDC);
    CRect rect;
    HICON hIcon;
    COLORREF crText = m_crText;
    // draw the colored rectangle portion
    rect.CopyRect(&lpDIS->rcItem);

    // draw the up/down/focused/disabled state

    UINT action = lpDIS->itemAction;
    UINT state = lpDIS->itemState;
    CString strText;
    LOGFONT lf;
    lf = m_lf;

    CFont dispFont;
    CFont *pFont;
    //GetWindowText(strText);
    if (lpDIS->itemData != NULL)
    {
        strText = (((MENUDATA*) (lpDIS->itemData))->menuText);
        if ((((MENUDATA *)(lpDIS->itemData))->menuIconNormal) == -1)
            hIcon = NULL;
        else if (state & ODS_SELECTED)
        {
            if ((((MENUDATA *)(lpDIS->itemData))->menuIconSelected) != -1)
                hIcon = AfxGetApp ()->LoadIcon (((MENUDATA *)(lpDIS->itemData))->menuIconSelected);
            else
                hIcon = AfxGetApp()->LoadIcon (((MENUDATA*)(lpDIS->itemData))->menuIconNormal);
        }
        else
            hIcon = AfxGetApp()->LoadIcon (((MENUDATA*)(lpDIS->itemData))->menuIconNormal);

        TRACE1 ("Draw for %s\n", strText);
    }
    else
    {
        strText.Empty();
        hIcon = NULL;
    }

    if ( (state & ODS_SELECTED) )
    {
        // draw the down edges

        CPen *pOldPen = pDC->SelectObject (&m_penBack);

        //You need only Text highlight and thats what you get
        if (m_hilightStyle != Normal)
        {
            pDC->FillRect (rect,&m_brBackground);
        }
        else
        {
            pDC->FillRect (rect,&m_brSelect);
        }

        pDC->SelectObject (pOldPen);
        pDC->Draw3dRect (rect,GetSysColor (COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
        lf.lfWeight = FW_BOLD;
        if ((HFONT)dispFont != NULL)
            dispFont.DeleteObject ();
        dispFont.CreateFontIndirect (&lf);
        crText = m_clrHilightText;

        //While selected move the text a bit
        TRACE0 ("SELECT,SELECTED\n");
    }
    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

        TRACE0 ("SELECT, NORMAL\n");
    }

    // draw the text if there is any
    //We have to paint the text only if the image is nonexistant
    if (hIcon != NULL)
    {
        if(DrawIconEx (pDC->GetSafeHdc(),rect.left,rect.top,hIcon,
            (m_iconX)?m_iconX:32,(m_iconY)?m_iconY:32,0,NULL,DI_NORMAL))
            TRACE0("Wrote the icon successfully\n");
        else
            TRACE0 ("SORRY.NOGO\n");
    }
    //This is needed always so that we can have the space for check marks
    rect.left = rect.left +((m_iconX)?m_iconX:32);

    if ( !strText.IsEmpty())
    {
        // pFont->GetLogFont (&lf);

        int iOldMode = pDC->GetBkMode();

        pDC->SetBkMode( TRANSPARENT);

        pDC->SetTextColor( crText);

        pFont = pDC->SelectObject (&dispFont);
        TRACE1( "About To DrawText %s\n",strText);
        pDC->DrawText (strText,rect,DT_LEFT|DT_SINGLELINE|DT_VCENTER);
        TRACE0("Done\n");
        pDC->SetBkMode( iOldMode );
        pDC->SelectObject (pFont); //set it to the old font
    }
    dispFont.DeleteObject ();
}

void CCustomMenu::MeasureItem( LPMEASUREITEMSTRUCT lpMIS )
{
    CDC *pDC = AfxGetApp()->m_pMainWnd->GetDC();
    CFont* pFont = pDC->SelectObject (&m_fontMenu);
    int iconX = 0,iconY= 0;
    TEXTMETRIC tm;
    pDC->GetTextMetrics (&tm);
    pDC->SelectObject (pFont);
    AfxGetApp()->m_pMainWnd->ReleaseDC (pDC);

    if (m_iconX)
        iconX = m_iconX;
    if (m_iconY)
        iconY = m_iconY;

    lpMIS->itemWidth = iconX + tm.tmAveCharWidth * lstrlen(((MENUDATA*)(lpMIS->itemData))->menuText) +10;
    lpMIS->itemHeight = (iconY > (m_iMenuHeight+1))?iconY:m_iMenuHeight + 1;
}

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

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

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

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

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


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

上一页  [1] [2] [3] [4] 


  • 上一篇文章:

  • 下一篇文章:
  • 相关文章