📄 color.cpp
字号:
/////////////////////////////////////////////////////////////////////////
/////////////// cololine.cpp /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "resource.h"
#include "color.h"
#include "colorwnd.h"
IMPLEMENT_DYNAMIC( CColorButton,CButton )
BOOL CColorButton::Create(LPCSTR lpszCaption, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID)
{
ASSERT( dwStyle & BS_OWNERDRAW );
if (pParentWnd == NULL)
pParentWnd = AfxGetApp()->m_pMainWnd;
if (pParentWnd != NULL)
ASSERT_VALID(pParentWnd);
ASSERT(nID);
ASSERT(rect.bottom>=rect.top && rect.right>=rect.left);
return CWnd::Create("button", lpszCaption, dwStyle, rect, pParentWnd, nID);
}
BOOL CColorButton::CreatePopUpWindow (
LPCSTR lpszWindowName,
const RECT& rect,
CWnd* pParentWnd )
{
::CoInitialize(NULL);
IUnknown * pIUnkn;
IColorPalette* pIColorPalette = NULL;
HRESULT hr = ::CoCreateInstance(CLSID_ColorPalette, NULL, CLSCTX_INPROC_SERVER ,
IID_IUnknown, (LPVOID *)&pIUnkn);
if ( FAILED(hr) )
{
ASSERT(0);
return FALSE;
}
if ( pIUnkn->QueryInterface(IID_IColorPalette, (LPVOID*)&pIColorPalette) != S_OK )
{
pIUnkn->Release();
return FALSE;
}
pIUnkn->Release();
ASSERT(pIColorPalette);
COLORREF* pColor = new COLORREF;
*pColor = m_color;
pIColorPalette->PopupWindow((OLE_HANDLE)pParentWnd->m_hWnd,(short)rect.left,(short)rect.top,(unsigned long*)pColor);
m_color = *pColor;
pIColorPalette->Release();
pIColorPalette = NULL;
delete pColor;
::CoUninitialize();
return TRUE;
}
void CColorButton::DrawFrame( LPDRAWITEMSTRUCT lpDIS )
{
ASSERT( lpDIS );
CDC* pDC = CDC::FromHandle( lpDIS->hDC );
CBrush *pOldBrush;
pOldBrush = (CBrush*) pDC->SelectStockObject( LTGRAY_BRUSH );
CRect rectT( lpDIS->rcItem.left,lpDIS->rcItem.top,lpDIS->rcItem.right,lpDIS->rcItem.bottom );
int Width = rectT.Width();
int Height = rectT.Height();
pDC->PatBlt(rectT.left,rectT.top,Width,Height,PATCOPY );
pDC->SelectStockObject( BLACK_BRUSH );
pDC->PatBlt(rectT.left,rectT.top,Width,1,PATCOPY );
pDC->PatBlt(rectT.right-1,rectT.top,1,Height,PATCOPY );
pDC->PatBlt( rectT.left,rectT.top,1,Height,PATCOPY );
pDC->PatBlt( rectT.left,rectT.bottom-1,Width,1,PATCOPY );
pDC->SelectObject( pOldBrush );
}
void CColorButton::DrawItem( LPDRAWITEMSTRUCT lpDIS)
{
ASSERT( lpDIS );
#ifdef _DEBUG
CRect rect;
rect.CopyRect( &lpDIS->rcItem ) ;
ASSERT( rect.Width()>6 && rect.Height() >6 );
#endif
if( lpDIS->itemState & ODS_DISABLED)
{
ASSERT( !m_bShowPopWndFlag );
DrawItemDisabled( lpDIS );
return ;
}
if( ! m_bShowPopWndFlag ) // NO POPUP WINDOW
{
if( lpDIS->itemState & ODS_FOCUS )
{
DrawItemTabFocus( lpDIS ) ; // SHOW THE FOCUS STATE BUTTON
m_byState = FOCUSSTATE;
}
else
{
DrawItemUp( lpDIS ); // SHOW THE UP STATE BUTTON
m_byState = UPSTATE;
}
}
else
{
DrawItemDown ( lpDIS ); // SHOW THE DOWN STATE BUTTON
m_byState = DOWNSTATE;
}
DrawOtherOnButton( lpDIS );
}
#ifdef _DEBUG
#define GRAYTEST 1
int grayDebug = 0;
#endif
#define ROP_PSDPxax 0x00B8074AL
void CColorButton::DrawItemDisabled( LPDRAWITEMSTRUCT lpDIS )
{
ASSERT( lpDIS );
CDC *pdc = CDC::FromHandle( lpDIS->hDC );
CDC memDCMono,memDC;
memDC.CreateCompatibleDC( pdc );
memDCMono.CreateCompatibleDC( pdc );
CRect rectBtn;
GetClientRect( rectBtn );
int Width = rectBtn.Width();
int Height = rectBtn.Height();
ASSERT( Width > 6 && Height > 6 );
CBitmap bitmapT,*pOldBitmap;
bitmapT.CreateCompatibleBitmap( pdc,Width,Height );
pOldBitmap = memDC.SelectObject( &bitmapT );
CBitmap bitmapMono,*pOldBitmapMono;
bitmapMono.CreateBitmap( Width,Height,1,1,NULL );
pOldBitmapMono = memDCMono.SelectObject( &bitmapMono );
HDC hOldDC = lpDIS->hDC;
lpDIS->hDC = ( HDC )memDC.GetSafeHdc();
DrawItemUp( lpDIS );
#ifdef _DEBUG
if( grayDebug == GRAYTEST )
pdc->BitBlt( 0,0,Width,Height,&memDC,0,0,SRCCOPY );
#endif
DrawOtherOnButton( lpDIS );
#ifdef _DEBUG
if( grayDebug == GRAYTEST )
pdc->BitBlt( 0,0,Width,Height,&memDC,0,0,SRCCOPY );
#endif
//create mask
memDCMono.PatBlt( 0,0,Width,Height,WHITENESS );
memDC.SetBkColor( ::GetSysColor( COLOR_BTNFACE ) );
memDCMono.BitBlt( 3,3,Width - 6,Height - 6,&memDC,3,3,SRCCOPY );
memDC.SetBkColor( ::GetSysColor( COLOR_BTNHIGHLIGHT ) );
memDCMono.BitBlt( 3,3,Width - 6,Height - 6,&memDC,3,3,SRCPAINT );
//memDCMono.BitBlt( 4,4,Width - 7,Height - 7,&memDCMono,3,3,SRCAND );
memDC.SetTextColor( 0L); // 0's in mono -> 0 (for ROP)
memDC.SetBkColor((COLORREF)0x00FFFFFFL); // 1's in mono -> 1
CBrush *pOldBrush =(CBrush*) memDC.SelectStockObject( WHITE_BRUSH );
memDC.BitBlt( 1,1,Width - 1 ,Height - 1,&memDCMono,0,0, ROP_PSDPxax );
memDC.SelectStockObject( GRAY_BRUSH );
memDC.BitBlt( 0,0,Width,Height,&memDCMono,0,0,ROP_PSDPxax );
pdc->BitBlt( 0,0,Width,Height,&memDC,0,0,SRCCOPY );
memDC.SelectObject( pOldBrush );
memDCMono.SelectObject( pOldBitmapMono );
memDC.SelectObject( pOldBitmap );
}
void CColorButton::DrawItemUp( LPDRAWITEMSTRUCT lpDIS )
{
ASSERT( lpDIS );
DrawFrame( lpDIS );
CDC* pDC = CDC::FromHandle( lpDIS->hDC );
CBrush *pOldBrush;
CRect rectT( lpDIS->rcItem.left,lpDIS->rcItem.top,lpDIS->rcItem.right,lpDIS->rcItem.bottom );
pOldBrush = ( CBrush*)pDC->SelectStockObject( WHITE_BRUSH );
rectT.InflateRect( -1,-1 );
int Width = rectT.Width();
int Height = rectT.Height();
pDC->PatBlt(rectT.left,rectT.top,Width,2,PATCOPY );
pDC->PatBlt(rectT.left,rectT.top,2,Height,PATCOPY );
pDC->SelectStockObject( GRAY_BRUSH );
pDC->PatBlt(rectT.right-2,rectT.top+2,2,Height-2,PATCOPY );
pDC->PatBlt( rectT.left+2,rectT.bottom-2,Width-2,2,PATCOPY );
pDC->SelectObject( pOldBrush );
}
void CColorButton::DrawItemDown( LPDRAWITEMSTRUCT lpDIS )
{
ASSERT( lpDIS );
DrawFrame( lpDIS );
CDC* pDC = CDC::FromHandle( lpDIS->hDC );
CBrush *pOldBrush;
CRect rectT( lpDIS->rcItem.left,lpDIS->rcItem.top,lpDIS->rcItem.right,lpDIS->rcItem.bottom );
pOldBrush = (CBrush*)pDC->SelectStockObject( GRAY_BRUSH );
rectT.InflateRect( -1,-1 );
pDC->PatBlt(rectT.left,rectT.top,2,rectT.Height(),PATCOPY );
pDC->PatBlt(rectT.left,rectT.top,rectT.Width(),2,PATCOPY );
pDC->SelectObject( pOldBrush );
}
void CColorButton::DrawItemTabFocus( LPDRAWITEMSTRUCT lpDIS )
{
ASSERT( lpDIS);
CDC* pDC = CDC::FromHandle( lpDIS->hDC );
CBrush *pOldBrush;
pOldBrush = (CBrush*) pDC->SelectStockObject( LTGRAY_BRUSH );
CRect rectT( lpDIS->rcItem.left,lpDIS->rcItem.top,lpDIS->rcItem.right,lpDIS->rcItem.bottom );
int Width = rectT.Width();
int Height = rectT.Height();
pDC->PatBlt(rectT.left,rectT.top,Width,Height,PATCOPY );
pDC->SelectStockObject( BLACK_BRUSH );
pDC->PatBlt(rectT.left,rectT.top,Width,2,PATCOPY );
pDC->PatBlt(rectT.right-2,rectT.top,2,Height,PATCOPY );
pDC->PatBlt( rectT.left,rectT.top,2,Height,PATCOPY );
pDC->PatBlt( rectT.left,rectT.bottom-2,Width,2,PATCOPY );
pDC->SelectStockObject( WHITE_BRUSH );
rectT.InflateRect( -2,-2 );
Width = rectT.Width();
Height = rectT.Height();
pDC->PatBlt(rectT.left,rectT.top,Width,2,PATCOPY );
pDC->PatBlt(rectT.left,rectT.top,2,Height,PATCOPY );
pDC->SelectStockObject( GRAY_BRUSH );
pDC->PatBlt(rectT.right-2,rectT.top,2,Height,PATCOPY );
pDC->PatBlt( rectT.left,rectT.bottom-2,Width,2,PATCOPY );
pDC->SelectObject( pOldBrush );
}
void CColorButton::OnLButtonDown( UINT nFlags, CPoint point )
{
OnDown();
}
void CColorButton::OnMouseMove(UINT nFlags,CPoint point)
{
}
void CColorButton::OnDown( void )
{
CRect rectWnd,rectPop;
GetWindowRect(rectWnd);
rectPop.left = rectWnd.left;
rectPop.top = rectWnd.bottom -2;
if( rectPop.left + WINDOW_WIDTH > GetSystemMetrics( SM_CXSCREEN ) )
{
rectPop.left = rectWnd.left - WINDOW_WIDTH;
}
if( rectPop.top + WINDOW_NORMALHEIGHT > GetSystemMetrics( SM_CYSCREEN ) )
{
rectPop.top = rectWnd.top - ( WINDOW_NORMALHEIGHT );
}
if( rectPop.left < 0 )
{
rectPop.left = rectWnd.right;
}
m_bShowPopWndFlag = TRUE ;
if( this != GetFocus() ) SetFocus( );
else
{
DRAWITEMSTRUCT ds;
ds.CtlType = ODT_BUTTON;
ds.hwndItem = GetSafeHwnd();
ds.hDC = ::GetDC( m_hWnd );
GetClientRect( &ds.rcItem );
ds.itemAction = ODA_DRAWENTIRE;
ds.itemState = ODS_SELECTED;
ds.CtlID = GetDlgCtrlID();
GetParent()->SendMessage( WM_DRAWITEM, ds.CtlID,(LPARAM)(LPDRAWITEMSTRUCT)&ds );
::ReleaseDC( m_hWnd,ds.hDC );
}
ScreenToClient( &rectPop );
CreatePopUpWindow( NULL,rectPop,this );
Invalidate();
m_bShowPopWndFlag = FALSE ;
DRAWITEMSTRUCT ds;
ds.CtlType = ODT_BUTTON;
ds.hwndItem = GetSafeHwnd();
ds.hDC = ::GetDC( m_hWnd );
GetClientRect( &ds.rcItem );
ds.itemAction = ODA_FOCUS;
ds.itemState = ODS_FOCUS;
ds.CtlID = GetDlgCtrlID();
GetParent()->SendMessage( WM_DRAWITEM, ds.CtlID,(LPARAM)(LPDRAWITEMSTRUCT)&ds );
::ReleaseDC( m_hWnd,ds.hDC );
}
void CColorButton::OnUp()
{
}
void CColorButton::OnLButtonUp( UINT nFlage, CPoint point )
{
}
void CColorButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
}
void CColorButton::DrawOtherOnButton( LPDRAWITEMSTRUCT lpDIS)
{
ASSERT( lpDIS );
DrawRectOnButton( lpDIS->hDC );
}
void CColorButton::DrawRectOnButton( HDC hDC )
{
HBRUSH hBrush,hOldBrush;
RECT m_ButtonRect ;
::GetClientRect( m_hWnd ,&m_ButtonRect );
ASSERT( m_ButtonRect.right - m_ButtonRect.left > 14
&& m_ButtonRect.bottom - m_ButtonRect.top > 14 );
hBrush=::CreateSolidBrush(m_color);
hOldBrush=(HBRUSH)::SelectObject(hDC,hBrush);
if( m_byState != DOWNSTATE )
::Rectangle(hDC,m_ButtonRect.left+7,m_ButtonRect.top+7,m_ButtonRect.right-7,m_ButtonRect.bottom-7);
else
::Rectangle(hDC,m_ButtonRect.left+8,m_ButtonRect.top+8,m_ButtonRect.right-6,m_ButtonRect.bottom-6);
::SelectObject( hDC,hOldBrush);
::DeleteObject(hBrush);
}
BEGIN_MESSAGE_MAP(CColorButton,CButton) // ENABLE MESSAGE MAP
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -