(1)????? 颜色按钮类和颜色对话框

1) 颜色对话框

MFC提供了颜色对话框类CMFCColorDialog进行颜色的选择,系统可以利用DoModal()调用,然后选择相应的颜色。

CMFCColorDialog dlg(m_Color,0,?this);

?????????if(dlg.DoModal()==IDOK)

???????? {

?????????????????? m_Color = dlg.GetColor();

????????????????? m_ColorDlgMsg.Format(_T("%d,%d,%d"),GetRValue(m_Color),GetGValue(m_Color),GetBValue(m_Color));

?????????????????? UpdateData(FALSE);

???????? }

2) 颜色按钮

可以利用颜色按钮选择相应的颜色,具有直观,在资源中增加按钮控件,并定义为CMFCColorButton,利用该类的函数初始化。

初始化:

???????? m_PickerBut.EnableAutomaticButton(_T("颜?色|?选?择?"),RGB(255, 0, 255));

???????? m_PickerBut.EnableOtherButton(_T("其?它¨1"));

???????? m_PickerBut.SetColor((COLORREF)-1);

???????? m_PickerBut.SetColumnsNumber(10);

颜色选择,当按下按钮时,可以获得颜色值:

COLORREFcolor = m_ColorPicker.GetColor();

?????????if?(color == -1)

???????? {

?????????????????? color= m_ColorPicker.GetAutomaticColor();

???????? }

???????? m_strRGB.Format(_T("%d,%d,%d"), GetRValue(color), GetGValue(color),GetBValue(color));

3) 颜色条控件

在资源中加入Picture控件,建立颜色条控件的边界,然后利用此范围建立CMFCColorBar,利用CMFCColorBar的操作函数建立并设置属性。

???????? m_ColorBarFrame.GetClientRect(&rectColorBar);????//获取区域

???????? m_ColorBarFrame.MapWindowPoints(this,&rectColorBar);???//将区域映射到窗体坐标

???????? m_wndColorBar.SetHorzMargin(0);

???????? m_wndColorBar.SetVertMargin(0);

???????? m_wndColorBar.EnableOtherButton(_T("其?它¨1"));

???????? m_wndColorBar.CreateControl(this,&rectColorBar,IDC_COLOR_BAR,5);

???????? m_wndColorBar.SetColor(RGB(0,0,0));

?建立鼠标单击消息并获取颜色信息

COLORREFcolor = m_wndColorBar.GetColor();

???????? m_ColorBarMsg.Format(_T("%d,%d,%d"), GetRValue(color),GetGValue(color), GetBValue(color));

???????? UpdateData(FALSE);

为了建立以调色板为基础的颜色条,首先需要建立调色板,在颜色条创建时利用调色板

//调色板的建立

#define?NUM_COLOURS 64

?????????struct

???????? {

?????????????????? LOGPALETTE??? LogPalette;

?????????????????? PALETTEENTRY? PalEntry[NUM_COLOURS];

???????? }pal;

?

???????? LOGPALETTE*pLogPalette = (LOGPALETTE*) &pal;

???????? pLogPalette->palVersion??? = 0x300;

???????? pLogPalette->palNumEntries= (WORD) NUM_COLOURS;

?

???????? COLORREFcolorStart = RGB(0, 255, 255);

???????? COLORREFcolorFinish = RGB(255, 255, 255);

?????????int?nShift = 6;

?????????for?(int?i = 0; i< NUM_COLOURS; i++)

???????? {

?????????????????? BYTEbR = (BYTE)((GetRValue(colorStart) *(NUM_COLOURS - i) + GetRValue(colorFinish)* i) >> nShift);

?????????????????? BYTEbG = (BYTE)((GetGValue(colorStart) *(NUM_COLOURS - i) + GetGValue(colorFinish)* i) >> nShift);

?????????????????? BYTEbB = (BYTE)((GetBValue(colorStart) *(NUM_COLOURS - i) + GetBValue(colorFinish)* i) >> nShift);

?

?????????????????? pLogPalette->palPalEntry[i].peRed?? = bR;

?????????????????? pLogPalette->palPalEntry[i].peGreen= bG;

?????????????????? pLogPalette->palPalEntry[i].peBlue? = bB;

?????????????????? pLogPalette->palPalEntry[i].peFlags= 0;

???????? }

????//颜色条的创建

???????? m_palColorPicker.CreatePalette(pLogPalette);

???????? m_wndColorBar2.SetHorzMargin(0);

???????? m_wndColorBar2.SetVertMargin(0);

???????? m_ColorBarFramw2.GetClientRect(&rectColorBar);

???????? m_ColorBarFramw2.MapWindowPoints(this, &rectColorBar);

???????? m_wndColorBar2.CreateControl(this, rectColorBar, IDC_COLOR_BAR2, 8?/* columns */, &m_palColorPicker);

???????? m_wndColorBar2.SetColor(colorStart);

颜色按钮的界面

转载于:https://www.sxzhongrui.com/DreamRecorder/p/9272640.html

过程:

1)使用别人已经写好的类,CColorListBox 完成。其实过程很简单,只是在vs2010 中需要注意一些细节:

? ?首先,在新建的项目中加入这两个类;

? ?然后, ?在vs对话框中添加CListBox m_list变量,然后将CListBox改为CColorListBox,这样就可以在点击BUTTON响应事件中,添加代码

? ?m_list.AddString(_T("nihao"),RGB(255,0,0));

? 只是这样,结果并不会想想象的出现文本颜色的变化。

? 需要做的是:修改m_list的属性:

? ? ? ? ? ?*)Owner Draw ?为Variable?

? ? ? ? ? ?*)Selction 为Single

? ? ? ? ? ?*)use TabStops 为False

? ? ? ? ? ?*)Has Strings 为True 这个是我在运行是,一直没有找到的出问题的地方

开始的时候设置的为False 但是怎么都不行,就单步调试,跟进去看代码,存入的是这个值,怎么都不行,再看它的重绘,取出的值就是不对,就像可能是list的属性设置不正确,由于属性不正确,它不去取你设置的值,所以就看vc6.0的代码,选中了又字符串,默认的是False 所以要选中!!!!

/*****************************************************************

CColorListBox.h

#if !defined(AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_) #define AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 // ColorListBox.h : header file //------------------------------------------------------------------- // // CColorListBox class -? // A CListBox-derived class with optional colored items. // // Version: 1.0 01/10/1998 Copyright ?Patrice Godard // // Version: 2.0 09/17/1999 Copyright ?Paul M. Meidinger // //------------------------------------------------------------------- / // CColorListBox window class CColorListBox : public CListBox { // Construction public: CColorListBox(); // Attributes public: // Operations public: int AddString(LPCTSTR lpszItem); // Adds a string to the list box int AddString(LPCTSTR lpszItem, COLORREF rgb); // Adds a colored string to the list box int InsertString(int nIndex, LPCTSTR lpszItem); // Inserts a string to the list box int InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb); // Inserts a colored string to the list box void SetItemColor(int nIndex, COLORREF rgb); // Sets the color of an item in the list box // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CColorListBox) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct); //}}AFX_VIRTUAL // Implementation public: virtual ~CColorListBox(); // Generated message map functions protected: //{{AFX_MSG(CColorListBox) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; / //{{AFX_INSERT_LOCATION}} // Microsoft Developer Studio will insert additional declarations immediately before the previous line. #endif // !defined(AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_)

/***************************************************************************************************************************************

CColorListBox.cpp

// ColorListBox.cpp : implementation file //------------------------------------------------------------------- // // CColorListBox class -? // A CListBox-derived class with optional colored items. // // Version: 1.0 01/10/1998 Copyright ?Patrice Godard // // Version: 2.0 09/17/1999 Copyright ?Paul M. Meidinger // //------------------------------------------------------------------- #include "stdafx.h" #include "ColorListBox.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif / // CColorListBox //------------------------------------------------------------------- // CColorListBox::CColorListBox() // // Return Value: None. // // Parameters : None. // // Remarks : Standard constructor. // { } // CColorListBox //------------------------------------------------------------------- // CColorListBox::~CColorListBox() // // Return Value: None. // // Parameters : None. // // Remarks : Destructor. // { } // ~CColorListBox() BEGIN_MESSAGE_MAP(CColorListBox, CListBox) //{{AFX_MSG_MAP(CColorListBox) //}}AFX_MSG_MAP END_MESSAGE_MAP() / // CColorListBox message handlers //------------------------------------------------------------------- // void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)? // // Return Value: None. // // Parameters : lpDIS - A long pointer to a DRAWITEMSTRUCT structure? // that contains information about the type of drawing required. // // Remarks : Called by the framework when a visual aspect of? // an owner-draw list box changes.? // { if ((int)lpDIS->itemID < 0) return;? CDC* pDC = CDC::FromHandle(lpDIS->hDC); COLORREF crText; CString sText; COLORREF crNorm = (COLORREF)lpDIS->itemData; // Color information is in item data. COLORREF crHilite = RGB(255-GetRValue(crNorm), 255-GetGValue(crNorm), 255-GetBValue(crNorm)); // If item has been selected, draw the highlight rectangle using the item's color. if ((lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) { CBrush brush(crNorm); pDC->FillRect(&lpDIS->rcItem, &brush); } // If item has been deselected, draw the rectangle using the window color. if (!(lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & ODA_SELECT)) { CBrush brush(::GetSysColor(COLOR_WINDOW)); pDC->FillRect(&lpDIS->rcItem, &brush); } // If item has focus, draw the focus rect. if ((lpDIS->itemAction & ODA_FOCUS) && (lpDIS->itemState & ODS_FOCUS)) pDC->DrawFocusRect(&lpDIS->rcItem);? // If item does not have focus, redraw (erase) the focus rect. if ((lpDIS->itemAction & ODA_FOCUS) && !(lpDIS->itemState & ODS_FOCUS)) pDC->DrawFocusRect(&lpDIS->rcItem);? // Set the background mode to TRANSPARENT to draw the text. int nBkMode = pDC->SetBkMode(TRANSPARENT); // If the item's color information is set, use the highlight color // gray text color, or normal color for the text. if (lpDIS->itemData) { if (lpDIS->itemState & ODS_SELECTED) crText = pDC->SetTextColor(crHilite); else if (lpDIS->itemState & ODS_DISABLED) crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT)); else crText = pDC->SetTextColor(crNorm); } // Else the item's color information is not set, so use the // system colors for the text. else { if (lpDIS->itemState & ODS_SELECTED) crText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT)); else if (lpDIS->itemState & ODS_DISABLED) crText = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT)); else crText = pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT)); } // Get and display item text. GetText(lpDIS->itemID, sText); CRect rect = lpDIS->rcItem; // Setup the text format. UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER; if (GetStyle() & LBS_USETABSTOPS) nFormat |= DT_EXPANDTABS; // Calculate the rectangle size before drawing the text. pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT); pDC->DrawText(sText, -1, &rect, nFormat); pDC->SetTextColor(crText);? pDC->SetBkMode(nBkMode); } // DrawItem //------------------------------------------------------------------- // void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS) // // Return Value: None. // // Parameters : lpMIS - A long pointer to a? // MEASUREITEMSTRUCT structure. // // Remarks : Called by the framework when a list box with? // an owner-draw style is created.? // { // ### Is the default list box item height the same as // the menu check height??? lpMIS->itemHeight = ::GetSystemMetrics(SM_CYMENUCHECK); } // MeasureItem //------------------------------------------------------------------- // int CColorListBox::AddString(LPCTSTR lpszItem) // // Return Value: The zero-based index to the string in the list box.? // The return value is LB_ERR if an error occurs; the? // return value is LB_ERRSPACE if insufficient space? // is available to store the new string. // // Parameters : lpszItem - Points to the null-terminated? // string that is to be added. // // Remarks : Call this member function to add a string to a list? // box. Provided because CListBox::AddString is NOT // a virtual function. // { return ((CListBox*)this)->AddString(lpszItem); } // AddString //------------------------------------------------------------------- // int CColorListBox::AddString(LPCTSTR lpszItem, COLORREF rgb) // // Return Value: The zero-based index to the string in the list box.? // The return value is LB_ERR if an error occurs; the? // return value is LB_ERRSPACE if insufficient space? // is available to store the new string. // // Parameters : lpszItem - Points to the null-terminated? // string that is to be added. // rgb - Specifies the color to be associated with the item. // // Remarks : Call this member function to add a string to a list? // box with a custom color. // { int nItem = AddString(lpszItem); if (nItem >= 0) SetItemData(nItem, rgb); return nItem; } // AddString //------------------------------------------------------------------- // int CColorListBox::InsertString(int nIndex, LPCTSTR lpszItem) // // Return Value: The zero-based index of the position at which the? // string was inserted. The return value is LB_ERR if? // an error occurs; the return value is LB_ERRSPACE if? // insufficient space is available to store the new string. // // Parameters : nIndex - Specifies the zero-based index of the position // to insert the string. If this parameter is ?, the string // is added to the end of the list. // lpszItem - Points to the null-terminated string that? // is to be inserted. // // Remarks : Inserts a string into the list box. Provided because? // CListBox::InsertString is NOT a virtual function. // { return ((CListBox*)this)->InsertString(nIndex, lpszItem); } // InsertString //------------------------------------------------------------------- // int CColorListBox::InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb) // // Return Value: The zero-based index of the position at which the? // string was inserted. The return value is LB_ERR if? // an error occurs; the return value is LB_ERRSPACE if? // insufficient space is available to store the new string. // // Parameters : nIndex - Specifies the zero-based index of the position // to insert the string. If this parameter is ?, the string // is added to the end of the list. // lpszItem - Points to the null-terminated string that? // is to be inserted. // rgb - Specifies the color to be associated with the item. // // Remarks : Inserts a colored string into the list box. // { int nItem = ((CListBox*)this)->InsertString(nIndex,lpszItem); if (nItem >= 0) SetItemData(nItem, rgb); return nItem; } // InsertString //------------------------------------------------------------------- // void CColorListBox::SetItemColor(int nIndex, COLORREF rgb) // // Return Value: None. // // Parameters : nIndex - Specifies the zero-based index of the item. // rgb - Specifies the color to be associated with the item. // // Remarks : Sets the 32-bit value associated with the specified // item in the list box. // { SetItemData(nIndex, rgb); RedrawWindow(); } // SetItemColor

vs2010编辑器的字体和颜色设置好,编写程序的时候感觉会很爽,下面就跟大家介绍下个人喜欢的设置参数

背景:黑色

关键字:绿色

类:橙色

变量:黄色

值类型:蓝色

字符串:紫色

数值:白色

函数:浅红色

注释:灰白色

选中:紫色

效果如下:

把下面的设置文件保存成xxx.vssettings文件,然后打开vs,然后点:工具-导入和导出设置向导-导入选定的环境设置,然后选择这个xxx.vssettings文件即可

?

对应的配置文件如下:

https://www.sxzhongrui.com/linxinfa/Visual-Studio/master/vs2010.vssettings

?