我想弄一个透明按纽,我查了资料,说重载OnEraseBkgnd,直接return   TURE。 就OK.我是这样实现的,首先创建了一个基于对话框的工程,在上面增加了一个BUTTON,属性:ID IDC_BUTTON1
然后我用类向导给他添加了一个类,CBTN,
在CBTN的头文件中进行了OnEraseBkgnd函数的声明,然后进行了消息映射,这里是我手动添加的(我点CBTN没找到增加WINDOWS消息处理),代码如下,编译出错.
头文件#if !defined(AFX_BTN_H__83C624F5_5E7A_4931_91CB_3C714DCF3371__INCLUDED_)
#define AFX_BTN_H__83C624F5_5E7A_4931_91CB_3C714DCF3371__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// BTN.h : header file
///////////////////////////////////////////////////////////////////////////////
// CBTN window
class CBTN : public CButton
{
// Construction
public:
CBTN();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBTN)
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CBTN();
    bool OnEraseBkgnd(CDC *pDC);
// Generated message map functions
protected:
//{{AFX_MSG(CBTN)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
afx_msg bool OnEraseBkgnd(CDC *pDC);
     DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_BTN_H__83C624F5_5E7A_4931_91CB_3C714DCF3371__INCLUDED_).CPP如下
// BTN.cpp : implementation file
//#include "stdafx.h"
#include "2.h"
#include "BTN.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CBTNCBTN::CBTN()
{
}CBTN::~CBTN()
{
}
BEGIN_MESSAGE_MAP(CBTN, CButton)
//{{AFX_MSG_MAP(CBTN)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)/////////////////////////////////////////////////////////////////////////////
// CBTN message handlers
bool CButton::OnEraseBkgnd(CDC *pDC)
{
return true;
}

解决方案 »

  1.   

    如果大家感觉上面麻烦的话,我简单总结一下:
    .h文件
    bool OnEraseBkgnd(CDC *pDC);
    afx_msg bool OnEraseBkgnd(CDC *pDC);
    .cpp文件
    ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)bool CButton::OnEraseBkgnd(CDC *pDC)
    {
    return true;
    }就这四句!大家看看是哪里出了什么问题?先说3KS!
      

  2.   

    囧,消息映射写错了,不别的,你的ON_COMMAND怎么写到END_MESSAGE_MAP下面去了呢?//H头文件中
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);//CPP文件
    BEGIN_MESSAGE_MAP(CBTN, CButton)
     ON_WM_ERASEGKBND()
    END_MESSAGE_MAP()BOOL CBTN::OnEraseBkgnd(CDC *pDC)
    {
    return TRUE;
    }
      

  3.   

    手动添加不熟悉的话,还是用class wizard向导添加吧,熟悉以后再手动添加
      

  4.   


    BEGIN_MESSAGE_MAP(CBTN, CButton)
    //{{AFX_MSG_MAP(CBTN)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
    消息映射应该在BEGIN_MESSAGE_MAP和END_MESSAGE_MAP之间。。
      

  5.   


    改正后如下:#if !defined(AFX_BTN_H__83C624F5_5E7A_4931_91CB_3C714DCF3371__INCLUDED_)
    #define AFX_BTN_H__83C624F5_5E7A_4931_91CB_3C714DCF3371__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // BTN.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CBTN window
    class CBTN : public CButton
    {
    // Construction
    public:
    CBTN();// Attributes
    public:// Operations
    public:
    BOOL onEraseBkgnd(CDC *pDC);
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CBTN)
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CBTN();// Generated message map functions
    protected:
    //{{AFX_MSG(CBTN)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
     DECLARE_MESSAGE_MAP()
    }; ///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_BTN_H__83C624F5_5E7A_4931_91CB_3C714DCF3371__INCLUDED_)
      

  6.   

    .CPP如下
    // BTN.cpp : implementation file
    //#include "stdafx.h"
    #include "2.h"
    #include "BTN.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CBTNCBTN::CBTN()
    {
    }CBTN::~CBTN()
    {
    }
    BEGIN_MESSAGE_MAP(CBTN, CButton)
    //{{AFX_MSG_MAP(CBTN)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CBTN message handlers
    BOOL CBTN::OnEraseBkgnd(CDC *pDC)
    {
    return TRUE;
    }
      

  7.   

    ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
    -->
    ON_WM_ERASEGKBND()囧。
      

  8.   

    还是出现问题啊,
    Compiling...
    BTN.cpp
    Linking...
    BTN.obj : error LNK2001: unresolved external symbol "public: int __thiscall CBTN::onEraseBkgnd(class CDC *)" (?onEraseBkgnd@CBTN@@QAEHPAVCDC@@@Z)
    Debug/2.exe : fatal error LNK1120: 1 unresolved externals
    执行 link.exe 时出错.2.exe - 1 error(s), 0 warning(s)
      

  9.   


    谢谢,你能告诉我为什么这里不是ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
    而是ON_WN_ERASEBGKND  这里有和区别吗?
      

  10.   

    BEGIN_MESSAGE_MAP(CBTN, CButton)
    //{{AFX_MSG_MAP(CBTN)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CBTN message handlers
    BOOL CBTN::OnEraseBkgnd(CDC *pDC)
    {
    return TRUE;
    }你这个函数什么都没操作,只是返回了一个true.
    清除背景你总要触发erasebkgnd消息吧.在那里面处理
      

  11.   

    return TRUE只是表示背景你处理过了,但并不是透明,一般这样写是为了防止闪烁
      

  12.   

    重载一下按钮类,然后在OnEarseBkground里直接返回TRUE. 
      

  13.   

    ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
    你的map里面是小写on,
    实现是On?