我是一个新手!
最近碰到一个问题,不知道如何使用图片来做按钮触发?

解决方案 »

  1.   

    可以把按钮设成图片按钮啊,(按钮properties->styles->bitmaps)
    而后在OnInitDialog()里添加如下代码
    CButton * aa=(CButton *)GetDlgItem(IDC_BUTTON_BEGIN);
    HBITMAP hbmp=(HBITMAP)::LoadImage(NULL,"button.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    aa->SetBitmap(hbmp);
    其中button.bmp为图片名
      

  2.   

    MFC 中就有 CBitmapButton 类。
      

  3.   

    #pragma once
    // CSkinButtonclass CSkinButton : public CButton
    {
    DECLARE_DYNAMIC(CSkinButton)public:
    CSkinButton();
    virtual ~CSkinButton();protected:
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
    DECLARE_MESSAGE_MAP()
    virtual void PreSubclassWindow();
    virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
    virtual void DrawBitmap(CDC* pDC, HBITMAP hBitmap, const CRect& rect);
    public:
    void SetTransparent(COLORREF crMask);
    void SetButtonBitmap(HBITMAP hNormal, HBITMAP hHover, HBITMAP hDown, BOOL bAutoSize=TRUE);
    protected:
    HBITMAP m_hNormal; // Normal state of bitmap
    HBITMAP m_hHover; // Mouse hover state of bitmap
    HBITMAP m_hDown; // Mouse Downed state of bitmap
    COLORREF m_crMask; // Transparent color
    BOOL m_bOver;
    BOOL m_bTracking;
    };/************************************************************
     * SKinButton.cpp : implementation file
     * Copyrith (C) LQ messager
     * Module Name: CSkinButton
     * Description: The class implement a simple skin button
     *
     * HomePage: http://iwantfly.meibu.com
     * Author:LiJun  Date:2006/08/1 update date:
     ************************************************************/
    #include "stdafx.h"
    #include "LQ.h"
    #include "SKinButton.h"
    // CSKinButtonIMPLEMENT_DYNAMIC(CSkinButton, CButton)
    CSkinButton::CSkinButton()
    {
    m_hNormal = NULL;
    m_hHover = NULL;
    m_hDown = NULL; m_bOver = FALSE;
    m_bTracking = FALSE;
    m_crMask = NULL;
    }CSkinButton::~CSkinButton()
    {
    if( m_hNormal )
    ::DeleteObject(m_hNormal);
    if( m_hHover )
    ::DeleteObject(m_hHover);
    if( m_hDown )
    ::DeleteObject(m_hDown);
    }
    BEGIN_MESSAGE_MAP(CSkinButton, CButton)
    ON_WM_MOUSEMOVE()
    ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
    ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
    END_MESSAGE_MAP()// CSKinButton message handlersvoid CSkinButton::PreSubclassWindow()
    {
    // TODO: Add your specialized code here and/or call the base class
    ModifyStyle(0, BS_OWNERDRAW);
    CButton::PreSubclassWindow();
    }void CSkinButton::SetButtonBitmap(HBITMAP hNormal, HBITMAP hHover, HBITMAP hDown, BOOL bAutoSize)
    {
    m_hNormal = hNormal;
    m_hHover  = hHover;
    m_hDown   = hDown; if( bAutoSize )
    {
    CRect rect;
    BITMAP bm;
    GetWindowRect(&rect);
    ::GetObject(m_hNormal, sizeof(BITMAP), &bm);
    rect.right  = rect.left + bm.bmWidth;
    rect.bottom = rect.top  + bm.bmHeight;
    GetParent()->ScreenToClient(&rect);
    MoveWindow(&rect);
    }
    }void CSkinButton::SetTransparent(COLORREF crMask)
    {
    m_crMask = crMask;
    }void CSkinButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    // TODO:  Add your code to draw the specified item
    ASSERT_VALID(this);
    CRect rect = lpDrawItemStruct->rcItem;
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    ASSERT(pDC != NULL);
    int nSaveDC = pDC->SaveDC();
    UINT nState = lpDrawItemStruct->itemState;
    CString strText;
    GetWindowText(strText);
    if( m_bOver )
    {
    if(nState & ODS_SELECTED)
    DrawBitmap(pDC, m_hDown, rect);
    else
    DrawBitmap(pDC, m_hHover, rect);
    }
    else
    {
    DrawBitmap(pDC, m_hNormal, rect);
    } if( strText.GetLength() != 0)
    {
    CFont* pFont = GetFont();
    CFont* pOldFont = pDC->SelectObject(pFont);
    CSize  szExtent = pDC->GetTextExtent(strText);
    CPoint pt;
    pt.x = rect.left + ((rect.Width() -  szExtent.cx) / 2);
    pt.y = rect.top  + ((rect.Height() - szExtent.cy) / 2); if(nState & ODS_SELECTED)
    pt.Offset(1, 1);
    int nBkMode = pDC->SetBkMode(TRANSPARENT); if(nState & ODS_DISABLED)
    pDC->DrawState(pt, szExtent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
    else
    pDC->DrawState(pt, szExtent, strText, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL); pDC->SetBkMode(nBkMode);
    }
    pDC->RestoreDC(nSaveDC);
    }void CSkinButton::DrawBitmap(CDC* pDC, HBITMAP hBitmap, const CRect& rect)
    {
    ASSERT(pDC != NULL && hBitmap != NULL);
    if( m_crMask != NULL)
    {
    DrawTransparentBitmap(pDC->GetSafeHdc(), hBitmap,
    (short)rect.left, (short)rect.top, m_crMask);
    }
    else
    {
    CDC MemDC;
    MemDC.CreateCompatibleDC(pDC);
    HBITMAP hOldBitmap = (HBITMAP)MemDC.SelectObject(hBitmap);
    pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
    &MemDC, 0, 0, SRCCOPY);
    MemDC.SelectObject(hOldBitmap);
    MemDC.DeleteDC();
    }
    }void CSkinButton::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);
    } CButton::OnMouseMove(nFlags, point);
    }LRESULT CSkinButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
    {
    m_bOver = TRUE;
    Invalidate();
    return 0;
    }LRESULT CSkinButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
    {
    m_bOver = FALSE;
    m_bTracking = FALSE;
    Invalidate(); return 0;
    }
      

  4.   

    还有一个全局函数:void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, short xStart,
                               short yStart, COLORREF cTransparentColor)
    {
       BITMAP     bm;
       COLORREF   cColor;
       HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;
       HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
       HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
       POINT      ptSize;   hdcTemp = CreateCompatibleDC(hdc);
       SelectObject(hdcTemp, hBitmap);   // Select the bitmap   GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
       ptSize.x = bm.bmWidth;            // Get width of bitmap
       ptSize.y = bm.bmHeight;           // Get height of bitmap
       DPtoLP(hdcTemp, &ptSize, 1);      // Convert from device                                     // to logical points   // Create some DCs to hold temporary data.
       hdcBack   = CreateCompatibleDC(hdc);
       hdcObject = CreateCompatibleDC(hdc);
       hdcMem    = CreateCompatibleDC(hdc);
       hdcSave   = CreateCompatibleDC(hdc);   // Create a bitmap for each DC. DCs are required for a number of
       // GDI functions.   // Monochrome DC
       bmAndBack   = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);   // Monochrome DC
       bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);   bmAndMem    = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
       bmSave      = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);   // Each DC must select a bitmap object to store pixel data.
       bmBackOld   = (HBITMAP)::SelectObject(hdcBack, bmAndBack);
       bmObjectOld = (HBITMAP)::SelectObject(hdcObject, bmAndObject);
       bmMemOld    = (HBITMAP)::SelectObject(hdcMem, bmAndMem);
       bmSaveOld   = (HBITMAP)::SelectObject(hdcSave, bmSave);   // Set proper mapping mode.
       SetMapMode(hdcTemp, GetMapMode(hdc));   // Save the bitmap sent here, because it will be overwritten.
       BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);   // Set the background color of the source DC to the color.
       // contained in the parts of the bitmap that should be transparent
       cColor = SetBkColor(hdcTemp, cTransparentColor);   // Create the object mask for the bitmap by performing a BitBlt
       // from the source bitmap to a monochrome bitmap.
       BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
              SRCCOPY);   // Set the background color of the source DC back to the original
       // color.
       SetBkColor(hdcTemp, cColor);   // Create the inverse of the object mask.
       BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
              NOTSRCCOPY);   // Copy the background of the main DC to the destination.
       BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
              SRCCOPY);   // Mask out the places where the bitmap will be placed.
       BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);   // Mask out the transparent colored pixels on the bitmap.
       BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);   // XOR the bitmap with the background on the destination DC.
       BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);   // Copy the destination to the screen.
       BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
              SRCCOPY);   // Place the original bitmap back into the bitmap sent here.
       BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);   // Delete the memory bitmaps.
       DeleteObject(SelectObject(hdcBack, bmBackOld));
       DeleteObject(SelectObject(hdcObject, bmObjectOld));
       DeleteObject(SelectObject(hdcMem, bmMemOld));
       DeleteObject(SelectObject(hdcSave, bmSaveOld));   // Delete the memory DCs.
       DeleteDC(hdcMem);
       DeleteDC(hdcBack);
       DeleteDC(hdcObject);
       DeleteDC(hdcSave);
       DeleteDC(hdcTemp);
    }
    用法:
    m_btnSkin.SubclassDlgItem(IDC_MYBUTTON, this);
    m_btnSkin.SetButtonBitmap(
    /* 正常的位图句柄 */,
    /* 鼠标停留的位图句柄*/, 
    /* 被按下的位图句枘*/
    );
      

  5.   

    搭车问一下,如果不添加资源,而是用外部的文件比如jpg作为显示的图片该怎么实现呢?
      

  6.   

    搭车问一下,如果不添加资源,而是用外部的文件比如jpg作为显示的图片该怎么实现呢?----------------------------------------------
    那就要从jpg转换到bmp.至于怎样转换暂时不作回答.
      

  7.   

    不能直接用JPG用来做背景的么?