// PaletteDLG.cpp : implementation file // #include "stdafx.h" #include "vector.h" #include "PaletteDLG.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif typedef struct _PALETTEINFO { COLORREF *prgbList; L_UINT32 uCount; }PALETTEINFO, *pPALETTEINFO; ///////////////////////////////////////////////////////////////////////////// // CPaletteDLG dialog CPaletteDLG::CPaletteDLG(CWnd* pParent /*=NULL*/) : CDialog(CPaletteDLG::IDD, pParent) { //{{AFX_DATA_INIT(CPaletteDLG) //}}AFX_DATA_INIT } void CPaletteDLG::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPaletteDLG) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CPaletteDLG, CDialog) //{{AFX_MSG_MAP(CPaletteDLG) ON_WM_MEASUREITEM() ON_WM_DRAWITEM() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPaletteDLG message handlers void CPaletteDLG::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) { lpMeasureItemStruct->itemHeight = 20; CDialog::OnMeasureItem(nIDCtl, lpMeasureItemStruct); } void CPaletteDLG::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) { HBRUSH hOldBrush; COLORREF rgbText; COLORREF rgbValue; CListBox *pList; L_TCHAR szBuffer[ 20 ]; LOGBRUSH LogBrush; LOGPEN LogPen; HPEN hOldPen; if( IDC_PALETTE_LIST == nIDCtl ) { if( ODT_LISTBOX == lpDrawItemStruct->CtlType && -1 != lpDrawItemStruct->itemID ) { pList = (CListBox *)GetDlgItem( IDC_PALETTE_LIST ); rgbValue = pList->GetItemData( lpDrawItemStruct->itemID ); SetBkMode( lpDrawItemStruct->hDC, TRANSPARENT ); SetTextAlign( lpDrawItemStruct->hDC, TA_CENTER ); rgbText = RGB( 255 - GetRValue( rgbValue ), 255 - GetGValue( rgbValue ), 255 - GetBValue( rgbValue ) ) ; SetTextColor( lpDrawItemStruct->hDC, rgbText ); hOldBrush = (HBRUSH)SelectObject( lpDrawItemStruct->hDC, CreateSolidBrush( rgbValue ) ); Rectangle( lpDrawItemStruct->hDC, 0, lpDrawItemStruct->rcItem.top, lpDrawItemStruct->rcItem.right, lpDrawItemStruct->rcItem.bottom ); wsprintf( szBuffer, TEXT("0x%02X%02X%02X"), GetRValue( rgbValue ), GetGValue( rgbValue ), GetBValue( rgbValue ) ); TextOut( lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.right / 2, ( lpDrawItemStruct->rcItem.bottom + lpDrawItemStruct->rcItem.top ) / 2 - 5 , szBuffer, lstrlen( szBuffer ) ); if ( lpDrawItemStruct->itemState & ODS_SELECTED ) { ZeroMemory( &LogBrush, sizeof(LOGBRUSH) ); LogBrush.lbStyle = BS_NULL; ZeroMemory( &LogPen, sizeof(LOGPEN) ); LogPen.lopnStyle = PS_SOLID; LogPen.lopnColor = (COLORREF) GetSysColor(COLOR_HIGHLIGHT); LogPen.lopnWidth.x = 1; LogPen.lopnWidth.y = 0; SelectObject( lpDrawItemStruct->hDC, CreateBrushIndirect( &LogBrush ) ); hOldPen = (HPEN)SelectObject( lpDrawItemStruct->hDC, CreatePenIndirect( &LogPen ) ); Rectangle( lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.left+1, lpDrawItemStruct->rcItem.top+1, lpDrawItemStruct->rcItem.right-1, lpDrawItemStruct->rcItem.bottom-1 ); DeleteObject( SelectObject( lpDrawItemStruct->hDC, hOldPen ) ); } DeleteObject( SelectObject( lpDrawItemStruct->hDC, hOldBrush ) ); return ; } } CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct); } BOOL CPaletteDLG::OnCommand(WPARAM wParam, LPARAM lParam) { L_INT16 k; L_INT nRet; L_INT nIndex; L_INT nCount; CListBox *pList; LPLOGPALETTE pLogPalette; COLORREF rgbColor; HPALETTE hPalette; COLORDLGPARAMS DlgParams; LDialogColor DlgClr; static COLORREF ccCustColors[ 16 ]; switch( LOWORD( wParam ) ) { case IDOK: pList = (CListBox *)GetDlgItem( IDC_PALETTE_LIST ); nCount = pList->GetCount( ); nRet = SUCCESS; if( nCount > 0 ) { pLogPalette = (LPLOGPALETTE) malloc( sizeof( LOGPALETTE ) + sizeof( PALETTEENTRY ) * 255 ); if( NULL != pLogPalette ) { pLogPalette->palVersion = 0x300; pLogPalette->palNumEntries = 256; for( L_INT16 i = 0 ; i < pLogPalette->palNumEntries ; i++ ) { if( i < nCount ) { k = i; } else { k = (WORD)( nCount - 1 ); } pLogPalette->palPalEntry[ i ].peRed = GetRValue( pList->GetItemData( k ) ); pLogPalette->palPalEntry[ i ].peGreen = GetGValue( pList->GetItemData( k ) ); pLogPalette->palPalEntry[ i ].peBlue = GetBValue( pList->GetItemData( k ) ); pLogPalette->palPalEntry[ i ].peFlags = 0; } hPalette = CreatePalette( pLogPalette ); if( NULL != hPalette ) { nRet = m_pVectorWindow->SetPalette( hPalette ); DeleteObject( hPalette ); } free( pLogPalette ); } } else { m_pVectorWindow->SetPalette( NULL ); } EndDialog( nRet ); return TRUE; case IDCANCEL: EndDialog( SUCCESS_DLG_CANCEL ); return TRUE; case IDC_PALETTE_ADD: DlgParams.crColor = 0; DlgParams.uColorSpace = DLG_COLOR_COLORSPACE_SHOW_RGB; DlgParams.uColorModel = 0; DlgParams.uStructSize = sizeof( COLORDLGPARAMS ); DlgParams.uDlgFlags = (DLG_COLOR_SHOW_OLDCOLOR | DLG_COLOR_SHOW_NEWCOLOR | DLG_COLOR_SHOW_NAME | DLG_COLOR_COLORSPACE_SHOW_HUE | DLG_COLOR_COLORSPACE_SHOW_BRIGHTNESS | DLG_COLOR_COLORSPACE_SHOW_WHEEL | DLG_COLOR_COLORSPACE_SHOW_RGB | DLG_COLOR_COLORSPACE_SHOW_CMY | DLG_COLOR_COLORSPACE_SHOW_CMYK | DLG_COLOR_COLORSPACE_SHOW_LAB | DLG_COLOR_COLORMODEL_SHOW_RGB | DLG_COLOR_COLORMODEL_SHOW_HSB | DLG_COLOR_COLORMODEL_SHOW_HLS | DLG_COLOR_COLORMODEL_SHOW_CMY | DLG_COLOR_COLORMODEL_SHOW_CMYK | DLG_COLOR_COLORMODEL_SHOW_LAB ); DlgClr.SetColorParams (&DlgParams); if( SUCCESS_DLG_OK == DlgClr.DoModalColor (m_hWnd)) { pList = (CListBox *)GetDlgItem( IDC_PALETTE_LIST ); DlgClr.GetColorParams(&DlgParams,sizeof(COLORDLGPARAMS)); rgbColor = DlgParams.crColor ; pList->SetItemData( pList->AddString( TEXT("") ), rgbColor ); } return TRUE; case IDC_PALETTE_REMOVE: pList = (CListBox *)GetDlgItem( IDC_PALETTE_LIST ); nIndex = pList->GetCurSel( ); if( LB_ERR != nIndex ) { pList->DeleteString( nIndex ); GetDlgItem( IDC_PALETTE_REMOVE )->EnableWindow( FALSE ); } break; case IDC_PALETTE_LIST: pList = (CListBox *)GetDlgItem( IDC_PALETTE_LIST ); nIndex = pList->GetCurSel( ); GetDlgItem( IDC_PALETTE_REMOVE )->EnableWindow( LB_ERR != nIndex ); return TRUE; } return CDialog::OnCommand(wParam, lParam); } BOOL CPaletteDLG::OnInitDialog() { L_INT nIndex; PALETTEINFO PaletteInfo; HPALETTE hPalette; PALETTEENTRY PalEnt[ 256 ]; CListBox *pColorList; CDialog::OnInitDialog(); ZeroMemory( &PaletteInfo, sizeof( PaletteInfo ) ); hPalette = m_pVectorWindow->GetPalette( ); if( NULL != hPalette ) { /* get palette entries and load them to the Palette info structure */ GetPaletteEntries( hPalette, 0, sizeof( PalEnt ) / sizeof( PalEnt[ 0 ] ), PalEnt ); /* search the palette to see how many actual item exist */ PaletteInfo.uCount = sizeof( PalEnt ) / sizeof( PalEnt[ 0 ] ); PaletteInfo.prgbList = (COLORREF *)malloc( sizeof( COLORREF ) * PaletteInfo.uCount ); for( L_INT i = 1 ; i < sizeof( PalEnt ) / sizeof( PalEnt[ 0 ] ) ; i++ ) { if( PalEnt[ i ].peRed == PalEnt[ i - 1 ].peRed && PalEnt[ i ].peGreen == PalEnt[ i - 1 ].peGreen && PalEnt[ i ].peBlue == PalEnt[ i - 1 ].peBlue ) { PaletteInfo.uCount = i; break; } else { PaletteInfo.prgbList[ i - 1 ] = RGB( PalEnt[ i - 1 ].peRed, PalEnt[ i - 1 ].peGreen, PalEnt[ i - 1 ].peBlue ); } } PaletteInfo.prgbList[ i - 1 ] = RGB( PalEnt[ i - 1 ].peRed, PalEnt[ i - 1 ].peGreen, PalEnt[ i - 1 ].peBlue ); } pColorList = (CListBox *)GetDlgItem( IDC_PALETTE_LIST ); for( L_UINT32 i = 0 ; i < PaletteInfo.uCount ; i++ ) { nIndex = pColorList->AddString( TEXT("") ); pColorList->SetItemData( nIndex, PaletteInfo.prgbList[ i ] ); } free( PaletteInfo.prgbList ); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }