📄 btnst.cpp
字号:
BOOL bRetValue;
ICONINFO ii;
// Free any loaded resource
FreeResources();
if (hIconIn)
{
// Icon when mouse over button?
m_csIcons[0].hIcon = hIconIn;
// Get icon dimension
::ZeroMemory(&ii, sizeof(ICONINFO));
bRetValue = ::GetIconInfo(hIconIn, &ii);
if (bRetValue == FALSE)
{
FreeResources();
return BTNST_INVALIDRESOURCE;
} // if
m_csIcons[0].dwWidth = (DWORD)(ii.xHotspot * 2);
m_csIcons[0].dwHeight = (DWORD)(ii.yHotspot * 2);
::DeleteObject(ii.hbmMask);
::DeleteObject(ii.hbmColor);
// Icon when mouse outside button?
if (hIconOut)
{
if (hIconOut == BTNST_AUTO_GRAY)
{
hIconOut = CreateGrayscaleIcon(hIconIn);
} // if
m_csIcons[1].hIcon = hIconOut;
// Get icon dimension
::ZeroMemory(&ii, sizeof(ICONINFO));
bRetValue = ::GetIconInfo(hIconOut, &ii);
if (bRetValue == FALSE)
{
FreeResources();
return BTNST_INVALIDRESOURCE;
} // if
m_csIcons[1].dwWidth = (DWORD)(ii.xHotspot * 2);
m_csIcons[1].dwHeight = (DWORD)(ii.yHotspot * 2);
::DeleteObject(ii.hbmMask);
::DeleteObject(ii.hbmColor);
} // if
} // if
Invalidate();
return BTNST_OK;
} // End of SetIcon
// This function assigns bitmaps to the button.
// Any previous icon or bitmap will be removed.
//
// Parameters:
// [IN] nBitmapIn
// ID number of the bitmap resource to show when the mouse is over the button.
// Pass NULL to remove any bitmap from the button.
// [IN] crTransColorIn
// Color (inside nBitmapIn) to be used as transparent color.
// [IN] nBitmapOut
// ID number of the bitmap resource to show when the mouse is outside the button.
// Can be NULL.
// [IN] crTransColorOut
// Color (inside nBitmapOut) to be used as transparent color.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDRESOURCE
// Failed loading the specified resource.
// BTNST_FAILEDMASK
// Failed creating mask bitmap.
//
DWORD CButtonST::SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, int nBitmapOut, COLORREF crTransColorOut)
{
HBITMAP hBitmapIn = NULL;
HBITMAP hBitmapOut = NULL;
HINSTANCE hInstResource = NULL;
// Find correct resource handle
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nBitmapIn), RT_BITMAP);
// Load bitmap In
hBitmapIn = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapIn), IMAGE_BITMAP, 0, 0, 0);
// Load bitmap Out
if (nBitmapOut)
hBitmapOut = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapOut), IMAGE_BITMAP, 0, 0, 0);
return SetBitmaps(hBitmapIn, crTransColorIn, hBitmapOut, crTransColorOut);
} // End of SetBitmaps
// This function assigns bitmaps to the button.
// Any previous icon or bitmap will be removed.
//
// Parameters:
// [IN] hBitmapIn
// Handle fo the bitmap to show when the mouse is over the button.
// Pass NULL to remove any bitmap from the button.
// [IN] crTransColorIn
// Color (inside hBitmapIn) to be used as transparent color.
// [IN] hBitmapOut
// Handle to the bitmap to show when the mouse is outside the button.
// Can be NULL.
// [IN] crTransColorOut
// Color (inside hBitmapOut) to be used as transparent color.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDRESOURCE
// Failed loading the specified resource.
// BTNST_FAILEDMASK
// Failed creating mask bitmap.
//
DWORD CButtonST::SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, HBITMAP hBitmapOut, COLORREF crTransColorOut)
{
int nRetValue;
BITMAP csBitmapSize;
// Free any loaded resource
FreeResources();
if (hBitmapIn)
{
m_csBitmaps[0].hBitmap = hBitmapIn;
m_csBitmaps[0].crTransparent = crTransColorIn;
// Get bitmap size
nRetValue = ::GetObject(hBitmapIn, sizeof(csBitmapSize), &csBitmapSize);
if (nRetValue == 0)
{
FreeResources();
return BTNST_INVALIDRESOURCE;
} // if
m_csBitmaps[0].dwWidth = (DWORD)csBitmapSize.bmWidth;
m_csBitmaps[0].dwHeight = (DWORD)csBitmapSize.bmHeight;
// Create mask for bitmap In
m_csBitmaps[0].hMask = CreateBitmapMask(hBitmapIn, m_csBitmaps[0].dwWidth, m_csBitmaps[0].dwHeight, crTransColorIn);
if (m_csBitmaps[0].hMask == NULL)
{
FreeResources();
return BTNST_FAILEDMASK;
} // if
if (hBitmapOut)
{
m_csBitmaps[1].hBitmap = hBitmapOut;
m_csBitmaps[1].crTransparent = crTransColorOut;
// Get bitmap size
nRetValue = ::GetObject(hBitmapOut, sizeof(csBitmapSize), &csBitmapSize);
if (nRetValue == 0)
{
FreeResources();
return BTNST_INVALIDRESOURCE;
} // if
m_csBitmaps[1].dwWidth = (DWORD)csBitmapSize.bmWidth;
m_csBitmaps[1].dwHeight = (DWORD)csBitmapSize.bmHeight;
// Create mask for bitmap Out
m_csBitmaps[1].hMask = CreateBitmapMask(hBitmapOut, m_csBitmaps[1].dwWidth, m_csBitmaps[1].dwHeight, crTransColorOut);
if (m_csBitmaps[1].hMask == NULL)
{
FreeResources();
return BTNST_FAILEDMASK;
} // if
} // if
} // if
Invalidate();
return BTNST_OK;
} // End of SetBitmaps
// This functions sets the button to have a standard or flat style.
//
// Parameters:
// [IN] bFlat
// If TRUE the button will have a flat style, else
// will have a standard style.
// By default, CButtonST buttons are flat.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
// Return value:
// BTNST_OK
// Function executed successfully.
//
DWORD CButtonST::SetFlat(BOOL bFlat, BOOL bRepaint)
{
m_bIsFlat = bFlat;
if (bRepaint) Invalidate();
return BTNST_OK;
} // End of SetFlat
// This function sets the alignment type between icon/bitmap and text.
//
// Parameters:
// [IN] byAlign
// Alignment type. Can be one of the following values:
// ST_ALIGN_HORIZ Icon/bitmap on the left, text on the right
// ST_ALIGN_VERT Icon/bitmap on the top, text on the bottom
// ST_ALIGN_HORIZ_RIGHT Icon/bitmap on the right, text on the left
// ST_ALIGN_OVERLAP Icon/bitmap on the same space as text
// By default, CButtonST buttons have ST_ALIGN_HORIZ alignment.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDALIGN
// Alignment type not supported.
//
DWORD CButtonST::SetAlign(BYTE byAlign, BOOL bRepaint)
{
switch (byAlign)
{
case ST_ALIGN_HORIZ:
case ST_ALIGN_HORIZ_RIGHT:
case ST_ALIGN_VERT:
case ST_ALIGN_OVERLAP:
m_byAlign = byAlign;
if (bRepaint) Invalidate();
return BTNST_OK;
break;
} // switch
return BTNST_INVALIDALIGN;
} // End of SetAlign
// This function sets the pressed style.
//
// Parameters:
// [IN] byStyle
// Pressed style. Can be one of the following values:
// BTNST_PRESSED_LEFTRIGHT Pressed style from left to right (as usual)
// BTNST_PRESSED_TOPBOTTOM Pressed style from top to bottom
// By default, CButtonST buttons have BTNST_PRESSED_LEFTRIGHT style.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDPRESSEDSTYLE
// Pressed style not supported.
//
DWORD CButtonST::SetPressedStyle(BYTE byStyle, BOOL bRepaint)
{
switch (byStyle)
{
case BTNST_PRESSED_LEFTRIGHT:
m_ptPressedOffset.x = 1;
m_ptPressedOffset.y = 1;
break;
case BTNST_PRESSED_TOPBOTTOM:
m_ptPressedOffset.x = 0;
m_ptPressedOffset.y = 2;
break;
default:
return BTNST_INVALIDPRESSEDSTYLE;
} // switch
if (bRepaint) Invalidate();
return BTNST_OK;
} // End of SetPressedStyle
// This function sets the state of the checkbox.
// If the button is not a checkbox, this function has no meaning.
//
// Parameters:
// [IN] nCheck
// 1 to check the checkbox.
// 0 to un-check the checkbox.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
// Return value:
// BTNST_OK
// Function executed successfully.
//
DWORD CButtonST::SetCheck(int nCheck, BOOL bRepaint)
{
if (m_bIsCheckBox)
{
if (nCheck == 0) m_nCheck = 0;
else m_nCheck = 1;
if (bRepaint) Invalidate();
} // if
return BTNST_OK;
} // End of SetCheck
// This function returns the current state of the checkbox.
// If the button is not a checkbox, this function has no meaning.
//
// Return value:
// The current state of the checkbox.
// 1 if checked.
// 0 if not checked or the button is not a checkbox.
//
int CButtonST::GetCheck()
{
return m_nCheck;
} // End of GetCheck
// This function sets all colors to a default value.
//
// Parameters:
// [IN] bRepaint
// If TRUE the control will be repainted.
//
// Return value:
// BTNST_OK
// Function executed successfully.
//
DWORD CButtonST::SetDefaultColors(BOOL bRepaint)
{
m_crColors[BTNST_COLOR_BK_IN] = ::GetSysColor(COLOR_BTNFACE);
m_crColors[BTNST_COLOR_FG_IN] = ::GetSysColor(COLOR_BTNTEXT);
m_crColors[BTNST_COLOR_BK_OUT] = ::GetSysColor(COLOR_BTNFACE);
m_crColors[BTNST_COLOR_FG_OUT] = ::GetSysColor(COLOR_BTNTEXT);
m_crColors[BTNST_COLOR_BK_FOCUS] = ::GetSysColor(COLOR_BTNFACE);
m_crColors[BTNST_COLOR_FG_FOCUS] = ::GetSysColor(COLOR_BTNTEXT);
if (bRepaint) Invalidate();
return BTNST_OK;
} // End of SetDefaultColors
// This function sets the color to use for a particular state.
//
// Parameters:
// [IN] byColorIndex
// Index of the color to set. Can be one of the following values:
// BTNST_COLOR_BK_IN Background color when mouse is over the button
// BTNST_COLOR_FG_IN Text color when mouse is over the button
// BTNST_COLOR_BK_OUT Background color when mouse is outside the button
// BTNST_COLOR_FG_OUT Text color when mouse is outside the button
// BTNST_COLOR_BK_FOCUS Background color when the button is focused
// BTNST_COLOR_FG_FOCUS Text color when the button is focused
// [IN] crColor
// New color.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDINDEX
// Invalid color index.
//
DWORD CButtonST::SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint)
{
if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
// Set new color
m_crColors[byColorIndex] = crColor;
if (bRepaint) Invalidate();
return BTNST_OK;
} // End of SetColor
// This functions returns the color used for a particular state.
//
// Parameters:
// [IN] byColorIndex
// Index of the color to get.
// See SetColor for the list of available colors.
// [OUT] crpColor
// A pointer to a COLORREF that will receive the color.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDINDEX
// Invalid color index.
//
DWORD CButtonST::GetColor(BYTE byColorIndex, COLORREF* crpColor)
{
if (byColorIndex >= BTNST_MAX_COLORS) return BTNST_INVALIDINDEX;
// Get color
*crpColor = m_crColors[byColorIndex];
return BTNST_OK;
} // End of GetColor
// This function applies an offset to the RGB components of the specified color.
// This function can be seen as an easy way to make a color darker or lighter than
// its default value.
//
// Parameters:
// [IN] byColorIndex
// Index of the color to set.
// See SetColor for the list of available colors.
// [IN] shOffsetColor
// A short value indicating the offset to apply to the color.
// This value must be between -255 and 255.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDINDEX
// Invalid color index.
// BTNST_BADPARAM
// The specified offset is out of range.
//
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
// This function sets the hilight logic for the button.
// Applies only to flat buttons.
//
// Parameters:
// [IN] bAlwaysTrack
// If TRUE the button will be hilighted even if the window that owns it, is
// not the active window.
// If FALSE the button will be hilighted only if the window that owns it,
// is the active window.
//
// Return value:
// BTNST_OK
// Function executed successfully.
//
DWORD CButtonST::SetAlwaysTrack(BOOL bAlwaysTrack)
{
m_bAlwaysTrack = bAlwaysTrack;
return BTNST_OK;
} // End of SetAlwaysTrack
// This function sets the cursor to be used when the mouse is over the button.
//
// Parameters:
// [IN] nCursorId
// ID number of the cursor resource.
// Pass NULL to remove a previously loaded cursor.
// [IN] bRepaint
// If TRUE the control will be repainted.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDRESOURCE
// Failed loading the specified resource.
//
DWORD CButtonST::SetBtnCursor(int nCursorId, BOOL bRepaint)
{
HINSTANCE hInstResource = NULL;
// Destroy any previous cursor
if (m_hCursor)
{
::DestroyCursor(m_hCursor);
m_hCursor = NULL;
} // if
// Load cursor
if (nCursorId)
{
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId), RT_GROUP_CURSOR);
// Load cursor resource
m_hCursor = (HCURSOR)::LoadImage(hInstResource, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
// Repaint the button
if (bRepaint) Invalidate();
// If something wrong
if (m_hCursor == NULL) return BTNST_INVALIDRESOURCE;
} // if
return BTNST_OK;
} // End of SetBtnCursor
// This function sets if the button border must be drawn.
// Applies only to flat buttons.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -