📄 buttonex.cpp
字号:
// BtnST.cpp : implementation file
//
#include "stdafx.h"
#include "ButtonEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonEx
CButtonEx::CButtonEx()
{
m_MouseOnButton = FALSE;
m_hIconIn = NULL;
m_hIconOut = NULL;
m_cxIcon = 0;
m_cyIcon = 0;
m_hCursor = NULL;
// Default type is "flat" button
m_bIsFlat = TRUE;
// By default draw border in "flat" button
m_bDrawBorder = TRUE;
// By default icon is aligned horizontally
m_nAlign = ST_ALIGN_HORIZ;
// By default show the text button
m_bShowText = TRUE;
// By default, for "flat" button, don't draw the focus rect
m_bDrawFlatFocus = FALSE;
SetDefaultInactiveBgColor();
SetDefaultInactiveFgColor();
SetDefaultActiveBgColor();
SetDefaultActiveFgColor();
} // End of CButtonEx
CButtonEx::~CButtonEx()
{
// Destroy the icons (if any)
if (m_hIconIn != NULL) ::DeleteObject(m_hIconIn);
if (m_hIconOut != NULL) ::DeleteObject(m_hIconOut);
// Destroy the cursor (if any)
if (m_hCursor != NULL) ::DestroyCursor(m_hCursor);
} // End of ~CButtonEx
BEGIN_MESSAGE_MAP(CButtonEx, CButton)
//{{AFX_MSG_MAP(CButtonEx)
ON_WM_CAPTURECHANGED()
ON_WM_SETCURSOR()
ON_WM_KILLFOCUS()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CButtonEx::SetIcon(int nIconInId, int nIconOutId, BYTE cx, BYTE cy)
{
HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconInId),
RT_GROUP_ICON);
// Set icon when the mouse is IN the button
m_hIconIn = (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0);
// Set icon when the mouse is OUT the button
if( nIconOutId ) {
if( nIconOutId == (int)BTNST_AUTO_GRAY )
m_hIconOut = BTNST_AUTO_GRAY;
else
m_hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0);
} // if
if( m_hIconOut ) {
if( m_hIconOut == BTNST_AUTO_GRAY) {
m_hIconOut = CreateGrayscaleIcon( m_hIconIn );
} // if
}
// m_hIconOut = (nIconOutId == NULL) ? m_hIconIn : (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0);
m_cxIcon = cx;
m_cyIcon = cy;
RedrawWindow();
} // End of SetIcon
//////////////////////////////////////////////////////////////////////////
HICON CButtonEx::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(m_hWnd);
hMemDC1 = ::CreateCompatibleDC(hMainDC);
hMemDC2 = ::CreateCompatibleDC(hMainDC);
if( hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL ) return NULL;
if( ::GetObject( csII.hbmColor, sizeof(BITMAP), &bmp ) ) {
csGrayII.hbmColor = ::CreateBitmap( csII.xHotspot*2, csII.yHotspot*2, bmp.bmPlanes, bmp.bmBitsPixel, NULL );
if( csGrayII.hbmColor ) {
hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);
::BitBlt( hMemDC2, 0, 0, csII.xHotspot*2, csII.yHotspot*2, hMemDC1, 0, 0, SRCCOPY );
DWORD dwLoopY = 0, dwLoopX = 0;
COLORREF crPixel = 0;
BYTE byNewPixel = 0;
for( dwLoopY = 0; dwLoopY < csII.yHotspot*2; dwLoopY++ ) {
for( dwLoopX = 0; dwLoopX < csII.xHotspot*2; dwLoopX++ ) {
crPixel = ::GetPixel( hMemDC2, 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 ) );
} // for
} // for
::SelectObject( hMemDC1, hOldBmp1 );
::SelectObject( hMemDC2, hOldBmp2 );
csGrayII.hbmMask = csII.hbmMask;
csGrayII.fIcon = TRUE;
hGrayIcon = ::CreateIconIndirect( &csGrayII );
} // if
::DeleteObject( csGrayII.hbmColor );
} // if
::DeleteObject( csII.hbmColor );
::DeleteObject( csII.hbmMask );
::DeleteDC( hMemDC1 );
::DeleteDC( hMemDC2 );
::ReleaseDC( m_hWnd, hMainDC );
return hGrayIcon;
} // End of CreateGrayscaleIcon
//////////////////////////////////////////////////////////////////////////
BOOL CButtonEx::SetBtnCursor(int nCursorId)
{
HINSTANCE hInstResource;
// Destroy any previous cursor
if (m_hCursor != NULL) ::DestroyCursor(m_hCursor);
m_hCursor = NULL;
// If we want a cursor
if (nCursorId != -1)
{
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nCursorId),
RT_GROUP_CURSOR);
// Load icon resource
m_hCursor = (HCURSOR)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
// If something wrong then return FALSE
if (m_hCursor == NULL) return FALSE;
}
return TRUE;
} // End of SetBtnCursor
void CButtonEx::SetFlat(BOOL bState)
{
m_bIsFlat = bState;
Invalidate();
} // End of SetFlat
BOOL CButtonEx::GetFlat()
{
return m_bIsFlat;
} // End of GetFlat
void CButtonEx::SetAlign(int nAlign)
{
switch (nAlign)
{
case ST_ALIGN_HORIZ:
m_nAlign = ST_ALIGN_HORIZ;
break;
case ST_ALIGN_VERT:
m_nAlign = ST_ALIGN_VERT;
break;
}
Invalidate();
} // End of SetAlign
int CButtonEx::GetAlign()
{
return m_nAlign;
} // End of GetAlign
void CButtonEx::DrawBorder(BOOL bEnable)
{
m_bDrawBorder = bEnable;
} // End of DrawBorder
const char* CButtonEx::GetVersionC()
{
return "2.3";
} // End of GetVersionC
const short CButtonEx::GetVersionI()
{
return 23; // Divide by 10 to get actual version
} // End of GetVersionI
void CButtonEx::SetShowText(BOOL bShow)
{
m_bShowText = bShow;
Invalidate();
} // End of SetShowText
BOOL CButtonEx::GetShowText()
{
return m_bShowText;
} // End of GetShowText
void CButtonEx::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd* pWnd; // Finestra attiva
CWnd* pParent; // Finestra che contiene il bottone
CButton::OnMouseMove(nFlags, point);
m_crActiveBg = RGB( 220, 220, 220 );//assouan
// If the mouse enter the button with the left button pressed
// then do nothing
if (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) return;
// If our button is not flat then do nothing
if (m_bIsFlat == FALSE) return;
pWnd = GetActiveWindow();
pParent = GetOwner();
if ((GetCapture() != this) &&
(
#ifndef ST_LIKEIE
pWnd != NULL &&
#endif
pParent != NULL))
{
m_MouseOnButton = TRUE;
//SetFocus(); // Thanks Ralph!
SetCapture();
Invalidate();
}
else
{
CRect rc;
GetClientRect(&rc);
if (!rc.PtInRect(point))
{
// Redraw only if mouse goes out
if (m_MouseOnButton == TRUE)
{
m_MouseOnButton = FALSE;
Invalidate();
}
// If user is NOT pressing left button then release capture!
if (!(nFlags & MK_LBUTTON)) ReleaseCapture();
}
}
} // End of OnMouseMove
void CButtonEx::OnKillFocus(CWnd * pNewWnd)
{
CButton::OnKillFocus(pNewWnd);
// If our button is not flat then do nothing
if (m_bIsFlat == FALSE) return;
if (m_MouseOnButton == TRUE)
{
m_MouseOnButton = FALSE;
Invalidate();
}
} // End of OnKillFocus
void CButtonEx::OnCaptureChanged(CWnd *pWnd)
{
if (m_MouseOnButton == TRUE)
{
ReleaseCapture();
Invalidate();
}
// Call base message handler
CButton::OnCaptureChanged(pWnd);
} // End of OnCaptureChanged
void CButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
#ifdef ST_USE_MEMDC
CDC *pdrawDC = CDC::FromHandle(lpDIS->hDC);
CMemDC memDC(pdrawDC);
CDC *pDC = &memDC;
#else
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
#endif
CPen *pOldPen;
BOOL bIsPressed = (lpDIS->itemState & ODS_SELECTED);
BOOL bIsFocused = (lpDIS->itemState & ODS_FOCUS);
BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
CRect itemRect = lpDIS->rcItem;
if (m_bIsFlat == FALSE)
{
if (bIsFocused)
{
CBrush br(RGB(0,0,0));
pDC->FrameRect(&itemRect, &br);
itemRect.DeflateRect(1, 1);
}
}
// Prepare draw... paint button's area with background color
COLORREF bgColor;
if ((m_MouseOnButton == TRUE) || (bIsPressed))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -