书上有个实现组合框颜色选择器的例子,说把ComboColorPicker.h和ComboColorPicker.cpp文件包含在你的工程文件中,并用VC的ClassWizard创建一个控件成员变量,我照做了,但一运行就出错,不知道问题出在哪儿。这两个文件代码应该没有错,就是不知道该怎样调用才能看到结果。请朋友们帮忙看看。这两个文件如下:
头文件:#if !defined(AFX_COMBOCOLORPICKER_H__B2348841_5541_11D1_8756_00A0C9181E86__INCLUDED_)
#define AFX_COMBOCOLORPICKER_H__B2348841_5541_11D1_8756_00A0C9181E86__INCLUDED_#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// ComboColorPicker.h : header file
// ? 1997 Baldvin Hansson/////////////////////////////////////////////////////////////////////////////
// CComboColorPicker windowclass CComboColorPicker : public CComboBox
{
// Construction
public:
CComboColorPicker();// Attributes
private:
bool m_bInitialized;
static COLORREF m_rgStandardColors[];
public:// Operations
private:
void InitializeData();
public:
COLORREF GetSelectedColor();// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CComboColorPicker)
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);// Implementation
public:
virtual ~CComboColorPicker(); // Generated message map functions
protected:
//{{AFX_MSG(CComboColorPicker)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.#endif // !defined(AFX_COMBOCOLORPICKER_H__B2348841_5541_11D1_8756_00A0C9181E86__INCLUDED_)
/////////////////////////////////////////////////////////////////////////////
实现文件:
// ComboColorPicker.cpp : implementation file
// ? 1997 Baldvin Hansson#include "stdafx.h"
#include "ComboColorPicker.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CComboColorPickerCOLORREF CComboColorPicker::m_rgStandardColors[] = {
RGB(0, 0, 0), // Black
RGB(255, 255, 255), // White
RGB(128, 0, 0), // Dark Red
RGB(0, 128, 0), // Dark Green
RGB(128, 128, 0), // Dark Yellow
RGB(0, 0, 128), // Dark Blue
RGB(128, 0, 128), // Dark Magenta
RGB(0, 128, 128), // Dark Cyan
RGB(192, 192, 192), // Light Grey
RGB(128, 128, 128), // Dark Grey
RGB(255, 0, 0), // Red
RGB(0, 255, 0), // Green
RGB(255, 255, 0), // Yellow
RGB(0, 0, 255), // Blue
RGB(255, 0, 255), // Magenta
RGB(0, 255, 255), // Cyan
};CComboColorPicker::CComboColorPicker()
{
m_bInitialized = false;
}CComboColorPicker::~CComboColorPicker()
{
}
BEGIN_MESSAGE_MAP(CComboColorPicker, CComboBox)
//{{AFX_MSG_MAP(CComboColorPicker)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CComboColorPicker message handlersint CComboColorPicker::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CComboBox::OnCreate(lpCreateStruct) == -1)
return -1; InitializeData(); return 0;
}void CComboColorPicker::PreSubclassWindow()
{
InitializeData(); CComboBox::PreSubclassWindow();
}void CComboColorPicker::InitializeData()
{
int nItem; if (m_bInitialized)
return; for (int nColor = 0; nColor < sizeof(m_rgStandardColors)/sizeof(COLORREF); nColor++)
{
nItem = AddString((LPCTSTR)m_rgStandardColors[nColor]); // Here we could affect the sort order at run-time if (CB_ERRSPACE == nItem)
break;
} m_bInitialized = true;
}void CComboColorPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
CBrush brushBlack;
brushBlack.CreateStockObject(BLACK_BRUSH); if (!dc.Attach(lpDrawItemStruct->hDC))
return; COLORREF rgbTextColor = dc.GetTextColor();
COLORREF rgbBkColor = dc.GetBkColor(); if (lpDrawItemStruct->itemAction & ODA_FOCUS)
{
dc.DrawFocusRect(&lpDrawItemStruct->rcItem);
}
else if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
{
if (lpDrawItemStruct->itemState & ODS_FOCUS)
dc.DrawFocusRect(&lpDrawItemStruct->rcItem);
else
dc.ExtTextOut(0, 0, ETO_OPAQUE, &lpDrawItemStruct->rcItem, _T(""), 0, NULL);
} if (0 <= (int)lpDrawItemStruct->itemID) // Any item selected?
{
::InflateRect(&lpDrawItemStruct->rcItem, -2, -2); dc.FillSolidRect(&lpDrawItemStruct->rcItem, (COLORREF)lpDrawItemStruct->itemData);
dc.FrameRect(&lpDrawItemStruct->rcItem, &brushBlack);
} // Restore the DC state
dc.SetTextColor(rgbTextColor);
dc.SetBkColor(rgbBkColor);
dc.Detach();
}COLORREF CComboColorPicker::GetSelectedColor()
{
int nItem = GetCurSel();
if (CB_ERR == nItem)
return RGB(0, 0, 0); // Default to black if nothing is selected return m_rgStandardColors[nItem];
}

解决方案 »

  1.   

    程序太长了,将你完整的程序发到:[email protected]
    我看看,应该是你使用的问题,而不是这两个文件的问题!
      

  2.   

    出什么错误?我估计下面的两处可能出错:
    (1)-----((LPCTSTR)m_rgStandardColors[nColor]);
    (2)-----dc.FillSolidRect(&lpDrawItemStruct->rcItem, (COLORREF)lpDrawItemStruct->itemData);
    可以自己写一个RGB和str之间的转换关系来进行转换
      

  3.   

    还有一个可能的原因是
    太早地调用
    InitializeData();这样在AddString的时候就会有问题!
      

  4.   

    这样的问题应该很简单的在你的对话框上放一个组合框控件,然后为它添加一个CComboColorPicker类型的控制变量在你的对话框初始化函数中调用这个变量的PreSubclassWindow()函数就可以了
      

  5.   

    vcleaner(我没当大哥很久了.......),邮件已经发给你了,标题为:“你好,麻烦你看看这个程序问题。”
      

  6.   

    legendhui(何离心之可同兮,吾将远逝以自疏) :调用不了,编译出错。
      

  7.   

    程序给你发回去了,你的程序太乱,我给你写了一个Demo。这个类不是很好使,资源编辑的时候必须将Combo控件Type选择为“Drop List”,Owner Draw选择为“Fixed”,否则就会出错。它虽然重载的PreSubClassWindow函数,但是它没有判断原来的风格,也没有修改风格。呵呵。自己看看我给你写的程序吧。
      

  8.   

    谢谢vcleaner(我没当大哥很久了.......):能否简单解释一下为什么不选择Combo类型的时候会出错呢?
      

  9.   

    vcleaner(我没当大哥很久了.......):还有,我按照你的方法改了我原来的程序,也能运行了,但下拉时候没有出现滚动条,只有上下箭头,不知道问题出在哪里。
      

  10.   

    改两处即可
    1.Owner Draw选择Fixed//重载DrawItem
    2.去掉Sort//避免重载CompareItem