非常简单,基本散分的!欢迎欢迎!

yiruirui0507 2010-09-14 10:19:12
我想弄一个透明按纽,我查了资料,说重载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

/////////////////////////////////////////////////////////////////////////////
// CBTN

CBTN::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;
}
...全文
132 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lolliboy 2010-09-14
  • 打赏
  • 举报
回复
ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
你的map里面是小写on,
实现是On?
yiruirui0507 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 yuchongjike 的回复:]
重载一下按钮类,然后在OnEarseBkground里直接返回TRUE.
[/Quote]

说了直接返回不行了,你还继续说......?
yuchongjike 2010-09-14
  • 打赏
  • 举报
回复
重载一下按钮类,然后在OnEarseBkground里直接返回TRUE.
Eleven 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yiruirui0507 的回复:]
引用 13 楼 yuchongjike 的回复:
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,onEraseBk……
[/Quote]
return TRUE只是表示背景你处理过了,但并不是透明,一般这样写是为了防止闪烁。。。
yiruirui0507 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 yuchongjike 的回复:]
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()

……
[/Quote]
网上说这样就能变透明,刚测试了,没变透明啊!
yuchongjike 2010-09-14
  • 打赏
  • 举报
回复
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消息吧.在那里面处理
yiruirui0507 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 visualeleven 的回复:]
ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
-->
ON_WM_ERASEGKBND()

囧。。。。
[/Quote]

谢谢,你能告诉我为什么这里不是ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
而是ON_WN_ERASEBGKND 这里有和区别吗?
yiruirui0507 2010-09-14
  • 打赏
  • 举报
回复
还是出现问题啊,
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)
Eleven 2010-09-14
  • 打赏
  • 举报
回复
ON_COMMAND(IDC_BUTTON1,onEraseBkgnd)
-->
ON_WM_ERASEGKBND()

囧。。。。
yiruirui0507 2010-09-14
  • 打赏
  • 举报
回复
.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

/////////////////////////////////////////////////////////////////////////////
// CBTN

CBTN::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;
}
yiruirui0507 2010-09-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 visualeleven 的回复:]
手动添加不熟悉的话,还是用class wizard向导添加吧,熟悉以后再手动添加。。。
[/Quote]

改正后如下:

#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_)
iqyely 2010-09-14
  • 打赏
  • 举报
回复
来学习下
三壮 2010-09-14
  • 打赏
  • 举报
回复

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之间。。
dengzikun 2010-09-14
  • 打赏
  • 举报
回复
透明按扭 CButtonTR
Eleven 2010-09-14
  • 打赏
  • 举报
回复
手动添加不熟悉的话,还是用class wizard向导添加吧,熟悉以后再手动添加。。。
Eleven 2010-09-14
  • 打赏
  • 举报
回复
囧,消息映射写错了,不别的,你的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;
}
fandh 2010-09-14
  • 打赏
  • 举报
回复
楼主编译报错,你的错误是什么?贴出来!
yiruirui0507 2010-09-14
  • 打赏
  • 举报
回复
如果大家感觉上面麻烦的话,我简单总结一下:
.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!
lcyw 2010-09-14
  • 打赏
  • 举报
回复
建议 楼主先看看孙鑫的 《VC深入编程》

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧