Partager via


CMFCButton Class

The CMFCButton class adds functionality to the CButton class such as aligning button text, combining button text and an image, selecting a cursor, and specifying a tool tip.

class CMFCButton : public CButton

Members

Public Constructors

Name

Description

CMFCButton::CMFCButton

Default constructor.

CMFCButton::~CMFCButton

Destructor.

Public Methods

Name

Description

CMFCButton::CleanUp

Resets internal variables and frees allocated resources such as images, bitmaps, and icons.

CMFCButton::CreateObject

Used by the framework to create a dynamic instance of this class type.

CMFCButton::DrawItem

Called by the framework when a visual aspect of an owner-drawn button has changed. (Overrides CButton::DrawItem.)

CMFCButton::EnableFullTextTooltip

Specifies whether to display the full text of a tooltip in a large tooltip window or a truncated version of the text in a small tooltip window.

CMFCButton::EnableMenuFont

Specifies whether the button text font is the same as the application menu font.

CMFCButton::EnableWindowsTheming

Specifies whether the style of the button border corresponds to the current Windows theme.

CMFCButton::GetThisClass

Used by the framework to obtain a pointer to the CRuntimeClass object that is associated with this class type.

CMFCButton::GetToolTipCtrl

Returns a reference to the underlying tooltip control.

CMFCButton::IsAutoCheck

Indicates whether a check box or radio button is an automatic button.

CMFCButton::IsAutorepeatCommandMode

Indicates whether a button is set to auto-repeat mode.

CMFCButton::IsCheckBox

Indicates whether a button is a check box button.

CMFCButton::IsChecked

Indicates whether the current button is checked.

CMFCButton::IsHighlighted

Indicates whether a button is highlighted.

CMFCButton::IsPressed

Indicates whether a button is pushed and highlighted.

CMFCButton::IsPushed

Indicates whether a button is pushed.

CMFCButton::IsRadioButton

Indicates whether a button is a radio button.

CMFCButton::IsWindowsThemingEnabled

Indicates whether the style of the button border corresponds to the current Windows theme.

CMFCButton::OnDrawParentBackground

Draws the background of a button's parent in the specified area. (Overrides AFX_GLOBAL_DATA::DrawParentBackground.)

CMFCButton::PreTranslateMessage

Translates window messages before they are dispatched to the TranslateMessage and DispatchMessage Windows functions. (Overrides CWnd::PreTranslateMessage.)

CMFCButton::SetAutorepeatMode

Sets a button to auto-repeat mode.

CMFCButton::SetCheckedImage

Sets the image for a checked button.

CMFCButton::SetFaceColor

Sets the background color for the button text.

CMFCButton::SetImage

Sets the image for a button.

CMFCButton::SetMouseCursor

Sets the cursor image.

CMFCButton::SetMouseCursorHand

Sets the cursor to the image of a hand.

CMFCButton::SetStdImage

Uses a CMenuImages object to set the button image.

CMFCButton::SetTextColor

Sets the color of the button text for a button that is not selected.

CMFCButton::SetTextHotColor

Sets the color of the button text for a button that is selected.

CMFCButton::SetTooltip

Associates a tooltip with a button.

CMFCButton::SizeToContent

Resizes a button to contain its button text and image.

Protected Methods

Name

Description

CMFCButton::OnDraw

Called by the framework to draw a button.

CMFCButton::OnDrawBorder

Called by the framework to draw the border of a button.

CMFCButton::OnDrawFocusRect

Called by the framework to draw the focus rectangle for a button.

CMFCButton::OnDrawText

Called by the framework to draw the button text.

CMFCButton::OnFillBackground

Called by the framework to draw the background of the button text.

CMFCButton::SelectFont

Retrieves the font that is associated with the specified device context.

Data Members

Name

Description

CMFCButton::m_bDrawFocus

Indicates whether to draw a focus rectangle around a button.

CMFCButton::m_bHighlightChecked

Indicates whether to highlight a BS_CHECKBOX-style button when the cursor hovers over it.

CMFCButton::m_bRightImage

Indicates whether to display an image on the right side of the button.

CMFCButton::m_bTransparent

Indicates whether the button is transparent.

CMFCButton::m_nAlignStyle

Specifies the alignment of the button text.

CMFCButton::m_nFlatStyle

Specifies the style of the button, such as borderless, flat, semi-flat, or 3D.

Remarks

Other types of buttons are derived from the CMFCButton class, such as the CMFCURLLinkButton class, which supports hyperlinks, and the CMFCColorButton class, which supports a color picker dialog box.

The style of a CMFCButton object can be 3D, flat, semi-flat or no border. Button text can be aligned at the left, top, or center of a button. At run time, you can control whether the button displays text, an image, or text and an image. You can also specify that a particular cursor image be displayed when the cursor hovers over a button.

Create a button control either directly in your code, or by using the MFC Class Wizard tool and a dialog box template. If you create a button control directly, add a CMFCButton variable to your application, and then call the constructor and Create methods of the CMFCButton object. If you use the MFC Class Wizard, add a CButton variable to your application, and then change the type of the variable from CButton to CMFCButton.

To handle notification messages in a dialog box application, add a message map entry and an event handler for each notification. The notifications sent by a CMFCButton object are the same as those sent by a CButton object.

Example

The following example demonstrates how to configure the properties of the button by using various methods in the CMFCButton class. The example is part of the New Controls sample.

    CMFCButton m_Button;


...


    // int m_iImage
    // IDB_BTN1_32, IDB_BTN1_HOT_32, IDB_BTN1, IDB_BTN1_HOT are int macros that are #define.
    if (m_iImage == 1) 
    {
        m_Button.SetImage((HBITMAP) NULL);
    }
    else
    {
        m_Button.SetImage(IDB_BTN1_32, IDB_BTN1_HOT_32);
    }


...


    // int m_iImage
    if (m_iImage == 0)
    {
        m_Button.SetWindowText(_T(""));
    }
    else
    {
        m_Button.SetWindowText(_T("Button"));
    }


...


    // Resize the button.
    m_Button.SizeToContent();
    m_Button.EnableFullTextTooltip(true);
    // Use the application menu font at the button text font.
    m_Button.EnableMenuFont();
    // Use the current Windows theme to draw the button borders.
    m_Button.EnableWindowsTheming(true);
    // Set the button to auto-repeat mode.
    m_Button.SetAutorepeatMode();
    // Set the background color for the button text.
    m_Button.SetFaceColor(RGB(255,0,0),true);
    m_Button.SetTextColor(RGB(0,0,255));
    // Set the tooltip of the button.
    m_Button.SetTooltip(_T("this is a button!"));

Inheritance Hierarchy

CObject

   CCmdTarget

      CWnd

         CButton

            CMFCButton

Requirements

Header: afxbutton.h

See Also

Reference

Hierarchy Chart

CMFCLinkCtrl Class

CMFCColorButton Class

CMFCMenuButton Class

Other Resources

MFC Classes