📄 iconbutton.cpp
字号:
{
CDC *pDC = CDC::FromHandle(lpDIS->hDC);
BOOL bIsPressed = (lpDIS->itemState & ODS_SELECTED);
BOOL bIsFocused = (lpDIS->itemState & ODS_FOCUS);
BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
CRect itemRect = lpDIS->rcItem;
if(m_hIcon!=NULL)
m_pBKObject->Draw(pDC,itemRect);
else
m_pBKObject->Draw(pDC,0,0);
if (bIsFocused)
{
CBrush br(RGB(0,0,0));
pDC->FrameRect(&itemRect, &br);
itemRect.DeflateRect(1, 1);
}
COLORREF bgColor;
if ((m_MouseOnButton == TRUE) || (bIsPressed))
bgColor = GetActiveBgColor();
else
bgColor = GetInactiveBgColor();
if (bIsPressed)
{
Line(itemRect.left, itemRect.bottom-1,itemRect.left, itemRect.top,COLOR_BLACK,pDC);
Line(itemRect.left, itemRect.top,itemRect.right, itemRect.top,COLOR_BLACK,pDC);
Line(itemRect.left+1, itemRect.bottom-1,itemRect.left+1, itemRect.top+1,COLOR_DARK_GRAY,pDC);
Line(itemRect.left+1, itemRect.top+1,itemRect.right, itemRect.top+1,COLOR_DARK_GRAY,pDC);
Line(itemRect.left, itemRect.bottom-1,itemRect.right-1, itemRect.bottom-1,COLOR_WHITE,pDC);
Line(itemRect.right-1, itemRect.bottom-1,itemRect.right-1, itemRect.top-1,COLOR_WHITE,pDC);
Line(itemRect.left+1, itemRect.bottom-2,itemRect.right-2, itemRect.bottom-2,COLOR_GRAY,pDC);
Line(itemRect.right-2, itemRect.bottom-2,itemRect.right-2, itemRect.top,COLOR_GRAY,pDC);
}
else
{
Line(itemRect.left, itemRect.bottom-1,itemRect.left, itemRect.top,COLOR_WHITE,pDC);
Line(itemRect.left, itemRect.top,itemRect.right, itemRect.top,COLOR_WHITE,pDC);
Line(itemRect.left+1, itemRect.bottom-1,itemRect.left+1, itemRect.top+1,COLOR_GRAY,pDC);
Line(itemRect.left+1, itemRect.top+1,itemRect.right, itemRect.top+1,COLOR_GRAY,pDC);
Line(itemRect.left, itemRect.bottom-1,itemRect.right-1, itemRect.bottom-1,COLOR_BLACK,pDC);
Line(itemRect.right-1, itemRect.bottom-1,itemRect.right-1, itemRect.top-1,COLOR_BLACK,pDC);
Line(itemRect.left+1, itemRect.bottom-2,itemRect.right-2, itemRect.bottom-2,COLOR_DARK_GRAY,pDC);
Line(itemRect.right-2, itemRect.bottom-2,itemRect.right-2, itemRect.top,COLOR_DARK_GRAY,pDC);
}
CRect captionRect = lpDIS->rcItem;
if (m_hIcon != NULL)
DrawTheIcon(pDC,&lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
if (bIsPressed)
captionRect.OffsetRect(1, 1);
CRect centerRect = captionRect;
if (m_bDrawFlatFocus == TRUE)
{
if (bIsFocused)
{
CRect focusRect = itemRect;
focusRect.DeflateRect(3, 3);
pDC->DrawFocusRect(&focusRect);
}
}
} // End of DrawItem
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::DrawTheIcon(CDC* pDC, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
{
CRect iconRect = rcItem;
switch (m_nAlign)
{
case ST_ALIGN_HORIZ:
iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
break;
case ST_ALIGN_VERT:
// Center the icon horizontally
iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
break;
}
// If button is pressed then press the icon also
if (IsPressed) iconRect.OffsetRect(1, 1);
// Ole'!
pDC->DrawState(iconRect.TopLeft(),
iconRect.Size(),
m_hIcon,(IsDisabled ? DSS_DISABLED : DSS_NORMAL),
(CBrush*)NULL);
} // End of DrawTheIcon
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::PreSubclassWindow()
{
// Add BS_OWNERDRAW style
SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);
CButton::PreSubclassWindow();
} // End of PreSubclassWindow
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::SetDefaultInactiveBgColor(BOOL bRepaint)
{
m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE);
if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultInactiveBgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::SetInactiveBgColor(COLORREF crNew, BOOL bRepaint)
{
m_crInactiveBg = crNew;
if (bRepaint == TRUE) Invalidate();
} // End of SetInactiveBgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
const COLORREF CIconButton::GetInactiveBgColor()
{
return m_crInactiveBg;
} // End of GetInactiveBgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::SetDefaultInactiveFgColor(BOOL bRepaint)
{
m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT);
if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultInactiveFgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::SetInactiveFgColor(COLORREF crNew, BOOL bRepaint)
{
m_crInactiveFg = crNew;
if (bRepaint == TRUE) Invalidate();
} // End of SetInactiveFgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
const COLORREF CIconButton::GetInactiveFgColor()
{
return m_crInactiveFg;
} // End of GetInactiveFgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::SetDefaultActiveBgColor(BOOL bRepaint)
{
m_crActiveBg = ::GetSysColor(COLOR_BTNFACE);
if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultActiveBgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::SetActiveBgColor(COLORREF crNew, BOOL bRepaint)
{
m_crActiveBg = crNew;
if (bRepaint == TRUE) Invalidate();
} // End of SetActiveBgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
const COLORREF CIconButton::GetActiveBgColor()
{
return m_crActiveBg;
} // End of GetActiveBgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::SetDefaultActiveFgColor(BOOL bRepaint)
{
m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT);
if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultActiveFgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CIconButton::SetActiveFgColor(COLORREF crNew, BOOL bRepaint)
{
m_crActiveFg = crNew;
if (bRepaint == TRUE) Invalidate();
} // End of SetActiveFgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
const COLORREF CIconButton::GetActiveFgColor()
{
return m_crActiveFg;
} // End of GetActiveFgColor
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
BOOL CIconButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (m_hCursor != NULL)
{
::SetCursor(m_hCursor);
return TRUE;
}
return CButton::OnSetCursor(pWnd, nHitTest, message);
} // End of OnSetCursor
////////////////////////////////////////
//
///////////////////////////////////////
BOOL CIconButton::PreTranslateMessage(MSG* pMsg)
{
switch(pMsg->message)
{
case WM_LBUTTONDOWN:
case WM_MOUSEMOVE:
case WM_LBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
if (m_pToolTip)
{
CPoint ptCurrentPos;
ptCurrentPos.x = LOWORD(pMsg->lParam);
ptCurrentPos.y = HIWORD(pMsg->lParam);
if (m_rectSearch.PtInRect(ptCurrentPos) == FALSE)
{
m_pToolTip->Activate(FALSE);
}
m_pToolTip->Activate(TRUE);
m_pToolTip->RelayEvent(pMsg);
}
}
return CButton::PreTranslateMessage(pMsg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -