颜色C ++的单选按钮

时间:2018-07-21 12:10:52

标签: button radio

您好,我已经使用MFC构建了一个界面对话框,只需一个单选按钮即可。 像这样:

CONTROL " Encode",IDC_RADIO1,"Button",BS_AUTORADIOBUTTON | BS_RIGHT | BS_NOTIFY,35,20,41,10,WS_EX_TRANSPARENT in the RC file

我创建了class并带有m_radio_0.SubclassDlgItem(IDC_RADIO1, this);

我已经写了我的DrawItem。因此,当我单击时,我遇到了问题,我看到了支弓的变化,但从不停留检查。当我想要getcheck()说0时。 我的代码是这样的:

CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CString strText;
    int h = lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top;

    CRect rect(lpDrawItemStruct->rcItem.left + 2,
                lpDrawItemStruct->rcItem.top + 2,
                lpDrawItemStruct->rcItem.left + h - 3,
                lpDrawItemStruct->rcItem.top + h - 3);

        // just draws focus rectangle when listbox is empty

        int selChange = lpDrawItemStruct->itemAction & ODA_SELECT;
        int focusChange = lpDrawItemStruct->itemAction & ODA_FOCUS;
        int drawEntire = lpDrawItemStruct->itemAction & ODA_DRAWENTIRE;

        BOOL sel = lpDrawItemStruct->itemState & ODS_SELECTED;
        // Draws background rectangle, color depends on transparency
        pDC->FillSolidRect(&lpDrawItemStruct->rcItem,::GetSysColor((GetExStyle()&WS_EX_TRANSPARENT) ? COLOR_BTNFACE : COLOR_WINDOW));

        if (lpDrawItemStruct->itemState & ODS_SELECTED) // 
        {
            pDC->DrawFrameControl( rect, DFC_BUTTON, DFCS_CHECKED | DFCS_BUTTONRADIO);
            CButton::SetCheck(TRUE);
        }
        else
        {
            pDC->DrawFrameControl( rect, DFC_BUTTON, DFCS_BUTTONRADIO);
            CButton::SetCheck(FALSE);
        }

        // Draws item text
        ::SetTextColor(lpDrawItemStruct->hDC, m_crTextColor);

        // DEFAULT ==> BS_RIGHT
        UINT Option = DT_RIGHT;

        if ((uStyle & BS_RIGHT) == BS_RIGHT) Option = DT_RIGHT;
        if ((uStyle & BS_LEFT) == BS_LEFT)  Option = DT_LEFT;
                if ((uStyle & BS_CENTER) == BS_CENTER) Option = DT_CENTER;
        GetWindowText(strText);
        ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
            &lpDrawItemStruct->rcItem,  Option);
        ::SetBkColor(lpDrawItemStruct->hDC, m_crBkColor);
  

>   为我提供解决方案。   谢谢

1 个答案:

答案 0 :(得分:0)

我的回应:

#include the header:
<<
// This file was created on March 28th 2001. By Robert Brault
//
//

#if !defined(AFX_COLORBUTTON_H__714C19E7_EA25_42DF_928A_51AC901B813D__INCLUDED_)
#define AFX_COLORBUTTON_H__714C19E7_EA25_42DF_928A_51AC901B813D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ColorRadio.h : header file
//
#include "color.h"

/////////////////////////////////////////////////////////////////////////////
// ColorRadio window

class CColorRadioButton : public CButton
{
// Construction
public:
    void SetTextColor(COLORREF crColor); // This Function is to set the Color for the Text.

    void SetBkColor(COLORREF crColor); // This Function is to set the BackGround Color for the Text.
    void SetCheck(UINT Value);
    UINT GetCheck();
    CColorRadioButton();
    void PreSubclassWindow();
    virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);

    DWORD Mystyle;

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(ColorRadio)
    //}}AFX_VIRTUAL

    virtual ~CColorRadioButton();

    // Generated message map functions
protected:
    //{{AFX_MSG(CColorStatic)

    CBrush m_brBkgnd; // Holds Brush Color for the Static Text
    COLORREF m_crBkColor; // Holds the Background Color for the Text
    COLORREF m_crTextColor; // Holds the Color for the Text
    BOOL  m_rb1Checked;
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    UINT Value_Check;
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_COLORSTATIC_H__614C19E7_EA25_42DF_928A_51AC901B813D__INCLUDED_)

>>
and the class
<<
// This file was created on March 28th 2001 by Robert Brault.
// I created this Class to be able change the Color of your Static Text.
// This is Derived from CButton.
//
// There are three functions available Currently:
// SetBkColor(COLORREF crColor)
// SetTextColor(COLORREF crColor)
//
// How To Use:
// Add three files to your project
// CColorRadioButton.cpp, CColorRadioButton.h and Color.h
// Color.h has (#define)'s for different colors (add any color you desire).
//
// Add #include "CColorRadioButton.h" to your Dialogs Header file.
// Declare an instance of CColorRadioButton for each button being modified.
// Ex. CColorRadioButton m_radio;
//
// In your OnInitDialog() add a SubclassDlgItem for each CColorStatic member variable.
// Ex. m_radio.SubclassDlgItem(IDC_ST_TEXT, this);
// In this same function initialize your color for each piece of text unless you want the default.


// CColorRadioButton.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "ColorRadioButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColorRadioButton

CColorRadioButton::CColorRadioButton()
{
    m_crBkColor = ::GetSysColor(COLOR_3DFACE); // Initializing the Background Color to the system face color.
    m_crTextColor = BLACK; // Initializing the text to Black
    m_brBkgnd.CreateSolidBrush(m_crBkColor); // Create the Brush Color for the Background.

}

CColorRadioButton::~CColorRadioButton()
{
}


BEGIN_MESSAGE_MAP(CColorRadioButton, CStatic)
    //{{AFX_MSG_MAP(CColorStatic)
    ON_WM_CTLCOLOR_REFLECT()
    ON_WM_CTLCOLOR()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorRadioButton message handlers

HBRUSH CColorRadioButton::CtlColor(CDC* pDC, UINT nCtlColor) 
{
    HBRUSH hbr;
    hbr = (HBRUSH)m_brBkgnd; // Passing a Handle to the Brush
    pDC->SetBkColor(m_crBkColor); // Setting the Color of the Text Background to the one passed by the Dialog
    pDC->SetTextColor(m_crTextColor); // Setting the Text Color to the one Passed by the Dialog

    if (nCtlColor) nCtlColor += 0;

    return hbr;
}

void CColorRadioButton::SetBkColor(COLORREF crColor)
{
    m_crBkColor = crColor; // Passing the value passed by the dialog to the member varaible for Backgound Color
    m_brBkgnd.DeleteObject(); // Deleting any Previous Brush Colors if any existed.
    m_brBkgnd.CreateSolidBrush(crColor); // Creating the Brush Color For the Static Text Background
    RedrawWindow();
}

void CColorRadioButton::SetTextColor(COLORREF crColor)
{
    m_crTextColor = crColor; 
    RedrawWindow();
}

void CColorRadioButton::SetCheck(UINT Value)
{
    m_rb1Checked = Value;
    RedrawWindow();

}


UINT CColorRadioButton::GetCheck()
{

    return (m_rb1Checked); 

}

void CColorRadioButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    UINT uStyle =  Mystyle; 
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CString strText;
    UINT Option = DT_RIGHT;
    CRect rcBullet(&lpDrawItemStruct->rcItem);
    rcBullet.right = rcBullet.left + rcBullet.Height();

    GetWindowText(strText);

    pDC->FillSolidRect(&lpDrawItemStruct->rcItem,::GetSysColor((GetExStyle()&WS_EX_TRANSPARENT) ? COLOR_BTNFACE : COLOR_WINDOW));

    UINT nState = DFCS_BUTTONRADIO|DFCS_ADJUSTRECT;
    if (m_rb1Checked) nState |= DFCS_CHECKED;
    pDC->DrawFrameControl(rcBullet, DFC_BUTTON, nState);

    if ((uStyle & BS_RIGHT) == BS_RIGHT)   Option = DT_RIGHT;
    if ((uStyle & BS_LEFT) == BS_LEFT)     Option = DT_LEFT;
    if ((uStyle & BS_CENTER) == BS_CENTER) Option = DT_CENTER;
    ::SetTextColor(lpDrawItemStruct->hDC, m_crTextColor);
    ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), &lpDrawItemStruct->rcItem,  Option); 

}

void CColorRadioButton::PreSubclassWindow()
{   
    CButton::PreSubclassWindow();
    Mystyle =GetButtonStyle()|  BS_OWNERDRAW;
    SetButtonStyle(Mystyle);
}

in your's dlg header do
<<
CColorRadioButton m_radio_0;
>>
and then you can call it
<<`enter code here`
m_radio_0.SubclassDlgItem(IDC_RADIO1, this);
    m_radio_0.SetTextColor(GREEN);
    m_radio_0.SetCheck(BST_CHECKED);
  

>

实际上就是