Condividi tramite


CMFCColorMenuButton Class

The CMFCColorMenuButton class supports a menu command or a toolbar button that starts a color picker dialog box.

class CMFCColorMenuButton : public CMFCToolBarMenuButton

Members

Public Constructors

Name

Description

CMFCColorMenuButton::CMFCColorMenuButton

Constructs a CMFCColorMenuButton object.

Public Methods

Name

Description

CMFCColorMenuButton::EnableAutomaticButton

Enables and disables an "automatic" button that is positioned above the regular color buttons. (The standard system automatic button is labeled Automatic.)

CMFCColorMenuButton::EnableDocumentColors

Enables the display of document-specific colors instead of system colors.

CMFCColorMenuButton::EnableOtherButton

Enables and disables an "other" button that is positioned below the regular color buttons. (The standard system "other" button is labeled More Colors….)

CMFCColorMenuButton::EnableTearOff

Enables the ability to tear off a color pane.

CMFCColorMenuButton::GetAutomaticColor

Retrieves the current automatic color.

CMFCColorMenuButton::GetColor

Retrieves the current button's color.

CMFCColorMenuButton::GetColorByCmdID

Retrieves the color that corresponds to a specified command ID.

CMFCColorMenuButton::OnChangeParentWnd

Called by the framework when the parent window changes.

CMFCColorMenuButton::OpenColorDialog

Opens a color selection dialog box.

CMFCColorMenuButton::SetColor

Sets the color of the current color button.

CMFCColorMenuButton::SetColorByCmdID

Sets the color of the specified color menu button.

CMFCColorMenuButton::SetColorName

Sets a new name for the specified color.

CMFCColorMenuButton::SetColumnsNumber

Sets the number of columns that are displayed by a CMFCColorBar object.

Protected Methods

Name

Description

CMFCColorMenuButton::CopyFrom

Copies another toolbar button to the current button.

CMFCColorMenuButton::CreatePopupMenu

Creates a color picker dialog box.

CMFCColorMenuButton::IsEmptyMenuAllowed

Indicates whether empty menus are supported.

CMFCColorMenuButton::OnDraw

Called by the framework to display an image on a button.

CMFCColorMenuButton::OnDrawOnCustomizeList

Called by the framework before a CMFCColorMenuButton object is displayed in the list of a toolbar customization dialog box.

Remarks

To replace the original menu command or toolbar button with a CMFCColorMenuButton object, create the CMFCColorMenuButton object, set any appropriate CMFCColorBar Class styles, and then call the ReplaceButton method of the CMFCToolBar Class class. If you customize a toolbar, call the CMFCToolBarsCustomizeDialog::ReplaceButton method.

The color picker dialog box is created during the processing of the CMFCColorMenuButton::CreatePopupMenu event handler. The event handler notifies the parent frame with a WM_COMMAND message. The CMFCColorMenuButton object sends the control ID that is assigned to the original menu command or toolbar button.

Example

The following example demonstrates how to create and configure a color menu button by using various methods in the CMFCColorMenuButton class. In the example, a CPalette object is first created and then used to construct an object of the CMFCColorMenuButton class. The CMFCColorMenuButton object is then configured by enabling its automatic and other buttons, and setting its color and the number of columns. This code is part of the Word Pad sample.

  CPalette    m_palColorPicker;   // Palette for color picker
    int         m_nNumColours;


...


CMFCColorMenuButton* CFormatBar::CreateColorButton ()
{
    if (m_palColorPicker.GetSafeHandle () == NULL)
    {
        m_nNumColours = sizeof (crColours)/sizeof(ColourTableEntry);
        ASSERT(m_nNumColours <= MAX_COLOURS);
        if (m_nNumColours > MAX_COLOURS)
            m_nNumColours = MAX_COLOURS;

        // Create the palette 
        struct 
        {
            LOGPALETTE    LogPalette;
            PALETTEENTRY  PalEntry[MAX_COLOURS];
        }pal;

        LOGPALETTE* pLogPalette = (LOGPALETTE*) &pal;
        pLogPalette->palVersion    = 0x300;
        pLogPalette->palNumEntries = (WORD) m_nNumColours; 

        for (int i = 0; i < m_nNumColours; i++)
        {
            pLogPalette->palPalEntry[i].peRed   = GetRValue(crColours[i].crColour);
            pLogPalette->palPalEntry[i].peGreen = GetGValue(crColours[i].crColour);
            pLogPalette->palPalEntry[i].peBlue  = GetBValue(crColours[i].crColour);
            pLogPalette->palPalEntry[i].peFlags = 0;
        }

        m_palColorPicker.CreatePalette (pLogPalette);
    }


    CMFCColorMenuButton* pColorButton = new 
        CMFCColorMenuButton (ID_CHAR_COLOR, _T("Text Color..."), &m_palColorPicker);

    pColorButton->EnableAutomaticButton (_T("Automatic"), RGB (0, 0, 0));
    pColorButton->EnableOtherButton (_T("More Colors..."));
    pColorButton->EnableDocumentColors (_T("Document's Colors"));
    pColorButton->EnableTearOff (ID_COLOR_TEAROFF, 5, 2);
    pColorButton->SetColumnsNumber (8);
    pColorButton->SetColor(RGB(0,0,255));

    // Initialize color names: 
    for (int i = 0; i < m_nNumColours; i++)
    {
        CMFCColorMenuButton::SetColorName (crColours[i].crColour, crColours[i].szName);
    }

    return pColorButton;
}

Inheritance Hierarchy

CObject

   CMFCToolBarButton

      CMFCToolBarMenuButton

         CMFCColorMenuButton

Requirements

Header: afxcolormenubutton.h

See Also

Reference

Hierarchy Chart

CMFCColorBar Class

CMFCToolBar Class

CMFCToolBarsCustomizeDialog Class

CMFCColorButton Class

Other Resources

MFC Classes