我想将一个对话框按钮和一个CBitmapButton对象联系起来,以实现位图按钮,代码如下,可是没成功,按钮不可见,应该怎样修改呢?
m_bmbutton.LoadBitmaps(IDB_BITMAPUD,IDB_BITMAPU,IDB_BITMAPU,IDB_BITMAPUD);
m_bmbutton.AutoLoad(IDC_BUTTON,this); //IDB_BITMAPD等是资源中的4幅位图,m_bmbutton是对话框中与按钮相关联的变量,定义在 .h文件中
IDC_BUTTON 的Styles 设置为OwnerDraw
能不能给一个详细的示例代码

解决方案 »

  1.   

    // MyButton.cpp : implementation file
    //#include "stdafx.h"
    #include "MyButton.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CMyButtonCMyButton::CMyButton()
    {
    m_bStatus=FALSE;
    }CMyButton::~CMyButton()
    {
    }
    BEGIN_MESSAGE_MAP(CMyButton, CButton)
    //{{AFX_MSG_MAP(CMyButton)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyButton message handlersvoid CMyButton::SetBmp(char *sel, char *unsel)
    {
    if(m_hDibSel=LoadDIB(sel))
    {
    LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDibSel);
    ::GlobalUnlock((HGLOBAL) m_hDibSel);
    width=::DIBWidth(lpDIB);
    height=::DIBHeight(lpDIB);
    m_hDibUnsel=LoadDIB(unsel);
    }
    }void CMyButton::SetBmp(char *unsel, char *down, char *up)
    {
    if(unsel!=0)
    m_hDibUnsel=LoadDIB(unsel);
    if(down!=0)
    {
    m_hDibDown=LoadDIB(down);
    }
    if(up!=0)
    {
    m_hDibUp=LoadDIB(up);
    }
    LPSTR lpDIB = (LPSTR)::GlobalLock ((HGLOBAL) m_hDibUnsel);
    ::GlobalUnlock ((HGLOBAL) m_hDibUnsel);
    width=::DIBWidth(lpDIB);
    height=::DIBHeight(lpDIB);
    }void CMyButton::PreSubclassWindow() 
    {
    CButton::PreSubclassWindow();
    CRect rect(0,0,width,height);
    ClientToScreen(rect);
    CWnd* pParent = GetParent();
    if (pParent) 
    pParent->ScreenToClient(rect);
    MoveWindow(rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
    ModifyStyle(0,BS_OWNERDRAW);
    }void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    ASSERT(lpDrawItemStruct != NULL);
    CDC* pDC   = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect rect = lpDrawItemStruct->rcItem;
    UINT state = lpDrawItemStruct->itemState;
    UINT nStyle = GetStyle();
    int nSavedDC = pDC->SaveDC();
    if ((state & ODS_SELECTED))
    {
    HPALETTE m_hPalette;
    m_hPalette = GetSystemPalette ();
    if(m_hDibSel)
    PaintDIB(pDC->m_hDC,rect,m_hDibSel,rect,m_hPalette);
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(IsWindowEnabled()?0x00ff0000:0xa0a0a0);
    CString text;
    GetWindowText(text);
    pDC->DrawText(text,rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
    }
    else
    {
    HPALETTE m_hPalette;
    m_hPalette = GetSystemPalette ();
    if(m_hDibUnsel)
    PaintDIB(pDC->m_hDC,rect,m_hDibUnsel,rect,m_hPalette);
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(IsWindowEnabled()?0x00ff0000:0xa0a0a0);
    CString text;
    GetWindowText(text);
    pDC->DrawText(text,rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
    }
    pDC->RestoreDC(nSavedDC);
    }//-----------------------------------------------CMyButton.h#if !defined(AFX_MYBUTTON_H__4F3BB97D_1433_42E8_9572_5BDA3ABBA557__INCLUDED_)
    #define AFX_MYBUTTON_H__4F3BB97D_1433_42E8_9572_5BDA3ABBA557__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // MyButton.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CMyButton window
    #include "dibapi.h"
    class CMyButton : public CButton
    {
    // Construction
    public:
    CMyButton();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyButton)
    public:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    protected:
    virtual void PreSubclassWindow();
    //}}AFX_VIRTUAL// Implementation
    public:
    BOOL m_bStatus;
    void SetBmp(char *unsel,char * down,char * up);
    void SetBmp(char *sel,char *unsel);
    int width;
    int height;
    HDIB m_hDibUnsel;
    HDIB m_hDibSel;
    HDIB m_hDibDown;
    HDIB m_hDibUp;
    HDIB m_hCurrentDib;
    virtual ~CMyButton(); // Generated message map functions
    protected:
    //{{AFX_MSG(CMyButton)
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYBUTTON_H__4F3BB97D_1433_42E8_9572_5BDA3ABBA557__INCLUDED_)
      

  2.   

    不用,这是个正常,mousedown,mouseup三态的按钮,不知你能不能用上。