📄 btnst.cpp
字号:
byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
if (crPixel != crTrans)
::SetPixel(hMemDC1, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
else
::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
} // for
} // for
::SelectObject(hMemDC1, hOldBmp1);
::SelectObject(hMemDC2, hOldBmp2);
} // if
::DeleteDC(hMemDC1);
::DeleteDC(hMemDC2);
::ReleaseDC(NULL, hMainDC);
return hGrayBitmap;
} // End of CreateGrayscaleBitmap
// This function creates a bitmap that is 25% darker than the original.
// The resulting bitmap will have the same size of the original one.
//
// Parameters:
// [IN] hBitmap
// Handle to the original bitmap.
// [IN] dwWidth
// Specifies the bitmap width, in pixels.
// [IN] dwHeight
// Specifies the bitmap height, in pixels.
// [IN] crTrans
// Color to be used as transparent color. This color will be left unchanged.
//
// Return value:
// If the function succeeds, the return value is the handle to the newly created
// darker bitmap.
// If the function fails, the return value is NULL.
//
HBITMAP CButtonST::CreateDarkerBitmap(HBITMAP hBitmap, DWORD dwWidth, DWORD dwHeight, COLORREF crTrans)
{
HBITMAP hGrayBitmap = NULL;
HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
hMainDC = ::GetDC(NULL);
if (hMainDC == NULL) return NULL;
hMemDC1 = ::CreateCompatibleDC(hMainDC);
if (hMemDC1 == NULL)
{
::ReleaseDC(NULL, hMainDC);
return NULL;
} // if
hMemDC2 = ::CreateCompatibleDC(hMainDC);
if (hMemDC2 == NULL)
{
::DeleteDC(hMemDC1);
::ReleaseDC(NULL, hMainDC);
return NULL;
} // if
hGrayBitmap = ::CreateCompatibleBitmap(hMainDC, dwWidth, dwHeight);
if (hGrayBitmap)
{
hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, hGrayBitmap);
hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, hBitmap);
//::BitBlt(hMemDC1, 0, 0, dwWidth, dwHeight, hMemDC2, 0, 0, SRCCOPY);
DWORD dwLoopY = 0, dwLoopX = 0;
COLORREF crPixel = 0;
for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
{
for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
{
crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);
if (crPixel != crTrans)
::SetPixel(hMemDC1, dwLoopX, dwLoopY, DarkenColor(crPixel, 0.25));
else
::SetPixel(hMemDC1, dwLoopX, dwLoopY, crPixel);
} // for
} // for
::SelectObject(hMemDC1, hOldBmp1);
::SelectObject(hMemDC2, hOldBmp2);
} // if
::DeleteDC(hMemDC1);
::DeleteDC(hMemDC2);
::ReleaseDC(NULL, hMainDC);
return hGrayBitmap;
} // End of CreateDarkerBitmap
// This function creates a grayscale icon starting from a given icon.
// The resulting icon will have the same size of the original one.
//
// Parameters:
// [IN] hIcon
// Handle to the original icon.
//
// Return value:
// If the function succeeds, the return value is the handle to the newly created
// grayscale icon.
// If the function fails, the return value is NULL.
//
// Updates:
// 26/Nov/2002 Restored 1 BitBlt operation
// 03/May/2002 Removed dependancy from m_hWnd
// Removed 1 BitBlt operation
//
HICON CButtonST::CreateGrayscaleIcon(HICON hIcon)
{
HICON hGrayIcon = NULL;
HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
BITMAP bmp;
HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
ICONINFO csII, csGrayII;
BOOL bRetValue = FALSE;
bRetValue = ::GetIconInfo(hIcon, &csII);
if (bRetValue == FALSE) return NULL;
hMainDC = ::GetDC(NULL);
hMemDC1 = ::CreateCompatibleDC(hMainDC);
hMemDC2 = ::CreateCompatibleDC(hMainDC);
if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
{
DWORD dwWidth = csII.xHotspot*2;
DWORD dwHeight = csII.yHotspot*2;
csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
if (csGrayII.hbmColor)
{
hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
//::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, SRCCOPY);
DWORD dwLoopY = 0, dwLoopX = 0;
COLORREF crPixel = 0;
BYTE byNewPixel = 0;
for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
{
for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
{
crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + (GetGValue(crPixel) * 0.587) + (GetBValue(crPixel) * 0.114));
if (crPixel)
::SetPixel(hMemDC2, dwLoopX, dwLoopY, RGB(byNewPixel, byNewPixel, byNewPixel));
else
::SetPixel(hMemDC2, dwLoopX, dwLoopY, crPixel);
} // for
} // for
::SelectObject(hMemDC1, hOldBmp1);
::SelectObject(hMemDC2, hOldBmp2);
csGrayII.hbmMask = csII.hbmMask;
csGrayII.fIcon = TRUE;
hGrayIcon = ::CreateIconIndirect(&csGrayII);
} // if
::DeleteObject(csGrayII.hbmColor);
//::DeleteObject(csGrayII.hbmMask);
} // if
::DeleteObject(csII.hbmColor);
::DeleteObject(csII.hbmMask);
::DeleteDC(hMemDC1);
::DeleteDC(hMemDC2);
::ReleaseDC(NULL, hMainDC);
return hGrayIcon;
} // End of CreateGrayscaleIcon
// This function creates a icon that is 25% darker than the original.
// The resulting icon will have the same size of the original one.
//
// Parameters:
// [IN] hIcon
// Handle to the original icon.
//
// Return value:
// If the function succeeds, the return value is the handle to the newly created
// darker icon.
// If the function fails, the return value is NULL.
//
HICON CButtonST::CreateDarkerIcon(HICON hIcon)
{
HICON hGrayIcon = NULL;
HDC hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
BITMAP bmp;
HBITMAP hOldBmp1 = NULL, hOldBmp2 = NULL;
ICONINFO csII, csGrayII;
BOOL bRetValue = FALSE;
bRetValue = ::GetIconInfo(hIcon, &csII);
if (bRetValue == FALSE) return NULL;
hMainDC = ::GetDC(NULL);
hMemDC1 = ::CreateCompatibleDC(hMainDC);
hMemDC2 = ::CreateCompatibleDC(hMainDC);
if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
{
DWORD dwWidth = csII.xHotspot*2;
DWORD dwHeight = csII.yHotspot*2;
csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, bmp.bmBitsPixel, NULL);
if (csGrayII.hbmColor)
{
hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
//::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, SRCCOPY);
DWORD dwLoopY = 0, dwLoopX = 0;
COLORREF crPixel = 0;
for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
{
for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
{
crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);
if (crPixel)
::SetPixel(hMemDC2, dwLoopX, dwLoopY, DarkenColor(crPixel, 0.25));
else
::SetPixel(hMemDC2, dwLoopX, dwLoopY, crPixel);
} // for
} // for
::SelectObject(hMemDC1, hOldBmp1);
::SelectObject(hMemDC2, hOldBmp2);
csGrayII.hbmMask = csII.hbmMask;
csGrayII.fIcon = TRUE;
hGrayIcon = ::CreateIconIndirect(&csGrayII);
} // if
::DeleteObject(csGrayII.hbmColor);
//::DeleteObject(csGrayII.hbmMask);
} // if
::DeleteObject(csII.hbmColor);
::DeleteObject(csII.hbmMask);
::DeleteDC(hMemDC1);
::DeleteDC(hMemDC2);
::ReleaseDC(NULL, hMainDC);
return hGrayIcon;
} // End of CreateDarkerIcon
COLORREF CButtonST::DarkenColor(COLORREF crColor, double dFactor)
{
if (dFactor > 0.0 && dFactor <= 1.0)
{
BYTE red,green,blue,lightred,lightgreen,lightblue;
red = GetRValue(crColor);
green = GetGValue(crColor);
blue = GetBValue(crColor);
lightred = (BYTE)(red-(dFactor * red));
lightgreen = (BYTE)(green-(dFactor * green));
lightblue = (BYTE)(blue-(dFactor * blue));
crColor = RGB(lightred,lightgreen,lightblue);
} // if
return crColor;
} // End of DarkenColor
// This function assigns icons to the button.
// Any previous icon or bitmap will be removed.
//
// Parameters:
// [IN] nIconIn
// ID number of the icon resource to show when the mouse is over the button.
// Pass NULL to remove any icon from the button.
// [IN] nCxDesiredIn
// Specifies the width, in pixels, of the icon to load.
// [IN] nCyDesiredIn
// Specifies the height, in pixels, of the icon to load.
// [IN] nIconOut
// ID number of the icon resource to show when the mouse is outside the button.
// Can be NULL.
// If this parameter is the special value BTNST_AUTO_GRAY (cast to int) the second
// icon will be automatically created starting from nIconIn and converted to grayscale.
// If this parameter is the special value BTNST_AUTO_DARKER (cast to int) the second
// icon will be automatically created 25% darker starting from nIconIn.
// [IN] nCxDesiredOut
// Specifies the width, in pixels, of the icon to load.
// [IN] nCyDesiredOut
// Specifies the height, in pixels, of the icon to load.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDRESOURCE
// Failed loading the specified resource.
//
DWORD CButtonST::SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, int nIconOut, int nCxDesiredOut, int nCyDesiredOut)
{
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, nCxDesiredIn, nCyDesiredIn, 0);
// Set icon when the mouse is OUT the button
switch (nIconOut)
{
case NULL:
break;
case (int)BTNST_AUTO_GRAY:
hIconOut = BTNST_AUTO_GRAY;
break;
case (int)BTNST_AUTO_DARKER:
hIconOut = BTNST_AUTO_DARKER;
break;
default:
hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut), IMAGE_ICON, nCxDesiredOut, nCyDesiredOut, 0);
break;
} // switch
return SetIcon(hIconIn, hIconOut);
} // End of SetIcon
// This function assigns icons to the button.
// Any previous icon or bitmap will be removed.
//
// Parameters:
// [IN] nIconIn
// ID number of the icon resource to show when the mouse is over the button.
// Pass NULL to remove any icon from the button.
// [IN] nIconOut
// ID number of the icon resource to show when the mouse is outside the button.
// Can be NULL.
// If this parameter is the special value BTNST_AUTO_GRAY (cast to int) the second
// icon will be automatically created starting from nIconIn and converted to grayscale.
// If this parameter is the special value BTNST_AUTO_DARKER (cast to int) the second
// icon will be automatically created 25% darker starting from nIconIn.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDRESOURCE
// Failed loading the specified resource.
//
DWORD CButtonST::SetIcon(int nIconIn, int nIconOut)
{
return SetIcon(nIconIn, 0, 0, nIconOut, 0, 0);
} // End of SetIcon
// This function assigns icons to the button.
// Any previous icon or bitmap will be removed.
//
// Parameters:
// [IN] hIconIn
// Handle fo the icon to show when the mouse is over the button.
// Pass NULL to remove any icon from the button.
// [IN] hIconOut
// Handle to the icon to show when the mouse is outside the button.
// Can be NULL.
// If this parameter is the special value BTNST_AUTO_GRAY the second
// icon will be automatically created starting from hIconIn and converted to grayscale.
// If this parameter is the special value BTNST_AUTO_DARKER the second
// icon will be automatically created 25% darker starting from hIconIn.
//
// Return value:
// BTNST_OK
// Function executed successfully.
// BTNST_INVALIDRESOURCE
// Failed loading the specified resource.
//
DWORD CButtonST::SetIcon(HICON hIconIn, HICON hIconOut)
{
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)
{
switch ((int)hIconOut)
{
case (int)BTNST_AUTO_GRAY:
hIconOut = CreateGrayscaleIcon(hIconIn);
break;
case (int)BTNST_AUTO_DARKER:
hIconOut = CreateDarkerIcon(hIconIn);
break;
} // switch
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.
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -