如题
我想做个半透明的按钮
我现在用BLENDFUNCTION bm;
memset(&bm,0,sizeof(bm));
bm.BlendOp=AC_SRC_OVER;
bm.BlendFlags=0;
bm.SourceConstantAlpha = m_nalphaValue;
TRACE("%d\n",m_nalphaValue);
bm.AlphaFormat=0;  
AlphaBlend(*pDC,x,y,w,h,*memDC,xSource,ySource,w,h,bm);  来实现半透明的。 鼠标停留在按钮上时改变m_nalphaValue为200; 鼠标离开按钮是改变m_nalphaValue为50;
是在重写DrawItem中调用的。 现在的问题是,鼠标移动到按钮上时按钮能变亮了,离开按钮后没反应。 谁能帮帮我啊。谢谢了

解决方案 »

  1.   

    网上的CButtonST类好像有这个功能……
      

  2.   

    ON_WM_MOUSEMOVE(), OnMouseMove()中响应一下就可以发生反映了
      

  3.   

    离开按钮的时候,你要调用Invalidate刷新一下按钮的。
      

  4.   

    重新调用刷新函数了。 也响应mousemove消息了,可是就是实现不了
    void CHoverButtonEx::OnMouseMove(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    if (!m_bTracking)
    {
    TRACKMOUSEEVENT tme;
    tme.cbSize = sizeof(tme);
    tme.hwndTrack = m_hWnd;
    tme.dwFlags = TME_LEAVE|TME_HOVER;
    tme.dwHoverTime = 1;
    m_bTracking = _TrackMouseEvent(&tme);
    m_point = point;
    }
    CBitmapButton::OnMouseMove(nFlags, point);
    }
    //画透明的位图
    void CHoverButtonEx::DrawTransparentBitmap(CDC* pDC, int x,  int y,int w,  int h,CDC* memDC,
                                       int xSource, // = 0
                                       int ySource,int alphavalue)  // = 0)
    {   BLENDFUNCTION bm;
    memset(&bm,0,sizeof(bm));
    bm.BlendOp=AC_SRC_OVER;
    bm.BlendFlags=0;
    bm.SourceConstantAlpha = m_nalphaValue;
    TRACE("%d\n",m_nalphaValue);
    bm.AlphaFormat=0;  
    AlphaBlend(*pDC,x,y,w,h,*memDC,xSource,ySource,w,h,bm); }
    //鼠标碰撞到按钮
    LRESULT CHoverButtonEx::OnMouseHover(WPARAM wparam, LPARAM lparam) 
    {

    m_bHover=TRUE;

    Invalidate(); return 0;
    }
    //鼠标离开按钮
    LRESULT CHoverButtonEx::OnMouseLeave(WPARAM wparam, LPARAM lparam)
    {
    m_bTracking = FALSE;
    m_bHover=FALSE;
    Invalidate();
    return 0;
    }
      

  5.   

    这是从写的drawitem 函数void CHoverButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    if ((m_ButtonSize.cx == 0) || (m_ButtonSize.cy == 0)) 
    {
    UINT style = GetButtonStyle();
    style &= ~BS_OWNERDRAW;
    SetButtonStyle(style); 
    }
    CDC * mydc=CDC::FromHandle(lpDrawItemStruct->hDC);
    CDC * MemDC = new CDC;
    MemDC->CreateCompatibleDC(mydc);
    CBitmap * p0ldBitmap =  MemDC->SelectObject(&mybitmap);
    if(m_bHover)   //停留在按钮上
    {
    m_nalphaValue = 200;
    TRACE("%d\n",m_nalphaValue);
    //mydc->BitBlt(0,00,m_ButtonSize.cx,m_ButtonSize.cy,MemDC,21,0,SRCCOPY);
    DrawTransparentBitmap(mydc,0,0,m_ButtonSize.cx,m_ButtonSize.cy,MemDC,21,0,200);
    }else
    {
    m_nalphaValue = 50;
    TRACE("%d\n",m_nalphaValue);
    mydc->BitBlt(0,00,m_ButtonSize.cx,m_ButtonSize.cy,MemDC,42,0,SRCCOPY);
    //DrawTransparentBitmap(mydc,0,0,m_ButtonSize.cx,m_ButtonSize.cy,MemDC,42,0,200);
    } MemDC->SelectObject(p0ldBitmap);

    delete MemDC;
    /*delete MemDC;
    mydc->DeleteDC();*/
    }
      

  6.   

    http://www.codeproject.com/KB/buttons/GdipButton.aspx去看吧,记得给分~
      

  7.   


    大哥:我用的图是位图的,你给我个PNG的,我还要转换一下?