📄 cjcolorpicker.cpp
字号:
if ( (nState & ODS_DISABLED) != 0)
{
iStateId = CBXS_DISABLED;
}
else
{
if ( (nState & ODS_SELECTED) != 0 || m_bActive)
{
iStateId |= CBXS_PRESSED;
}
if ( (nState & ODS_HOTLIGHT) != 0 || m_bMouseOver)
{
iStateId = CBXS_HOT;
}
}
// Draw the drop arrow
// We use the drop arrow component from a combobox
m_pTheme->DrawThemeBackground( hArrowTheme,
m_hWnd,
pDC->GetSafeHdc(),
CP_DROPDOWNBUTTON,
iStateId,
&rcArrow,
NULL);
// draw the separator line.
CPen penShadow( PS_SOLID, 1, ::GetSysColor( COLOR_3DSHADOW ));
CPen* pOldPen = pDC->SelectObject( &penShadow );
CPen penHilite( PS_SOLID, 1, ::GetSysColor( COLOR_3DHILIGHT ));
pDC->SelectObject( &penHilite );
// Draw the colour box.
rcItem.DeflateRect( 1, 1 );
rcItem.right = rcArrow.left-1;
CBrush brush( ((nState & ODS_DISABLED) || m_crColorBk == CLR_DEFAULT)?
::GetSysColor(COLOR_3DFACE) : m_crColorBk);
CBrush* pOldBrush = (CBrush*)pDC->SelectObject( &brush );
pDC->SelectStockObject(( nState & ODS_DISABLED )?WHITE_PEN:BLACK_PEN );
pDC->Rectangle( rcItem );
// Draw the window text (if any)
CString strText;
GetWindowText(strText);
if (strText.GetLength())
{
pDC->SetBkMode(TRANSPARENT);
if (nState & ODS_DISABLED)
{
rcItem.OffsetRect(1,1);
pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC->DrawText(strText, rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
rcItem.OffsetRect(-1,-1);
pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC->DrawText(strText, rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
}
else
{
pDC->SetTextColor((m_crColorText == CLR_DEFAULT)? 0 : m_crColorText);
pDC->DrawText(strText, rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
}
}
// fix potential resource leak - KStowell - 10-21-99
pDC->SelectObject( pOldPen );
pDC->SelectObject( pOldBrush );
brush.DeleteObject();
penHilite.DeleteObject();
penHilite.DeleteObject();
}
else
{
CRect rcArrow( rcItem );
rcArrow.left = rcArrow.Width()-::GetSystemMetrics( SM_CXHTHUMB );
pDC->DrawFrameControl( &rcArrow, DFC_SCROLL, dwArrow );
pDC->Draw3dRect( rcArrow, ::GetSysColor( COLOR_3DFACE ), ::GetSysColor( COLOR_3DFACE ));
rcArrow.DeflateRect( 1,1 );
pDC->Draw3dRect( rcArrow, ::GetSysColor( COLOR_3DFACE ), ::GetSysColor( COLOR_3DFACE ));
if((nState & ODS_SELECTED) || (m_bActive == TRUE))
rcArrow.OffsetRect( 1,1 );
// draw the seperator line.
CPen penShadow( PS_SOLID, 1, ::GetSysColor( COLOR_3DSHADOW ));
CPen* pOldPen = pDC->SelectObject( &penShadow );
pDC->MoveTo( rcArrow.left-1, rcArrow.top+2 );
pDC->LineTo( rcArrow.left-1, rcArrow.bottom-2 );
CPen penHilite( PS_SOLID, 1, ::GetSysColor( COLOR_3DHILIGHT ));
pDC->SelectObject( &penHilite );
pDC->MoveTo( rcArrow.left, rcArrow.top+2 );
pDC->LineTo( rcArrow.left, rcArrow.bottom-2 );
// draw the control border.
pDC->DrawEdge( rcItem, dwState, BF_RECT );
// draw the focus rect.
rcItem.DeflateRect( 2, 2 );
pDC->Draw3dRect( rcItem, ::GetSysColor( COLOR_3DFACE ), ::GetSysColor( COLOR_3DFACE ) );
if( nState & ODS_FOCUS ) {
pDC->DrawFocusRect( rcItem );
}
// draw the color box.
if((nState & ODS_SELECTED) || (m_bActive == TRUE))
rcItem.OffsetRect( 1,1 );
rcItem.DeflateRect( 2, 2 );
rcItem.right = rcArrow.left-4;
CBrush brush( ((nState & ODS_DISABLED) || m_crColorBk == CLR_DEFAULT)?
::GetSysColor(COLOR_3DFACE) : m_crColorBk);
CBrush* pOldBrush = (CBrush*)pDC->SelectObject( &brush );
pDC->SelectStockObject(( nState & ODS_DISABLED )?WHITE_PEN:BLACK_PEN );
pDC->Rectangle( rcItem );
// Draw the window text (if any)
CString strText;
GetWindowText(strText);
if (strText.GetLength())
{
pDC->SetBkMode(TRANSPARENT);
if (nState & ODS_DISABLED)
{
rcItem.OffsetRect(1,1);
pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC->DrawText(strText, rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
rcItem.OffsetRect(-1,-1);
pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC->DrawText(strText, rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
}
else
{
pDC->SetTextColor((m_crColorText == CLR_DEFAULT)? 0 : m_crColorText);
pDC->DrawText(strText, rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
}
}
// fix potential resource leak - KStowell - 10-21-99
pDC->SelectObject( pOldPen );
pDC->SelectObject( pOldBrush );
brush.DeleteObject();
penHilite.DeleteObject();
penHilite.DeleteObject();
}
}
// 1999-06-11 end mods KStowell
/////////////////////////////////////////////////////////////////////////////
// CCJColorPicker overrides
void CCJColorPicker::PreSubclassWindow()
{
ModifyStyle(0, BS_OWNERDRAW); // Make it owner drawn
CButton::PreSubclassWindow();
SetWindowSize(); // resize appropriately
}
/////////////////////////////////////////////////////////////////////////////
// CCJColorPicker operations
void CCJColorPicker::SetThemeHelper(CCJThemeHelper* pTheme)
{
m_pTheme = pTheme;
}
COLORREF CCJColorPicker::GetColor()
{
return (m_nSelectionMode == CP_MODE_TEXT)?
GetTextColor(): GetBkColor();
}
void CCJColorPicker::SetColor(COLORREF crColor)
{
(m_nSelectionMode == CP_MODE_TEXT)?
SetTextColor(crColor): SetBkColor(crColor);
}
void CCJColorPicker::SetBkColor(COLORREF crColorBk)
{
m_crColorBk = crColorBk;
//BEGIN SRO
CheckTextColor(crColorBk);
//END SRO
if (IsWindow(m_hWnd)) RedrawWindow();
}
void CCJColorPicker::SetTextColor(COLORREF crColorText)
{
m_crColorText = crColorText;
if (IsWindow(m_hWnd)) RedrawWindow();
}
void CCJColorPicker::SetDefaultText(LPCTSTR szDefaultText)
{
m_strDefaultText = (szDefaultText)? szDefaultText : _T("");
}
void CCJColorPicker::SetCustomText(LPCTSTR szCustomText)
{
m_strCustomText = (szCustomText)? szCustomText : _T("");
}
/////////////////////////////////////////////////////////////////////////////
// CCJColorPicker implementation
void CCJColorPicker::SetWindowSize()
{
// 10-14-1999 - Kirk Stowell - Added to help reduce flicker, we only need to make
// a call to GetParent() once at initialization.
m_pParentWnd = GetParent();
BOOL bIsThemed = ( (NULL != m_pTheme) && m_pTheme->IsAppThemed() );
if (!bIsThemed)
{
// Get size dimensions of edges
CSize MarginSize(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));
// Get size of dropdown arrow
int nArrowWidth = max(::GetSystemMetrics(SM_CXHTHUMB), 5*MarginSize.cx);
int nArrowHeight = max(::GetSystemMetrics(SM_CYVTHUMB), 5*MarginSize.cy);
CSize ArrowSize(max(nArrowWidth, nArrowHeight), max(nArrowWidth, nArrowHeight));
// Get window size
CRect rect;
GetWindowRect(rect);
if (m_pParentWnd) {
m_pParentWnd->ScreenToClient(rect);
}
// Set window size at least as wide as 2 arrows, and as high as arrow + margins
int nWidth = max(rect.Width(), 2*ArrowSize.cx + 2*MarginSize.cx);
MoveWindow(rect.left, rect.top, nWidth, ArrowSize.cy+2*MarginSize.cy, TRUE);
// Get the new coords of this window
GetWindowRect(rect);
ScreenToClient(rect);
// Get the rect where the arrow goes, and convert to client coords.
m_ArrowRect.SetRect(rect.right - ArrowSize.cx - MarginSize.cx,
rect.top + MarginSize.cy, rect.right - MarginSize.cx,
rect.bottom - MarginSize.cy);
}
}
void CCJColorPicker::CheckTextColor(COLORREF crColor)
{
int R = GetRValue(crColor);
int G = GetGValue(crColor);
int B = GetBValue(crColor);
double Intensity = .299 * R + .587 * G + .114 * B ;
if ( Intensity >= 127 ) {
SetTextColor(RGB(0,0,0));
} else {
SetTextColor(RGB(255,255,255));
}
}
BOOL CCJColorPicker::OnEraseBkgnd(CDC* pDC)
{
// overridden for flicker-free drawing.
UNUSED_ALWAYS(pDC);
return TRUE;
}
void CCJColorPicker::OnPaint()
{
CPaintDC dc(this); // device context for painting
// KStowell - Get the client rect.
CRect rcClient, rcClip;
dc.GetClipBox( &rcClip );
GetClientRect( &rcClient );
// KStowell - Create a memory device-context. This is done to help reduce
// screen flicker, since we will paint the entire control to the
// off screen device context first.
CDC memDC;
CBitmap bitmap;
memDC.CreateCompatibleDC(&dc);
bitmap.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
// KStowell - Repaint the background.
memDC.FillSolidRect( rcClient, ::GetSysColor(COLOR_WINDOW) );
// let the control do its default drawing.
CWnd::DefWindowProc( WM_PAINT, (WPARAM)memDC.m_hDC, 0 );
// KStowell - Copy the memory device context back into the original DC via BitBlt().
dc.BitBlt( rcClip.left, rcClip.top, rcClip.Width(), rcClip.Height(), &memDC,
rcClip.left, rcClip.top, SRCCOPY );
// KStowell - Cleanup resources.
memDC.SelectObject(pOldBitmap);
memDC.DeleteDC();
bitmap.DeleteObject();
}
// AJM 6.12.2002 added to support "hot" events
void CCJColorPicker::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bMouseOver)
{
m_bMouseOver = TRUE;
TRACKMOUSEEVENT tme;
tme .cbSize = sizeof (tme);
tme .dwFlags = TME_LEAVE;
tme .hwndTrack = m_hWnd;
::_TrackMouseEvent (&tme);
InvalidateRect (NULL);
}
CButton::OnMouseMove(nFlags, point);
}
LRESULT CCJColorPicker::OnMsgMouseLeave(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
if (m_bMouseOver)
{
m_bMouseOver = FALSE;
InvalidateRect (NULL);
}
return Default();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -