A button control class that inherits from cbutton. It can coexist and change the button background color and text. You can design the background color by yourself.

Source: Internet
Author: User
Tags drawtext

Header file: cmybutton. h:
# Pragma once
# Include "afxwin. H"

Class cmybutton: Public cbutton
{
// Declare_dynamic (cmybutton)
Public:
Cmybutton ();
Virtual ~ Cmybutton ();
// Set the background color of the button down.
Void setdowncolor (colorref color );
// Set the background color of the button up
Void setupcolor (colorref color );
Bool attach (const uint NID, cwnd * pparent );

Protected:

// Functions that must be reloaded
Virtual void drawitem (lpdrawitemstruct );

Public:
// The three colors are text, button down background color, and button up background color.
Colorref m_textcolor, m_downcolor, m_upcolor;
};

Source File: cmybutton. cpp

# Include "stdafx. H"
# Include "cmybutton. H"

Cmybutton: cmybutton (void)
{
M_downcolor = m_upcolor = RGB (0, 0, 0 );
}

Cmybutton ::~ Cmybutton (void)
{
}

// Cmybutton is a cbutton derived class and has all the member functions of cbutton,

// However, the bs_ownerdraw style is required during creation.

// If the button is not dynamically generated, use the attach function to make cmybutton replace the window process of the original button.

Bool cmybutton: attach (const uint NID, cwnd * pparent)
{
// Getdlgitem (NID)-> modifystyle (0, bs_ownerdraw, 0 );

If (! Subclassdlgitem (NID, pparent ))
Return false;

Return true;
}

Void cmybutton: setdowncolor (colorref color)
{
M_downcolor = color;
}

Void cmybutton: setupcolor (colorref color)
{
M_upcolor = color;
}

Void cmybutton: drawitem (lpdrawitemstruct)
{
// Todo: add your code to draw the specified item
Cdc dc;
DC. Attach (lpdrawitemstruct-> HDC); // obtain the drawn device environment CDC
Verify (lpdrawitemstruct-> ctltype = odt_button );

// Properly use the text on the button. The steps here are: 1. First, get the text of the button edited in the resource,

// Re-draw the text to the button,
// Set the background color of the text to transparent. In this way, only the text is displayed on the button.

Const int bufsize = 512;
Tchar buffer [bufsize];
Getwindowtext (buffer, bufsize );

Int size = strlen (buffer); // get the length
Drawtext (lpdrawitemstruct-> HDC, buffer, size, & lpdrawitemstruct-> rcitem, dt_center | dt_vcenter | dt_singleline | dt_tabstop); // draw text
Setbkmode (lpdrawitemstruct-> HDC, transparent); // transparent

If (lpdrawitemstruct-> itemstate & ods_selected) // processing when the button is pressed
{//
// Redraw the entire control
Cbrush brush (m_downcolor );
DC. fillrect (& (lpdrawitemstruct-> rcitem), & brush );//

// Because the text is re-painted, the text must be re-painted.
Drawtext (lpdrawitemstruct-> HDC, buffer, size, & lpdrawitemstruct-> rcitem, dt_center | dt_vcenter | dt_singleline | dt_tabstop );
Setbkmode (lpdrawitemstruct-> HDC, transparent );
}
Else // when the button is not operated or pops up
{
Cbrush brush (m_upcolor );
DC. fillrect (& (lpdrawitemstruct-> rcitem), & brush );//

// Re-paint the text as above
Drawtext (lpdrawitemstruct-> HDC, buffer, size, & lpdrawitemstruct-> rcitem, dt_center | dt_vcenter | dt_singleline | dt_tabstop );
Setbkmode (lpdrawitemstruct-> HDC, transparent );
}

If (lpdrawitemstruct-> itemstate & ods_selected) & (lpdrawitemstruct-> itemaction & (oda_select | oda_drawentire )))
{// This control is selected to highlight the border
Colorref fc = RGB (255-getrvalue (m_upcolor), 255-getgvalue (m_upcolor), 255-getbvalue (m_upcolor ));//
Cbrush brush (FC );//
DC. framerect (& (lpdrawitemstruct-> rcitem), & brush );//

}
If (! (Lpdrawitemstruct-> itemstate & ods_selected) & (lpdrawitemstruct-> itemaction & oda_select ))
{
// The selected state is ended and the border is removed.
Cbrush brush (m_upcolor );
DC. framerect (& lpdrawitemstruct-> rcitem, & brush );//

}
DC. Detach ();//
}

 

How to call the cmybutton class:

In the dialog box Class header file # include "cmybutton. H", find the oninitdialog () function in the dialog box class. If the oninitdialog () function cannot be found, reload it in the event attribute of the dialog box,

The Declaration of the m_cbbtn variable is:

Cmybutton m_cbbtn; // This sentence can be placed in other places of the class, as long as it is legal.

// Change the button to the bs_ownerdraw style. other styles are invalid.

Getdlgitem (idc_button1)-> modifystyle (0, bs_ownerdraw, 0 );

// Bind the control idc_button1 to the cmybutton class, and respond to the reload function drawitem ()
M_cbbtn.attach (idc_button1, this );

// Set the background color of button down
M_cbbtn.setdowncolor (RGB (255, 0, 0 ));

// Set the background color of Button up
M_cbbtn.setupcolor (RGB (0, 0, 255 ));

 

PS: if the connection code is m_cbbtn.attach (idc_button1, this); this statement is interrupted, probably because the idc_button1 control has been bound to a message. It is not successful to bind it again here,

The correct method is to map the function in dodataexchange ().

// Ddx_control (PDX, idc_button1, m_cbbtn); // comment out this sentence.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.