书上有个实现组合框颜色选择器的例子,说把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];
}
头文件:#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];
}
解决方案 »
- [求教]能否hook win32api重定向http下载的文件到本机文件?
- 基于对话框应用程序右下角拖动特效问题(IE6的右下角的样子)
- 代码创建的控件为什么不能显示出来呢?
- 等待提示的小对话框的实现问题
- 谁能提供个网络较时NTP服务端的源码分享参考下
- linux 线程池的用法代码 !!!
- 大量收集黄健翔语录,特别是黄健翔的各种版本的意澳大战解说词!
- directshow中setting the video window移植后不能播放(分不够可以再加)
- MFC里有没有按扭控件提供鼠标划过按扭上方时产生一个事件的
- 自己实现MDI结构时遇到的问题之一
- 调用API:SHBrowseForFolder()选择文件夹总是报错,为什么?
- 高手帮帮忙呀 在对话框上怎么不见我会的图呀
我看看,应该是你使用的问题,而不是这两个文件的问题!
(1)-----((LPCTSTR)m_rgStandardColors[nColor]);
(2)-----dc.FillSolidRect(&lpDrawItemStruct->rcItem, (COLORREF)lpDrawItemStruct->itemData);
可以自己写一个RGB和str之间的转换关系来进行转换
太早地调用
InitializeData();这样在AddString的时候就会有问题!
1.Owner Draw选择Fixed//重载DrawItem
2.去掉Sort//避免重载CompareItem