大家好,这个问题搞了我两天了,本人菜鸟。我建立了一个基于对话框的一个类。想做的功能是对话框界面有若干小格,点击小格会出现颜色对话框,选择颜色后,按“确定”界面里的小格就变成所选的颜色。
以下是重要代码:
响应函数代码:
void CColorButton::OnClicked() 
{
// TODO: Add your control notification handler code here
CColorDialog ColorDlg;
ColorDlg.DoModal();  //显示颜色选择框
COLORREF thisColor=ColorDlg.GetColor();//获取选择的颜色
if(thisColor=NULL)//判断是否为空
return;
m_Color=thisColor;//记录新颜色
this->Invalidate(true);
}
类的原型:
class CColorButton : public CButton
{
// Construction
public:
CColorButton(COLORREF color=RGB(255,255,255));
public:
// void OnClicked();
virtual ~CColorButton();
private:
COLORREF m_Color; DECLARE_MESSAGE_MAP()
};
重载函数:
void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
// TODO: Add your code to draw the specified item
CRect rect,Clrect;
rect=lpDrawItemStruct->rcItem;
CDC* pDC=::CDC::FromHandle(lpDrawItemStruct->hDC);
CBrush brush;
brush.CreateSolidBrush(m_Color);//将用户选的颜色载入笔刷
Clrect.SetRect(rect.left+2,rect.top+2,rect.right-2,rect.bottom-2);
//绘制3D边框
pDC->Draw3dRect(&rect,GetSysColor(COLOR_3DSHADOW),GetSysColor(COLOR_3DHILIGHT));
pDC->FillRect(&Clrect,&brush);
brush.DeleteObject();
}
完了,我在对话框设置一个按钮上面的Owner draw我打了勾,我把这个按钮映射在
OnClicked     ON_IDC_BUTTON1:BN_CLICKED  但我运行程序就看不到按钮了,界面什么都没有
请高手指教!看问题发生在什么情况上,就是
/////////////////////////////////////////////////////////////////////////////
// CColorButton
CColorButton::CColorButton()
{
}CColorButton::~CColorButton()
{
}
是自动生成的,调试时会报错,但删除了就不报错了。运行不显示按钮。

解决方案 »

  1.   

    // ColorButton.cpp : implementation file
    //#include "stdafx.h"
    #include "ColorButton.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CColorButtonCColorButton::CColorButton(COLORREF color /*= RGB(255,255,255)*/)
    {
    m_Color = color;
    }CColorButton::~CColorButton()
    {
    }
    BEGIN_MESSAGE_MAP(CColorButton, CButton)
    //{{AFX_MSG_MAP(CColorButton)
    ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CColorButton message handlersvoid CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    // TODO: Add your code to draw the specified item
    CRect rect,Clrect; 
    rect=lpDrawItemStruct->rcItem; 
    CDC* pDC=::CDC::FromHandle(lpDrawItemStruct->hDC); 
    CBrush brush; 
    brush.CreateSolidBrush(m_Color);//将用户选的颜色载入笔刷 
    Clrect.SetRect(rect.left+2,rect.top+2,rect.right-2,rect.bottom-2); 
    //绘制3D边框 
    pDC->Draw3dRect(&rect,GetSysColor(COLOR_3DSHADOW),GetSysColor(COLOR_3DHILIGHT)); 
    pDC->FillRect(&Clrect,&brush); 
    brush.DeleteObject(); 
    }void CColorButton::OnClicked() 
    {
    // TODO: Add your control notification handler code here
    m_Color = RGB(255,0,0);
    }你自己试试吧,点击按钮后,是会变色的。
    自动生成的构造函数会报错,是因为你修改了构造函数的定义,加了一个COLORREF color参数,但却没有修改函数实现,造成了定义和实现不符。
      

  2.   

    // ColorButton.cpp : implementation file 
    // #include "stdafx.h" 
    #include "ColorButton.h" #ifdef _DEBUG 
    #define new DEBUG_NEW 
    #undef THIS_FILE 
    static char THIS_FILE[] = __FILE__; 
    #endif ///////////////////////////////////////////////////////////////////////////// 
    // CColorButton CColorButton::CColorButton(COLORREF color /*= RGB(255,255,255)*/) 

    m_Color = color; 
    } CColorButton::~CColorButton() 


    BEGIN_MESSAGE_MAP(CColorButton, CButton) 
    //{{AFX_MSG_MAP(CColorButton) 
    ON_CONTROL_REFLECT(BN_CLICKED, OnClicked) 
    //}}AFX_MSG_MAP 
    END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// 
    // CColorButton message handlers void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)  

    // TODO: Add your code to draw the specified item 
    CRect rect,Clrect;  
    rect=lpDrawItemStruct->rcItem;  
    CDC* pDC=::CDC::FromHandle(lpDrawItemStruct->hDC);  
    CBrush brush;  
    brush.CreateSolidBrush(m_Color);//将用户选的颜色载入笔刷  
    Clrect.SetRect(rect.left+2,rect.top+2,rect.right-2,rect.bottom-2);  
    //绘制3D边框  
    pDC->Draw3dRect(&rect,GetSysColor(COLOR_3DSHADOW),GetSysColor(COLOR_3DHILIGHT));  
    pDC->FillRect(&Clrect,&brush);  
    brush.DeleteObject();  
    } void CColorButton::OnClicked()  

    // TODO: Add your control notification handler code here 
    m_Color = RGB(255,0,0); 

    你自己试试吧,点击按钮后,是会变色的。 
    自动生成的构造函数会报错,是因为你修改了构造函数的定义,加了一个COLORREF color参数,但却没有修改函数实现,造成了定义和实现不符。
      

  3.   

    请问一下你的
    ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
    是怎么实现的呀?
    我在对话框添加的这个按钮,在CColorButton类中找不到object ID 不知道为什么?
    通过class Wizard 设置的么?