C++Button设置按钮⽂本
1、类CButtonST中定义⼀个设置按钮图标的成员函数
DWORD CButtonST::SetIcon(int nIconIn, int nIconOut)
{
HICON        hIconIn            = NULL;
HICON        hIconOut        = NULL;
HINSTANCE    hInstResource    = NULL;
// Find correct resource handle
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn), RT_GROUP_ICON);
// Set icon when the mouse is IN the button
hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, 0, 0, 0);
// Set icon when the mouse is OUT the button
if (nIconOut)
{
if (nIconOut == (int)BTNST_AUTO_GRAY)
hIconOut = BTNST_AUTO_GRAY;
else
hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut), IMAGE_ICON, 0, 0, 0);    } // if改变button按钮的形状
return SetIcon(hIconIn, hIconOut);
}//函数⾥⾯⽤到的全⼤写变量或对象基本都是C++⾃带的封装
2、设置Button格式的成员函数(包括字体和颜⾊)
DWORD CButtonST::OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint)
{
BYTE    byRed = 0;
BYTE    byGreen = 0;
BYTE    byBlue = 0;
short    shOffsetR = shOffset;
short    shOffsetG = shOffset;
short    shOffsetB = shOffset;
if (byColorIndex >= BTNST_MAX_COLORS)    return BTNST_INVALIDINDEX;
if (shOffset < -255 || shOffset > 255)    return BTNST_BADPARAM;
// Get RGB components of specified color
byRed = GetRValue(m_crColors[byColorIndex]);
byGreen = GetGValue(m_crColors[byColorIndex]);
byBlue = GetBValue(m_crColors[byColorIndex]);
// Calculate max. allowed real offset
if (shOffset > 0)
{
if (byRed + shOffset > 255)        shOffsetR = 255 - byRed;
if (byGreen + shOffset > 255)    shOffsetG = 255 - byGreen;
if (byBlue + shOffset > 255)    shOffsetB = 255 - byBlue;
shOffset = min(min(shOffsetR, shOffsetG), shOffsetB);
} // if
else
{
if (byRed + shOffset < 0)        shOffsetR = -byRed;
if (byGreen + shOffset < 0)        shOffsetG = -byGreen;
if (byBlue + shOffset < 0)        shOffsetB = -byBlue;
shOffset = max(max(shOffsetR, shOffsetG), shOffsetB);
} // else
// Set new color
m_crColors[byColorIndex] = RGB(byRed + shOffset, byGreen + shOffset, byBlue + shOffset);    if (bRepaint)    Invalidate();
return BTNST_OK;
} // End of OffsetColor以上⽤到的全部都是C++⾃带的封装宏定义或者函数
3、设置Button隐藏按钮的函数
DWORD CButtonST::SetFlat(BOOL bFlat, BOOL bRepaint)
{
m_bIsFlat = bFlat;
if (bRepaint)    Invalidate();
return BTNST_OK;
} // End of SetFlat
4、设置⽂本函数
void CButtonST::SetTooltipText(int nText, BOOL bActivate)
{
CString sText;
// Load string resource
sText.LoadString(nText);
// If string resource is not empty
if (sText.IsEmpty() == FALSE) SetTooltipText((LPCTSTR)sText, bActivate);
} // End of SetTooltipText
5、以上给予CButtonST类的,所以需要定义⼀个CButtonST的对象才能调⽤CButtonST的成员函数CButtonST    m_btnMode_Line;
5.1、调⽤设置Button图标函数
m_btnMode_Line.SetIcon(IDI_ICON_LINE, IDI_ICON_LINE);
5.2、调⽤设置Button格式函数
m_btnMode_Line.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
5.3、调⽤设置Button隐藏函数
m_btnMode_Line.SetFlat(TRUE);
5.4、调⽤设置Button⽂本函数
m_btnMode_Line.SetTooltipText(_T("直线"));
5.5、设置按钮使能函数
m_btnMode_Line.EnableWindow(BEnable);    \\这是C++底层封装的函数BEnable = true\false

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。