📄 cjcolorpicker.cpp
字号:
// CJColorPicker.cpp : implementation file
//
// CJColorPicker is a drop-in Color picker control. Check out the
// header file or the accompanying HTML doc file for details.
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Extended by Alexander Bischofberger (bischofb@informatik.tu-muenchen.de)
// Copyright (c) 1998.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to your
// computer, causes your pet cat to fall ill, increases baldness or
// makes you car start emitting strange noises when you start it up.
//
// Expect bugs.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
// Updated 16 May 1998
// 31 May 1998 - added Default text (CJM)
// 9 Jan 1999 - minor vis update
//
/////////////////////////////////////////////////////////////////////////////
/****************************************************************************
*
* $Date: 13/12/02 23:04 $
* $Revision: 2 $
* $Archive: /Projects/Libraries/CJLibrary/CJLibrary/CJColorPicker.cpp $
*
* $History: CJColorPicker.cpp $
*
* ***************** Version 2 *****************
* User: Anna Date: 13/12/02 Time: 23:04
* Updated in $/Projects/Libraries/CJLibrary/CJLibrary
* CCJColorPicker is now theme aware
*
*
* ***************** Version 5 *****************
* User: Kirk Stowell Date: 10/31/99 Time: 11:49p
* Updated in $/CodeJock/CJLibrary
* Overrode OnEraseBkgnd(...), OnPaint() and made modifications to
* DrawItem(...) for flicker free drawing.
*
* Modified resource include for static builds.
*
* Added method CheckTextColor(...) which tests if the intensity of the
* color is greater as 128. If the intensity < 128 => color is dark, so
* the text must be light. Stephane Routelous
* [routelous@cad-cam-concept.de]
*
* Fixed potential resource and memory leak problems.
*
* Removed un-necessary calls to GetParent(), a call is made only once at
* initialization, to ensure we are working with a valid handle.
*
* Made class methods virtual for inheritance purposes.
*
* ***************** Version 4 *****************
* User: Kirk Stowell Date: 10/14/99 Time: 12:22p
* Updated in $/CodeJock/CJLibrary
* Added source control history to file header.
*
***************************************************************************/
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <tmschema.h>
#include "CJColorPopup.h"
#include "CJColorPicker.h"
#include "CJResource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
void AFXAPI DDX_CJColorPicker(CDataExchange *pDX, int nIDC, COLORREF& crColor)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
ASSERT (hWndCtrl != NULL);
CCJColorPicker* pColorPicker = (CCJColorPicker*) CWnd::FromHandle(hWndCtrl);
if (pDX->m_bSaveAndValidate)
{
crColor = pColorPicker->GetColor();
}
else // initializing
{
pColorPicker->SetColor(crColor);
}
}
/////////////////////////////////////////////////////////////////////////////
// CCJColorPicker
CCJColorPicker::CCJColorPicker()
{
SetBkColor(GetSysColor(COLOR_3DFACE));
SetTextColor(GetSysColor(COLOR_BTNTEXT));
m_bTrackSelection = FALSE;
m_nSelectionMode = CP_MODE_BK;
m_bActive = FALSE;
// 1999-06-11 begin mods KStowell
m_strDefaultText.LoadString( IDS_COLOR_AUTO );
m_strCustomText.LoadString( IDS_COLOR_CUST );
// 1999-06-11 end mods KStowell
// 10-14-1999 - Kirk Stowell
m_pParentWnd = NULL;
// AJM 5.12.2002
m_pTheme = NULL;
m_bMouseOver = FALSE;
}
CCJColorPicker::~CCJColorPicker()
{
}
IMPLEMENT_DYNCREATE(CCJColorPicker, CButton)
BEGIN_MESSAGE_MAP(CCJColorPicker, CButton)
//{{AFX_MSG_MAP(CCJColorPicker)
ON_WM_CREATE()
ON_WM_ERASEBKGND()
//ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_CONTROL_REFLECT_EX( BN_CLICKED, OnClicked)
//}}AFX_MSG_MAP
ON_MESSAGE( CPN_SELENDOK, OnSelEndOK)
ON_MESSAGE( CPN_SELENDCANCEL, OnSelEndCancel)
ON_MESSAGE( CPN_SELCHANGE, OnSelChange)
ON_MESSAGE( WM_MOUSELEAVE, OnMsgMouseLeave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCJColorPicker message handlers
LONG CCJColorPicker::OnSelEndOK(UINT lParam, LONG /*wParam*/)
{
COLORREF crNewColor = (COLORREF) lParam;
m_bActive = FALSE;
SetColor(crNewColor);
//BEGIN SRO
CheckTextColor(crNewColor);
//END SRO
// 10-14-1999 - Kirk Stowell - Removed redundant calls to GetParent().
if (m_pParentWnd) {
m_pParentWnd->SendMessage(CPN_CLOSEUP, lParam, (WPARAM) GetDlgCtrlID());
m_pParentWnd->SendMessage(CPN_SELENDOK, lParam, (WPARAM) GetDlgCtrlID());
}
if (crNewColor != GetColor()) {
if (m_pParentWnd) {
m_pParentWnd->SendMessage(CPN_SELCHANGE, lParam, (WPARAM) GetDlgCtrlID());
}
}
return TRUE;
}
LONG CCJColorPicker::OnSelEndCancel(UINT lParam, LONG /*wParam*/)
{
m_bActive = FALSE;
SetColor((COLORREF) lParam);
//BEGIN SRO
CheckTextColor((COLORREF) lParam);
//END SRO
// 10-14-1999 - Kirk Stowell - Removed redundant calls to GetParent().
if (m_pParentWnd) {
m_pParentWnd->SendMessage(CPN_CLOSEUP, lParam, (WPARAM) GetDlgCtrlID());
m_pParentWnd->SendMessage(CPN_SELENDCANCEL, lParam, (WPARAM) GetDlgCtrlID());
}
return TRUE;
}
LONG CCJColorPicker::OnSelChange(UINT lParam, LONG /*wParam*/)
{
if (m_bTrackSelection) {
SetColor((COLORREF) lParam);
//BEGIN SRO
CheckTextColor((COLORREF) lParam);
//END SRO
}
// 10-14-1999 - Kirk Stowell - Removed redundant calls to GetParent().
if (m_pParentWnd) {
m_pParentWnd->SendMessage(CPN_SELCHANGE, lParam, (WPARAM) GetDlgCtrlID());
}
return TRUE;
}
int CCJColorPicker::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;
SetWindowSize(); // resize appropriately
return 0;
}
// On mouse click, create and show a CJColorPopup window for Color selection
BOOL CCJColorPicker::OnClicked()
{
m_bActive = TRUE;
CRect rect;
GetWindowRect(rect);
new CCJColorPopup(CPoint(rect.left, rect.bottom), // Point to display popup
GetColor(), // Selected Color
this, // parent
m_strDefaultText, // "Default" text area
m_strCustomText); // Custom Text
// 10-14-1999 - Kirk Stowell - Removed redundant calls to GetParent().
if (m_pParentWnd) {
m_pParentWnd->SendMessage(CPN_DROPDOWN, (LPARAM)GetColor(), (WPARAM) GetDlgCtrlID());
}
// Docs say I should return FALSE to stop the parent also getting the message.
// HA! What a joke.
return TRUE;
}
// 1999-06-11 begin mods KStowell
void CCJColorPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct != NULL);
// define some temporary variables.
CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
CRect rcItem = lpDrawItemStruct->rcItem;
int nState = lpDrawItemStruct->itemState;
DWORD dwState = EDGE_RAISED;
DWORD dwArrow = DFCS_SCROLLDOWN;
BOOL bIsThemed = ( (NULL != m_pTheme) && m_pTheme->IsAppThemed() );
HTHEME hTheme = NULL;
HTHEME hArrowTheme = NULL;
if (bIsThemed)
{
hTheme = m_pTheme->OpenThemeData(GetSafeHwnd(), L"BUTTON");
hArrowTheme = m_pTheme->OpenThemeData(GetSafeHwnd(), L"COMBOBOX");
}
if (NULL != hTheme)
{
int iStateId = PBS_NORMAL;
if ( (nState & ODS_DISABLED) != 0)
{
iStateId = PBS_DISABLED;
}
else
{
if ( (nState & ODS_HOTLIGHT) != 0 || m_bMouseOver)
{
iStateId = PBS_HOT;
}
if ( (nState & ODS_SELECTED) != 0 || m_bActive)
{
iStateId |= PBS_PRESSED;
}
if (nState & ODS_FOCUS)
{
iStateId |= PBS_DEFAULTED;
}
if ( (nState & ODS_DEFAULT) != 0)
{
iStateId |= PBS_DEFAULTED;
}
}
// Draw the background image defined by the visual style
m_pTheme->DrawThemeBackground( hTheme,
m_hWnd,
pDC->GetSafeHdc(),
BP_PUSHBUTTON,
iStateId,
&rcItem,
NULL);
// Get the size of the content area for the background defined by the visual style.
m_pTheme->GetThemeBackgroundContentRect(hTheme,
pDC->GetSafeHdc(),
BP_PUSHBUTTON,
iStateId,
&rcItem,
&rcItem);
}
else
{
// Paint the background.
pDC->FillSolidRect(rcItem, ::GetSysColor(COLOR_3DFACE));
}
// set display flags based on state.
if((nState & ODS_SELECTED) || (m_bActive == TRUE)) {
dwState = EDGE_SUNKEN;
dwArrow = DFCS_SCROLLDOWN|DFCS_PUSHED;
}
if(nState & ODS_DISABLED) {
dwArrow = DFCS_SCROLLDOWN|DFCS_INACTIVE;
}
// Draw the drop arrow and the colour box
if (NULL != hArrowTheme)
{
CRect rcArrow( rcItem );
int nArrowWidth = 16 + ::GetSystemMetrics(SM_CXEDGE) / 2;
rcArrow.left = rcArrow.right - nArrowWidth;
rcArrow.DeflateRect(2,1);
rcArrow.OffsetRect(1,0);
int iStateId = CBXS_NORMAL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -